gpt4 book ai didi

c - Win32 : Toolbar dialog seems to never get focus and causes the main window to process slow?

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:27 26 4
gpt4 key购买 nike

我正在按照 this tutorial 编写应用程序.我知道本教程已过时,因此,我已调整代码以考虑 unicode。

我有一个看起来像 MDI 的主窗口。然后,我有一个 View 菜单,它可以切换工具栏对话框的显示和隐藏。

当我显示对话框时,它显示了,但是 PUSHBUTTON 没有正确显示。它们仅在我再次单击主窗口时出现。

另外,我似乎无法点击工具栏对话框中的两个 PUSHBUTTON

资源(resource.h)定义如下(仅展示与本题相关的内容):

#define IDD_TOOLBAR              102
#define IDC_PRESS 1000
#define IDC_OTHER 1001
#define ID_VIEW_SHOWTOOLBAR 40002
#define ID_VIEW_HIDETOOLBAR 40003

在我的.rc文件中的对话框如下:

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

并在我的 WndProc 函数中显示如下:

// 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);
}
}

下面是我在 WinMain 函数的消息循环中处理主窗口和对话框的不同消息的方式。

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);
}
}
}

我的问题是:

  1. 我似乎无法点击对话框的按钮。

  2. 当我尝试单击对话框的按钮时,我的主窗口响应自己的消息变得非常缓慢。

也就是说,当我想将我的工具栏对话框显示为无模式对话框时,因为当我以模式显示它时,它工作得很好!

有解决这个问题的线索吗?

谢谢!

最佳答案

正如 DReJ 在上述评论中所说,问题出在我的消息泵中。

麻烦的是我这样写:

while (GetMessageW(&Msg, hWnd, 0, 0) > 0) {
// Processing message here...
}

我要写的是:

while (GetMessageW(&Msg, NULL, 0, 0) > 0) {
// Processing message here...
}

因此,因为我正在获取给定窗口的消息,即 hWnd 实例,所以我的 ToolbarDialog 似乎没有时间完全绘制自己或类似的东西。在这种情况下将 hWnd 替换为 NULL 完全解决了问题。

关于c - Win32 : Toolbar dialog seems to never get focus and causes the main window to process slow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4464740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com