Dialogs

This is a discussion on Dialogs within the Windows Programming forums, part of the Platform Specific Boards category; I am using a dialog and want to use an keyboard accelerator with the dialog. I was wondering if this ...

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    Dialogs

    I am using a dialog and want to use an keyboard accelerator with the dialog. I was wondering if this was possible. I know how to do it with a regular window in the message loop (by using TranslateAccelerator), but I do not know how to do it with a dialog.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Assuming a modeless dialog - exactly the same as a normal window. However you are confronted with a few choices. If you want default dialog processing for things like TAB and ESC keys then you still need to use IsDialogMessage or, alternatively, you can re-invent the wheel in your accelerator table.

    The second choice you have is which you give precedence to ie TranslateAccelerator or IsDialogMessage. I prefer to give the custom accelerator the first go eg:
    Code:
    while (GetMessage(&Msg,NULL,0,0)>0)
        {
        //is msg from a resource defined keyboard accelerator key?
        if (!TranslateAccelerator(hwnd,hAccelTable,&Msg))
            {
            //check for default dlg keyboard keys eg TAB, ESC
            if ((!IsWindow(hwnd))||(!IsDialogMessage(hwnd,&Msg)))
                {
                //translate and dispatch other messages
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
                }
             }
        }
    Where hwnd is the handle of your modeless dialog, hAccelTable is the handle to your accelerator table and Msg is the MSG struct.

    Hope that helps.

    edit: bored so made code pretty.
    Last edited by Ken Fitlike; 08-31-2002 at 08:27 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    When I create my dialog I am using the function DialogBox. How can I use the message loop you gave with the DialogBox function?

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by sean345
    When I create my dialog I am using the function DialogBox. How can I use the message loop you gave with the DialogBox function?

    - Sean
    Good question!......never really thought about that! You got me to pull out the compiler and start messing....

    I tried to install a hook to read the MSG before the GetMessage() of your dialog laid its hands on it .....

    After some messing I came up with this......seems to woke AOK

    Code:
    #include <windows.h>
    #include "resource.h"
    
    HHOOK g_hHook;//evils of globals I know...
    HACCEL g_hAcc;
    HWND g_hWnd;
    
    
    BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,
    						 LPARAM lParam){
    	switch(uMsg){
    	case WM_INITDIALOG:
    		g_hWnd = hwndDlg;//Set Global hwnd
    		return 1;
    	case WM_CLOSE:
    		EndDialog(hwndDlg,0);
    		return 1;
    	case WM_COMMAND:
    		if(LOWORD(wParam)==ID_F1){//catch F1 Key
    			MessageBox(hwndDlg,"F1 Pressed",NULL,MB_OK);
    			return 1;
    		}
    		else break;
    	}
    
    	return 0;
    }
     
    LRESULT CALLBACK GetMsgProc(int code,WPARAM wParam,
    							LPARAM lParam){
    
    	TranslateAccelerator(g_hWnd,g_hAcc,(MSG*)lParam); //Here's the func!
    
    	return CallNextHookEx(g_hHook,code,wParam,lParam);
    }
     
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int){	
    	
    	g_hAcc = LoadAccelerators(hInst,
    		MAKEINTRESOURCE(IDR_ACCELERATOR1));
    	if(!g_hAcc)return 1;
    
    	g_hHook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,
    		hInst,GetCurrentThreadId());
    	if(!g_hHook)return 1;
     
    	DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG1),
    		HWND_DESKTOP,DialogProc);
    
    	UnhookWindowsHookEx(g_hHook);
    
    	return 0;
    }
    All that does is add a call to the hook procdecure before getting on with the normal business....

    There's probably an easier more straight forward way though.....but the above works ok......

  5. #5
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Ok. That works. Thanks for your help.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ok..Tabbed dialogs...XP style
    By Joelito in forum Windows Programming
    Replies: 5
    Last Post: 05-14-2006, 02:36 PM
  2. I need to use dialogs as tabs?
    By BenPage in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2005, 08:59 AM
  3. Modeless Dialogs in Multiple threads
    By MrGrieves in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2004, 01:33 PM
  4. dialogs are easier, but what about the main window?
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 01-21-2003, 12:42 PM
  5. Common Dialogs
    By DominicTrix in forum Windows Programming
    Replies: 3
    Last Post: 12-23-2002, 04:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21