使用Win32编写已简单窗口程序,主函数使用DialogBox弹出模式对话框作为主窗口界面,怎样给这个对话框添加加速键表功能支持。INT_PTR  __stdcall DlgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
  case(uMsg)
  {
  case WM_INITDIALOG:
    //do something
    return TRUE;
  }
  return TRUE;
}
int  __stdcall WinMain(HINSTANCE hInstance,HINSTANCE /* hPrevInstance */,LPSTR lpCmdLine,int nCmdShow)
{
  HACCEL  hAccel = NULL;  hAccel = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR_XX));
  //How to use this accelerator table on the modal dialog below.
  DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG_XX),NULL,DlgProc);
  return 0;
}

解决方案 »

  1.   

    楼主主窗口WinMain里面
    hAccel = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR_XX));
      //How to use this accelerator table on the modal dialog below.
    DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG_XX),NULL,DlgProc);
    这样应该可以吧~~
    但是要注意再加载资源时,要注意命名~~~
    IDR_ACCELERATOR_XX一定要和加速键资源名称一致~~~
    还有调用DialogBox弹出模式对话框过程也一定要注意IDD_DIALOG_XX加载定义的名称和DialogBox函数参数MAKEINTRESOURCE里面的参数名相同~~
      

  2.   

    这个问题似乎不好解决,微软的建议是:
    1.用一个 WH_MSGFILTER钩子,在钩子函数里调用TranslateAccelerator
    2.用非模式对话框,在主消息循环里调用TranslateAccelerator(做为主窗口,模式和非模式对话框效果是一样的)
    参见
    http://support.microsoft.com/default.aspx?scid=kb;en-us;108936
      

  3.   

    实际上,DialogBox函数本身在设计上有一些缺陷,所以MFC的模式对话框其实是用非模式对话框模拟的
      

  4.   

    楼上的意见正确,只补充一点,如果你加速键表不是很复杂的话,就考虑在DlgProc中通过WM_CHAR自己处理吧.
      

  5.   

    You can use a modaless dialog box as the main window.while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)

        if (bRet == -1)
        {
            // handle the error and possibly exit
        }
        else
        {
            if (hwndDlgModeless == (HWND) NULL || 
                    !IsDialogMessage(hwndDlgModeless, &msg) && 
                    !TranslateAccelerator(hwndMain, haccel, 
                        &msg)) 
            { 
                TranslateMessage(&msg); 
                DispatchMessage(&msg); 
            }
        } 
    }