Accelerators in dialogs

This is a discussion on Accelerators in dialogs within the Windows Programming forums, part of the Platform Specific Boards category; I'm working on a program which uses CreateWindow() to create a menu bar window at the top of the screen, ...

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    Accelerators in dialogs

    I'm working on a program which uses CreateWindow() to create a menu bar window at the top of the screen, and then calls CreateDialog() to create several modeless dialogs depending on what menu options the user chooses, etc. In the menu, the File->Save item has an id of ID_FILE_SAVE. I defined an accelerator with the same ID for Ctrl-S. The accelerator is loaded in WinMain before the start of the message loop with
    Code:
    HACCEL hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SWITCHCONF);
    This accelerator works when the menu bar window has the focus, but when the focus is on the dialog, it doesn't. My message loop looks like this:

    Code:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
    	if (!TranslateAccelerator(hwnd, hAccelTable, &msg)
    		&&!TranslateAccelerator(hVLDialog, hAccelTable, &msg)) 
    	{
    		if (	!IsDialogMessage(hVLDialog,&msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    }
    I tried handling the WM_COMMAND message in the message handler for the dialog:
    Code:
    BOOL CALLBACK VLDialog(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case ID_FILE_SAVE:
    				{
    					/*SendMessage(hMainWindow,
                                                        WM_COMMAND,
                                                        MAKEWPARAM(ID_FILE_SAVE,0),
                                                        MAKELPARAM(0,0));*/
    					MessageBox(hwnd,"ID_FILE_SAVE","SAVE",MB_OK);
    					break;
    				}
    ...
    but it seems that the message is never received. What am I doing incorrectly?
    Last edited by confuted; 06-24-2005 at 04:01 PM.
    Away.

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    Can we see how did you create the menu bar?

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Quote Originally Posted by Joelito
    Can we see how did you create the menu bar?
    Sure. This code was generated by the MSVC.NET wizard:
    Code:
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	MSG msg;
    	HACCEL hAccelTable;
    
    	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_SWITCHCONF, szWindowClass, MAX_LOADSTRING);
    	MyRegisterClass(hInstance);
    
    	if (!InitInstance (hInstance, nCmdShow)) 
    	{
    		return FALSE;
    	}
    	
    	InitCommonControls();
    ...
    Code:
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX); 
    
    	wcex.style		= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= (WNDPROC)WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon		= (HICON)LoadImage(GetModuleHandle(NULL),
                                      MAKEINTRESOURCE(IDI_SWITCHCONF32), 
                                      IMAGE_ICON, 32, 32, 0);
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= (LPCTSTR)IDC_SWITCHCONF;
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= (HICON)LoadImage(GetModuleHandle(NULL),
                                      MAKEINTRESOURCE(IDI_SWITCHCONF16), 
                                      IMAGE_ICON, 16, 16, 0);
    
    	return RegisterClassEx(&wcex);
    }
    
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
       hInst = hInstance;
    
       hWnd = CreateWindow(szWindowClass, szTitle,
                       WS_MINIMIZEBOX
                       |WS_MAXIMIZEBOX|WS_MAXIMIZE
                       |WS_SYSMENU|WS_BORDER,
                       0, 0, 1282, 51, NULL, NULL, 
                       hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
       hMainWindow=hWnd;
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    The name of the accelerator table is IDC_SWITCHCONF; the menu has the same name.
    Last edited by confuted; 06-25-2005 at 05:12 AM.
    Away.

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, 03:36 PM
  2. I need to use dialogs as tabs?
    By BenPage in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2005, 09:59 AM
  3. Replies: 1
    Last Post: 11-20-2004, 03:02 AM
  4. help with keyboard accelerators
    By nnormal in forum Windows Programming
    Replies: 1
    Last Post: 08-06-2004, 12:22 PM
  5. Modeless Dialogs in Multiple threads
    By MrGrieves in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2004, 02:33 PM

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