让非模态对话框处理TAB按键事件的三种方法

让非模态对话框处理TAB按键事件的三种方法

作者:Oscar Cai
时间:2005-9-17 20:17

对话框(Dialog)分为模态对话框和非模态对话框(Modeless Dialog)。非模态对话框不能处理TAB键、快捷键等按键事件,也就是说用户不能在非模态对话框中通过按TAB键切换各控件之间的焦点(Focus)。但这也不是没有解决办法的。

方法一

在MSDN中,让非模态对话框处理TAB按键事件的经典代码如下:

HINSTANCE hinst;
HWND hwndMain;
HWND hwndDlgModeless = NULL;


 PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine,  nCmdShow)
{
    MSG msg;
    BOOL bRet;
    WNDCLASS wc;
    HACCEL haccel;
    UNREFERENCED_PARAMETER(lpszCmdLine);

    

     (!hPrevInstance)
    {
        wc.style = 0;
        wc.lpfnWndProc = (WNDPROC) WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
        wc.hbrBackground = GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName =  "MainMenu";
        wc.lpszClassName = "MainWndClass";

         (!RegisterClass(&wc))
             FALSE;
    }

    hinst = hInstance;  

    hwndMain = CreateWindow("MainWndClass", "Sample",
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL,
        (HMENU) NULL, hinst, (LPVOID) NULL);

     (!hwndMain)
         FALSE;

    ShowWindow(hwndMain, nCmdShow);
    UpdateWindow(hwndMain);

    ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    {
         (bRet == -1)
        {
            
        }
        
        {
             (hwndDlgModeless == (HWND) NULL ||
                    !IsDialogMessage(hwndDlgModeless, &msg) &&
                    !TranslateAccelerator(hwndMain, haccel, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }

     msg.wParam;
}

其主要解决方案是让非模态对话框hwndDlgModeless的父窗口hwndMain,在其主消息循环中通过调用IsDialogMessage函数来通知hwndDlgModeless TAB按键事件。可是,如果hwndMain没有明确写出的主消息循环,由该怎么办呢?

方法二

譬如,当hwndMain指向的是一个模态对话框时,其WinMain函数通常实现如下:

 
 
 
 
 BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);


 APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine,  nCmdShow)
{
    WNDCLASS wc;
    INITCOMMONCONTROLSEX cc;

    memset(&wc,0,(wc));
    wc.lpfnWndProc = DefDlgProc;
    wc.cbWndExtra = DLGWINDOWEXTRA;
    wc.hInstance = hinst;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName = "MainWndClass";
    RegisterClass(&wc);
    memset(&cc,0,(cc));
    cc.dwSize = (cc);
    cc.dwICC = 0xffffffff;
    InitCommonControlsEx(&cc);

     DialogBox(hinst, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, (DLGPROC) DialogFunc);

}

  InitializeApp(HWND hDlg,WPARAM wParam, LPARAM lParam)
{
     1;
}

 BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
     (msg) {
     WM_INITDIALOG:
        InitializeApp(hwndDlg,wParam,lParam);
         TRUE;
     WM_COMMAND:
         (LOWORD(wParam)) {
         IDOK:
            EndDialog(hwndDlg,1);
             1;
         IDCANCEL:
            EndDialog(hwndDlg,0);
             1;
        }
        ;
    
     WM_CLOSE:
        EndDialog(hwndDlg,0);
         TRUE;

    }
     FALSE;
}

资源文件中,IDD_MAINDIALOG定义的对话框只有标题栏、默认的OK(IDOK)键和Cancel(IDCANCEL)键。对话框的主消息循环在系统API DialogBox中,我们无法动其分毫。于是,只有修改WinMain函数,还得添加三个全局变量:

HINSTANCE hinst;
HWND hwndMain;
HWND hwndDlgModeless = NULL;

 APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine,  nCmdShow)
{
    WNDCLASS wc;
    INITCOMMONCONTROLSEX cc;
    MSG msg;
     res;

    memset(&wc,0,(wc));
    wc.lpfnWndProc = DefDlgProc;
    wc.cbWndExtra = DLGWINDOWEXTRA;
    wc.hInstance = hinst;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName = "MainWndClass";
    RegisterClass(&wc);
    memset(&cc,0,(cc));
    cc.dwSize = (cc);
    cc.dwICC = 0xffffffff;
    InitCommonControlsEx(&cc);

    hInst = hinst;

    hMainDlg = CreateDialog(hinst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC) DialogFunc);

    ( (res = ()GetMessage( &msg, NULL, 0, 0 )) != 0)
    {
         (res == -1)
        {
        }
        
        {
            (hMainDlg == (HWND) NULL ||
                    !IsDialogMessage(hMainDlg, &msg) ||
                    hModelessDlg == (HWND) NULL ||
                    !IsDialogMessage(hModelessDlg, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }

    (hModelessDlg != NULL) {
        DestroyWindow(hModelessDlg);
        hModelessDlg = NULL;
    }

    (hMainDlg != NULL) {
        DestroyWindow(hMainDlg);
        hMainDlg = NULL;
    }

     msg.wParam;
}

因为将父窗口hwndMain创建为非模态对话框,所以在其主消息循环处除了调用IsDialogMessage函数处理非模态对话框hwndDlgModeless的消息外,还要调用该函数处理hwndMain的消息。另外要特别注意的是,调用DestroyWindow函数销毁对话框hModelessDlghwndMain,只能在退出主消息循环之后进行,千万不能放到对话框过程函数DialogFunc中对消息WM_CLOSEWM_QUIT的处理模块中,否则会引起整个进程陷在主消息循环里,无法退出。在处理消息WM_CLOSE时,只需调用函数PostQuitMessage即可。消息WM_QUIT就不用处理了。以下是让非模态对话框处理TAB按键事件全部代码(除去注释):

 
 
 
 


 HWND hModelessDlg = NULL;
 HWND hMainDlg = NULL;
 HINSTANCE hInst = NULL;

 ErrMsg(s)   MessageBox(NULL, (s), "Caution", MB_OK)

 BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);

 APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine,  nCmdShow)
{
    WNDCLASS wc;
    INITCOMMONCONTROLSEX cc;
    MSG msg;
     res;

    memset(&wc,0,(wc));
    wc.lpfnWndProc = DefDlgProc;
    wc.cbWndExtra = DLGWINDOWEXTRA;
    wc.hInstance = hinst;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName = "MainWndClass";
    RegisterClass(&wc);
    memset(&cc,0,(cc));
    cc.dwSize = (cc);
    cc.dwICC = 0xffffffff;
    InitCommonControlsEx(&cc);

    hInst = hinst;

    hMainDlg = CreateDialog(hinst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC) DialogFunc);

    ( (res = ()GetMessage( &msg, NULL, 0, 0 )) != 0)
    {
         (res == -1)
        {
            // handle the error and possibly exit
        }
        
        {
            (hMainDlg == (HWND) NULL ||
                    !IsDialogMessage(hMainDlg, &msg) ||
                    hModelessDlg == (HWND) NULL ||
                    !IsDialogMessage(hModelessDlg, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }

    (hModelessDlg != NULL) {
        DestroyWindow(hModelessDlg);
        hModelessDlg = NULL;
    }

    (hMainDlg != NULL) {
        DestroyWindow(hMainDlg);
        hMainDlg = NULL;
    }

     msg.wParam;
}

  InitializeApp(HWND hDlg,WPARAM wParam, LPARAM lParam)
{
    hMainDlg = hDlg;
    hModelessDlg = CreateDialog(hInst, MAKEINTRESOURCE(DLG_STEP1), hDlg, NULL);
     1;
}

 BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
     i;

     (msg) {

     WM_INITDIALOG:
        InitializeApp(hwndDlg,wParam,lParam);
         TRUE;

     WM_COMMAND:
         (LOWORD(wParam)) {
         IDOK:
         IDCANCEL:
            PostQuitMessage(0);
            ;
        }
        ;

     WM_CLOSE:
        PostQuitMessage(0);
        ;
    }

     FALSE;
}

方法三

还可以用函数FindResourceLoadResourceLockResourceUnlockResourceFreeResource来加载和释放对话框资源,用函数CreateDialogIndirect创建非模态父对话框hwndMain。要使用这种方法的话,大部分代码都相同或类似,但查找、加载、锁定、释放对话框资源这些代码实现起来颇为麻烦,还不如直接调用函数CreateDialog来得方便、简洁。这里就不详细说明了。

你可能感兴趣的:(LccWin32)