问题
I'm writing an application following this tutorial. I'm aware that this tutorial dates, and as such, I have adapted the code to take into consideration the unicode.
I have a main window which looks like an MDI. Then, I have a View menu which toggles a Toolbar dialog as to be shown and hidden.
When I show the dialog, it is displayed, but the PUSHBUTTON
s are not displayed correctly. They only appear when I click my main window again.
Plus, I don't seem to be able to click neither of the PUSHBUTTON
s into my toolbar dialog.
The resources (resource.h
) are defined as follows (only showing what is relevant to this question):
#define IDD_TOOLBAR 102
#define IDC_PRESS 1000
#define IDC_OTHER 1001
#define ID_VIEW_SHOWTOOLBAR 40002
#define ID_VIEW_HIDETOOLBAR 40003
And the dialog as follows in my .rc
file:
IDD_TOOLBAR DIALOGEX 0, 0, 85, 50
STYLE DS_FIXEDSYS | DS_MODALFRAME | WS_CAPTION | WS_POPUP
EXSTYLE WS_EX_TOOLWINDOW
CAPTION L"Toolbar"
FONT 8, "MS Shell Dlg"
BEGIN
PUSHBUTTON L"&Press this button", IDC_PRESS, 7, 7, 70, 14
PUSHBUTTON L"&Or this one", IDC_OTHER, 7, 28, 70, 14
END
And showing it as follows in my WndProc function:
// As a global variable I have my toolbar handler.
HWND g_hToolbar = NULL;
BOOL CALLBACK ToolbarDlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case IDC_OTHER:
MessageBoxW(hWnd, L"You just clicked IDC_OTHER!", L"Information", MB_OK | MB_ICONINFORMATION);
break;
case IDC_PRESS:
MessageBoxW(hWnd, L"You just clicked ODC_PRESS!", L"Information", MB_OK | MB_ICONINFORMATION);
break;
default:
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_VIEW_HIDETOOLBAR:
ShowWindow(g_hToolbar, SW_HIDE);
break;
case ID_VIEW_SHOWTOOLBAR:
if (NULL == g_hToolbar)
g_hToolbar = CreateDialogW(GetModuleHandle(NULL)
, MAKEINTRESOURCE(IDD_TOOLBAR)
, hWnd
, ToolbarDlgProc);
ShowWindow(g_hToolbar, SW_SHOW);
break;
}
break;
default:
return DefWindowProcW(hWnd, Msg, wParam, lParam);
}
}
And here's the way I handle the different messages for my main window and my dialog in my message loop in my WinMain function.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// Declaring, registring and creating my main window to hWnd here...
MSG Msg;
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
while (GetMessageW(&Msg, hWnd, 0, 0) > 0) {
if (!IsDialogMessageW(g_hToolbar, &Msg)) {
TranslateMessage(&Msg);
DispatchMessageW(&Msg);
}
}
}
My problem is:
I don't seem to be able to click on my dialog's buttons.
When I attempt to click on my dialog's buttons, my main window becomes very slow to respond to its own messages.
That is, when I want to show my Toolbar dialog as a modeless dialog, because when I show it modal, it works perfectly!
Any clue to solve this issue?
Thanks!
回答1:
The problem is, as DReJ said in the above comment, in my message pump.
The trouble is that I write:
while (GetMessageW(&Msg, hWnd, 0, 0) > 0) {
// Processing message here...
}
And that I shall write:
while (GetMessageW(&Msg, NULL, 0, 0) > 0) {
// Processing message here...
}
So, because I was getting the messages for a given window, the hWnd instance, my ToolbarDialog seemed to lack of time to draw itself completely or something like it. Replacing hWnd for NULL in that scenario solved the problem entirely.
来源:https://stackoverflow.com/questions/4464740/win32-toolbar-dialog-seems-to-never-get-focus-and-causes-the-main-window-to-pro