C的用法:
OPENFILENAME ofn;
memset(&ofn, 0x00, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hYourWnd;
ofn.hInstance = hYourInstance;
ofn.lpstrFilter = lpFilter; // 文件类型过滤器,每个过滤器由两部分组成——描述部分及文件类型(扩展名)——这两部分由ASCII为0的字符分开,多个过滤器之间也由ASCII码为0的字符分开,过滤器最后由两个ASCII吗为0的字符来标识其结束
ofn.nFilterIndex = 0; // 对话框打开时显示第一个过滤器
ofn.lpstrFile = NULL; // 缺省时选择的文件名,可为空
ofn.nMaxFile = 0; // ofn.lpstrFile所指缓冲的大小
ofn.lpstrFileTitle = lpFileBuffer; // 指向存贮文件名部分的缓冲区,可为空
ofn.nMaxFileTitle = 260; // lpFileBuffer缓冲区大小
ofn.lpstrInitialDir = NULL; // 打开对话框时所在的路径
ofn.lpstrTitle = "打开文件"; // 对话框标题
ofn.nFlags = OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST and OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt"; //缺省扩展名if( GetOpenFileName( &ofn ) == 0 )
MessageBox(hYourWnd, "获取文件名失败", "Error", MB_OK and MB_ICONERROR);
OPENFILENAME ofn;
memset(&ofn, 0x00, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hYourWnd;
ofn.hInstance = hYourInstance;
ofn.lpstrFilter = lpFilter; // 文件类型过滤器,每个过滤器由两部分组成——描述部分及文件类型(扩展名)——这两部分由ASCII为0的字符分开,多个过滤器之间也由ASCII码为0的字符分开,过滤器最后由两个ASCII吗为0的字符来标识其结束
ofn.nFilterIndex = 0; // 对话框打开时显示第一个过滤器
ofn.lpstrFile = NULL; // 缺省时选择的文件名,可为空
ofn.nMaxFile = 0; // ofn.lpstrFile所指缓冲的大小
ofn.lpstrFileTitle = lpFileBuffer; // 指向存贮文件名部分的缓冲区,可为空
ofn.nMaxFileTitle = 260; // lpFileBuffer缓冲区大小
ofn.lpstrInitialDir = NULL; // 打开对话框时所在的路径
ofn.lpstrTitle = "打开文件"; // 对话框标题
ofn.nFlags = OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST and OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt"; //缺省扩展名if( GetOpenFileName( &ofn ) == 0 )
MessageBox(hYourWnd, "获取文件名失败", "Error", MB_OK and MB_ICONERROR);
Dim Mystructure As OPENFILENAME
Mystructure.lStructSize = 76
Mystructure.flags = OFN_EXPLORER
Mystructure.hwndOwner = Form1.hWnd
Mystructure.lpstrInitialDir = "C:\"
Mystructure.lpstrFilter = "*.TXT"
Dim i As Integer
i = GetOpenFileName(Mystructure)
End Sub在另一个模块里加上API的声明。
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public 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