GetOpenFileName 函数未打开对话框
GetOpenFileName function is not opening the dialog box
所以我有这个简单的代码,因为我是 win32 的新手所以不要指望我写出非常困难的代码,但是,这是我的 winProc
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY: PostQuitMessage (0); break;
case WM_CREATE : make_controls(hwnd); break;
case WM_COMMAND: handle_commands(hwnd, wParam, lParam); break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
这是handle_commands
函数
void handle_commands(HWND hwnd, WPARAM wp, LPARAM lp){
if( HIWORD(wp) == BN_CLICKED && LOWORD(wp) == openBtn ){
// openBtn is the only button in the whole application
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '[=11=]' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '[=11=]';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All[=11=]*.*[=11=]Text[=11=]*.TXT[=11=]";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
}
}// this is the end of the handle_commands functions
但问题是它没有打开任何对话框
据我所知,互联网上的人们使用相同的代码成功打开。
是的!我已经包含 commdlg.h 和相应的库
提前致谢!
所以问题是在handle_commands
中,hwnd
被改了。这意味着 OPENFILENAME
结构不知道它的正确所有者,因此,尽管单击按钮触发了正确的代码,但它仍然没有打开对话框。
所以只需注释掉 handle_commands
函数中的 HWND hwnd
行
所以我有这个简单的代码,因为我是 win32 的新手所以不要指望我写出非常困难的代码,但是,这是我的 winProc
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY: PostQuitMessage (0); break;
case WM_CREATE : make_controls(hwnd); break;
case WM_COMMAND: handle_commands(hwnd, wParam, lParam); break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
这是handle_commands
函数
void handle_commands(HWND hwnd, WPARAM wp, LPARAM lp){
if( HIWORD(wp) == BN_CLICKED && LOWORD(wp) == openBtn ){
// openBtn is the only button in the whole application
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '[=11=]' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '[=11=]';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All[=11=]*.*[=11=]Text[=11=]*.TXT[=11=]";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
}
}// this is the end of the handle_commands functions
但问题是它没有打开任何对话框
据我所知,互联网上的人们使用相同的代码成功打开。
是的!我已经包含 commdlg.h 和相应的库
提前致谢!
所以问题是在handle_commands
中,hwnd
被改了。这意味着 OPENFILENAME
结构不知道它的正确所有者,因此,尽管单击按钮触发了正确的代码,但它仍然没有打开对话框。
所以只需注释掉 handle_commands
函数中的 HWND hwnd
行