我想用加速键在我对话框上实现按下CTRL+G就执行一个函数,我的做法如下:(1)在资源中Accelerator加入ID:
      ID               KEY        TYPE
ID_MY_ACCELERATOR     CTRL+G     VIRTKEY(2)在mydlg.h中的public中声明:
    HACCEL hAccel; (3)在mydlg.cpp的OnInitDialog()中初始化:
   hAccel = LoadAccelerators(((CMyApp*)AfxGetApp())->m_hInstance,MAKEINTRESOURCE(ID_MY_ACCELERATOR));(4)在PreTranslateMessage函数中重定义
  BOOL CORNOTDlg::PreTranslateMessage(MSG* pMsg) 
 {
    if (TranslateAccelerator(GetSafeHwnd(),hAccel,pMsg))
        return TRUE;
    else
        return CDialog::PreTranslateMessage(pMsg);
 }
首先在第三步初始化出错,得到的hAccel值为0x00000000;
第二然后怎样跟我在dlg中写的函数displaytime()关联起来?让我在按下CTRL+G后能执行这个函数?

解决方案 »

  1.   

    BOOL RegisterHotKey( 
    HWND hWnd, 
    int id, 
    UINT fsModifiers, 
    UINT vk );
      

  2.   

    将ID_MY_ACCELERATOR改为“MY_ACCELERATOR”,然后
    m_hHotKey = LoadAccelerators( AfxGetInstanceHandle(), "MY_ACCELERATOR" ); 在PreTranslateMessage(MSG* pMsg) 中
    {
    // TODO: Add your specialized code here and/or call the base class
    if( m_hHotKey != NULL && TranslateAccelerator( m_hWnd, m_hHotKey, pMsg ) )
    {
    return TRUE;
    }
    return CDialog::PreTranslateMessage(pMsg);
    }
      

  3.   

    Creating Accelerators for Font Attributes
    The example in this section shows how to perform the following tasks: Create an accelerator-table resource. 
    Load the accelerator table at run time. 
    Translate accelerators in a message loop. 
    Process WM_COMMAND messages generated by the accelerators. 
    These tasks are demonstrated in the context of an application that includes a Character menu and corresponding accelerators that allow the user to select attributes of the current font. The following portion of a resource-definition file defines the Character menu and the associated accelerator table. Note that the menu items show the accelerator keystrokes and that each accelerator has the same identifier as its associated menu item. #include <windows.h> 
    #include "acc.h" 
     
    MainMenu MENU 

        POPUP   "&Character" 
        { 
            MENUITEM    "&Regular\tF5",         IDM_REGULAR 
            MENUITEM    "&Bold\tCtrl+B",        IDM_BOLD 
            MENUITEM    "&Italic\tCtrl+I",      IDM_ITALIC 
            MENUITEM    "&Underline\tCtrl+U",   IDM_ULINE 
        }

     
    FontAccel ACCELERATORS 

        VK_F5,  IDM_REGULAR,    VIRTKEY 
        "B",    IDM_BOLD,       CONTROL, VIRTKEY 
        "I",    IDM_ITALIC,     CONTROL, VIRTKEY 
        "U",    IDM_ULINE,      CONTROL, VIRTKEY 
    } The following sections from the application's source file show how to implement the accelerators. HWND hwndMain;      // handle to main window 
    HANDLE hinstAcc;    // handle to application instance 
    int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow) 

        MSG msg;            // application messages 
        BOOL bRet;          // for return value of GetMessage
        HACCEL haccel;      // handle to accelerator table 
     
        // Perform the initialization procedure. 
     
        // Create a main window for this application instance. 
     
        hwndMain = CreateWindowEx(0L, "MainWindowClass", 
            "Sample Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, 
            hinst, NULL ); 
     
        // If a window cannot be created, return "failure." 
     
        if (!hwndMain) 
            return FALSE; 
     
        // Make the window visible and update its client area. 
     
        ShowWindow(hwndMain, nCmdShow); 
        UpdateWindow(hwndMain); 
     
        // Load the accelerator table. 
     
        haccel = LoadAccelerators(hinstAcc, "FontAccel"); 
        if (haccel == NULL) 
            HandleAccelErr(ERR_LOADING);     // application defined 
     
        // Get and dispatch messages until a WM_QUIT message is 
        // received. 
     
        while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
        {
            if (bRet == -1)
            {
                // handle the error and possibly exit
            }
            else
            { 
                // Check for accelerator keystrokes. 
         
                if (!TranslateAccelerator( 
                        hwndMain,  // handle to receiving window 
                        haccel,    // handle to active accelerator table 
                        &msg))         // message data 
                {
                    TranslateMessage(&msg); 
                    DispatchMessage(&msg); 
                } 
            } 
        }
        return msg.wParam; 

     
    LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam) 

        BYTE fbFontAttrib;        // array of font-attribute flags 
        static HMENU hmenu;       // handle to main menu 
     
        switch (uMsg) 
        { 
            case WM_CREATE: 
     
                // Add a check  to the Regular menu item to 
                // indicate that it is the default. 
     
                hmenu = GetMenu(hwndMain); 
                CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND | 
                    MF_CHECKED); 
                return 0; 
     
            case WM_COMMAND: 
                switch (LOWORD(wParam)) 
                { 
                    // Process the accelerator and menu commands. 
     
                    case IDM_REGULAR: 
                    case IDM_BOLD: 
                    case IDM_ITALIC: 
                    case IDM_ULINE: 
     
                        // GetFontAttributes is an application-defined 
                        // function that sets the menu-item check s 
                        // and returns the user-selected font attributes. 
     
                        fbFontAttrib = GetFontAttributes( 
                            (BYTE) LOWORD(wParam), hmenu); 
     
                        // SetFontAttributes is an application-defined 
                        // function that creates a font with the 
                        // user-specified attributes the font with 
                        // the main window's device context. 
     
                        SetFontAttributes(fbFontAttrib); 
                        break; 
     
                    default: 
                        break; 
                } 
                break; 
     
                // Process other messages. 
     
            default: 
                return DefWindowProc(hwndMain, uMsg, wParam, lParam); 
        } 
        return NULL; 
    }
      

  4.   

    在对话框初始化中
    m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));
    在PreTranslateMessage(MSG* pMsg)中 
    {
    if(m_hAccelTable)
    {
    if(::TranslateAccelerator(m_hWnd,m_hAccelTable,pMsg))
    return true; 
    } return CDialog::PreTranslateMessage(pMsg);
    }
      

  5.   

    taoni(taoni):在哪里关联函数displaytime()?
      

  6.   

    还是不能白,谁能告知完整步骤?
    用块速键实现CTRL+G执行自定义函数displaytime()
      

  7.   

    复杂的我不会,不过你可以考虑把你的displaytime()函数的实现放在菜单里
    ,这样只需要把菜单的Caption设定为:显示时间(&G)\tCtrl+G
    就可以实现你的目的了。
    对于怎么添加菜单的处理函数,很简单我就不说了。上面是我的一点建议,希望能对你有所帮助。