【发布时间】:2021-09-04 23:39:54
【问题描述】:
我有另一个 SO 问题,那里的人帮助我解决了一个问题。我不明白 DialogProc 应该在每个 case 上返回什么。 DialogProc 返回 TRUE 是什么意思?它与 FALSE 相比如何?有什么区别?
MSDN 表示没有返回值。
INT_PTR CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
{
wchar_t c0_txt[] = L"Title";
LVCOLUMNW col{};
col.mask = LVCF_TEXT | LVCF_WIDTH | LVIF_IMAGE;
col.cx = 60;
col.pszText = c0_txt;
ListView_InsertColumn(GetDlgItem(hWnd, IDC_LIST1), 0, &col);
return TRUE;
}
case WM_NCDESTROY:
PostQuitMessage(0);
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
DestroyWindow(hWnd);
break;
default:
break;
}
break;
case WM_SIZE:
{
const int width = LOWORD(lParam);
const int height = HIWORD(lParam);
// IDC_LIST1 will occupy the entire client area of its parent.
// Adjust as needed.
MoveWindow(GetDlgItem(hWnd, IDC_LIST1),
0, 0, width, height, TRUE);
return TRUE;
}
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd
)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
HWND hWnd = CreateDialogParamW(hInstance, MAKEINTRESOURCEW(IDD_MAIN), nullptr, &DialogProc, 0);
if (!hWnd)
{
MessageBoxW(nullptr, L"Dialog Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
【问题讨论】:
-
你链接到的页面状态为
Typically, the dialog box procedure should return TRUE if it processed the message, and FALSE if it did not. If the dialog box procedure returns FALSE, the dialog manager performs the default dialog operation in response to the message.你不清楚吗? -
@AlanBirtles,哦,我因为某种原因错过了那个。但是,我还是不明白。已处理和未处理的消息有什么区别?
-
如果您返回
FALSE,那么在大多数情况下,消息会传递给DefDlgProc进行处理(然后可能会将其传递给DefWindowProc)。如果您想为某些消息提供自己的行为而不是接受默认行为,请返回TRUE以告诉对话管理器您处理了该消息。
标签: c++ user-interface winapi