Windows API OPENFILENAME扩展过滤器问题



我有这个openfile()函数与OPENFILENAME结构,lpstrFilter不过滤文件类型,对话框显示所有类型的文件,我想要防止,它应该通常只显示选定的文件类型,根据扩展名过滤器,也当用户选择另一种文件类型,例如。PNG文件,它应该更新的文件在对话框中,只显示PNG文件,而不是这种情况下,所以这段代码是什么错了?,是否有任何特定的标志可以帮助解决这个问题?

void openfile() {        
ZeroMemory(&opn, sizeof(opn));
opn.lStructSize = sizeof(opn);
opn.hwndOwner = hWindow;
opn.lpstrFile = tz1;
opn.lpstrFile[0] = '';
opn.nMaxFile = sizeof(tz1);
opn.lpstrFilter = "JPG - JPEG File*.JPGPNG File*.PNGBMP - Bitmat File*.BMP";
opn.nFilterIndex = 2;
opn.lpstrFileTitle = NULL;
opn.lpstrTitle = "Select an Image";
opn.nMaxFileTitle = 0;
opn.lpstrInitialDir = NULL; 
opn.hInstance = hInstance;
opn.lpstrDefExt = ("JPG");
opn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
opn.lpfnHook = NULL;
bfile = 0;
if (GetOpenFileName(&opn)) {

if (access(tz1, F_OK) == -1){
// showing some file does not exist message
}
} // if Get
} // openfile()

我在这里发布我的解决方案,它可能会帮助那些有同样问题的人,也许有一个更好的解决方案,但这就是我所尝试的,我添加了一个lpfnHook函数来刷新OPENFILENAME对话框窗口中的文件。

void openfile() { 
ZeroMemory(&opn, sizeof(opn));
opn.lStructSize = sizeof(opn);
opn.hwndOwner = hDlg; // HWND of the OPENFILENAME Dialog Window
opn.lpstrFile = tz1;
opn.lpstrFile[0] = '';
opn.nMaxFile = sizeof(tz1);
opn.lpstrFilter = "JPG - JPEG File*.JPGPNG File*.PNGBMP - Bitmat File*.BMP";
opn.nFilterIndex = fidx; // could be 1,2 or 3
opn.lpstrFileTitle = NULL;
opn.lpstrTitle = "Select an Image";
opn.nMaxFileTitle = 0;
opn.lpstrInitialDir = NULL; // NULL is original
opn.hInstance = hInstance;
opn.lpstrDefExt = NULL;
opn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
opn.lpfnHook = Refresh;     // The lpfnHook function   
bfile = 0;
if (GetOpenFileName(&opn)) {
if (access(tz1, F_OK) == -1){
// file does not exist message
} 

} // if Get
} // openfile()

UINT_PTR CALLBACK Refresh(HWND hDlog, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{

case WM_NOTIFY:

if (opn.nFilterIndex !=fidx)
{
fidx = opn.nFilterIndex;
PostMessage(hDlg, WM_KEYDOWN, VK_F5, 0);

}
break;
}
return TRUE;
} // end of HOOK


相关内容

  • 没有找到相关文章