如何在无模式对话框中使用 Enter 键?
How to use the Enter key in a modeless dialog?
我一直在为大学做一个 WinAPI 项目,我们被要求在一个对话框中实现完整的程序。起初,我使用了一个模态对话框,一切正常,除了对话框的任务栏中没有图标,因为我直接在 WM_CREATE 上创建它并且没有使主 window反正可见,因为它没有被使用。
现在我完全放弃了主要的 window 句柄,只使用 CreateDialog
创建一个无模式对话框,但从那以后我不能使用 Enter 键替代我的默认按钮。
case WM_COMMAND:
if(LOWORD(wparam) == IDOK || LOWORD(wparam) == IDC_OK) {
[...] //doing stuff
}
break;
这是我的全部主要功能:
int WINAPI WinMain(HINSTANCE dieseInstanz, HINSTANCE vorherigeInstanz, LPSTR lpszArgument, int Fensterstil) {
MSG Meldung;
HWND dialog = NULL;
dialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDC_DIALOG), NULL, dialogHandler);
if(dialog != NULL) {
ShowWindow(dialog, SW_SHOW);
} else {
MessageBox(NULL, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
}
while(GetMessage(&Meldung, NULL, 0, 0)) {
TranslateMessage(&Meldung);
DispatchMessage(&Meldung);
}
return Meldung.wParam;
}
我是不是做错了一些基本的事情,或者它没有按照我想要的方式工作?
澄清一下:如果我在对话框中按 Enter 键,我只会听到典型的 Windows 通知声音。
您的消息循环需要包含对 IsDialogMessage()
:
的调用
Determines whether a message is intended for the specified dialog box and, if it is, processes the message.
...
When IsDialogMessage
processes a message, it checks for keyboard messages and converts them into selections for the corresponding dialog box. For example, the TAB key, when pressed, selects the next control or group of controls, and the DOWN ARROW key, when pressed, selects the next control in a group.
Because the IsDialogMessage
function performs all necessary translating and dispatching of messages, a message processed by IsDialogMessage
must not be passed to the TranslateMessage
or DispatchMessage
function.
根据 Using Dialog Boxes: Creating a Modeless Dialog Box:
The second part of the example is the application's main message loop. The loop includes the IsDialogMessage function to ensure that the user can use the dialog box keyboard interface in this modeless dialog box.
CreateDialog()
文档中也有说明:
After CreateDialog
returns, the application displays the dialog box (if it is not already displayed) by using the ShowWindow
function. The application destroys the dialog box by using the DestroyWindow
function. To support keyboard navigation and other dialog box functionality, the message loop for the dialog box must call the IsDialogMessage
function.
因此,将您的消息循环更改为看起来更像这样:
while (GetMessage(&Meldung, NULL, 0, 0)) {
if (!IsWindow(dialog) || !IsDialogMessage(dialog, &Meldung)) {
TranslateMessage(&Meldung);
DispatchMessage(&Meldung);
}
}
我一直在为大学做一个 WinAPI 项目,我们被要求在一个对话框中实现完整的程序。起初,我使用了一个模态对话框,一切正常,除了对话框的任务栏中没有图标,因为我直接在 WM_CREATE 上创建它并且没有使主 window反正可见,因为它没有被使用。
现在我完全放弃了主要的 window 句柄,只使用 CreateDialog
创建一个无模式对话框,但从那以后我不能使用 Enter 键替代我的默认按钮。
case WM_COMMAND:
if(LOWORD(wparam) == IDOK || LOWORD(wparam) == IDC_OK) {
[...] //doing stuff
}
break;
这是我的全部主要功能:
int WINAPI WinMain(HINSTANCE dieseInstanz, HINSTANCE vorherigeInstanz, LPSTR lpszArgument, int Fensterstil) {
MSG Meldung;
HWND dialog = NULL;
dialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDC_DIALOG), NULL, dialogHandler);
if(dialog != NULL) {
ShowWindow(dialog, SW_SHOW);
} else {
MessageBox(NULL, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
}
while(GetMessage(&Meldung, NULL, 0, 0)) {
TranslateMessage(&Meldung);
DispatchMessage(&Meldung);
}
return Meldung.wParam;
}
我是不是做错了一些基本的事情,或者它没有按照我想要的方式工作?
澄清一下:如果我在对话框中按 Enter 键,我只会听到典型的 Windows 通知声音。
您的消息循环需要包含对 IsDialogMessage()
:
Determines whether a message is intended for the specified dialog box and, if it is, processes the message.
...
When
IsDialogMessage
processes a message, it checks for keyboard messages and converts them into selections for the corresponding dialog box. For example, the TAB key, when pressed, selects the next control or group of controls, and the DOWN ARROW key, when pressed, selects the next control in a group.Because the
IsDialogMessage
function performs all necessary translating and dispatching of messages, a message processed byIsDialogMessage
must not be passed to theTranslateMessage
orDispatchMessage
function.
根据 Using Dialog Boxes: Creating a Modeless Dialog Box:
The second part of the example is the application's main message loop. The loop includes the IsDialogMessage function to ensure that the user can use the dialog box keyboard interface in this modeless dialog box.
CreateDialog()
文档中也有说明:
After
CreateDialog
returns, the application displays the dialog box (if it is not already displayed) by using theShowWindow
function. The application destroys the dialog box by using theDestroyWindow
function. To support keyboard navigation and other dialog box functionality, the message loop for the dialog box must call theIsDialogMessage
function.
因此,将您的消息循环更改为看起来更像这样:
while (GetMessage(&Meldung, NULL, 0, 0)) {
if (!IsWindow(dialog) || !IsDialogMessage(dialog, &Meldung)) {
TranslateMessage(&Meldung);
DispatchMessage(&Meldung);
}
}