熱點推薦:
您现在的位置: 電腦知識網 >> 操作系統 >> Windows優化 >> 正文

使用Windows API函數顯示打開文件對話框

2013-11-12 16:48:14  來源: Windows優化 

  平時使用VB編程時需要用到打開文件類似的對話框時我們一般需要添加一個commondialogue的控件使用這個控件能完成如打開另存顏色打印等眾多的功能但是如果一個功能很簡單的程序使用這個控件卻也造成了一些不必要的麻煩一個是需要安裝再個增加了發布文件包的體積
  
  下面介紹如何使用Window API顯示一個打開的對話框
  Generalbas:
  
  Declare Function GetOpenFileName Lib comdlgdll Alias _
  GetOpenFileNameA (pOpenfilename As OPENFILENAME) As Long
  
  Type OPENFILENAME
  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As Long
  lpstrFileTitle As String
  nMaxFileTitle As Long
  lpstrInitialDir As String
  lpstrTitle As String
  flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As String
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
  End Type
  
  在調用對話框的按鈕的click事件輸入下面代碼
  Private Sub FileOpen_Click()
  Dim ofn As OPENFILENAME
  Dim rtn As String
  
  ofnlStructSize = Len(ofn)
  ofnhwndOwner = MehWnd
  ofnhInstance = ApphInstance
  ofnlpstrFilter = 所有文件
  ofnlpstrFile = Space()
  ofnnMaxFile =
  ofnlpstrFileTitle = Space()
  ofnnMaxFileTitle =
  ofnlpstrInitialDir = AppPath
  ofnlpstrTitle = 打開文件
  ofnflags =
  rtn = GetOpenFileName(ofn)
  If rtn >= Then
  msgbox ofnlpstrFile
  Else
  msgbox Cancel Was Pressed
  End If
  End Sub
  
  

From:http://tw.wingwit.com/Article/os/youhua/201311/10921.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.