Is it possible to use accelerators in dialog box? In window-based program I have to use TranslateAccelerator function but how about dialog-based program?
If your dialog box is modeless, you can handle the keystrokes by loading and destroying your accelerators. I have multiple dialogs boxes in an application with a number of different accelerator tables for each one. I simply load and destroy my accelerators as needed.
//
// main.cpp
//
#include <windows.h>
#include "header.h"
HACCEL hacc;
HINSTANCE hinst; //global hinstance of this program
BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
hacc=LoadAccelerators(hinst,MAKEINTRESOURCE(IAC_ACCELERATOR));
returntrue;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDA_DOSOMETHING:
MessageBox(hwnd,"Accelerator works!","OK",64); // This message box must show up when I press 'x' on keyboard
break;
}
break;
default: returnfalse;
}
}
int WinMain(HINSTANCE hThis,HINSTANCE hPrev,LPSTR cmd,int xxx)
{
hinst=hThis;
DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DLG),0,DlgProc);
}
But this IS a dialog box and the message box must show up when I press 'x' on keyboard. As I said, in window-based program I used TranslateAccelerator() function: