热键显示窗口

 热键显示窗口

方法一:   
  http://support.microsoft.com/?kbid=222829   
  1   插入一个新的Accelerator到资源里,把加速键和对应的响应控件(如一个按钮)关联   
  2   在对话框头文件中声明:   
      HACCEL   m_hAccel;   
  3   在对话框的构造函数里初始化m_hAccel   
      m_hAccel   =   ::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));   
  4   然后重载对话框的PreTranslateMessage函数,在   
  BOOL   CAboutDlg::PreTranslateMessage(MSG*   pMsg)     
  {   
        if   (m_hAccelTable)     
        {   
              if   (::TranslateAccelerator(m_hWnd,   m_hAccelTable,   pMsg))     
              {   
                    return(TRUE);   
              }   
        }   
        return   CDialog::PreTranslateMessage(pMsg);   
  }   
  方法二:   
  1       声明热键消息处理函数原型           
    在.h中消息映射声明处(AFX_mSG字样之后)加入如下语句:           
    LRESULT     OnHotKey(WPARAM     wParam,LPARAM     lParam);           
  2.     消息与相应处理函数相关联           
    在.Cpp中加入消息映射宏,使消息与相应处理函数发生关系,       
                ON_MESSAGE(WM_HOTKEY,OnHotKey);           
  3.     为方便以后的操作     
          预先在类中创建一个响应WM_CREATE和WM_DESTROY消息的函数       
                OnCreate()与OnDestroy()的框架   
  4.向系统登记热键           
    在OnCreate()函数中加入如下代码以向系统登记热键,本例子的热键设为       
                Ctrl+Shift+A.           
    RegisterHotKey(m_hWnd,1001,MOD_CONTROL|MOD_SHIFT,'A');           
    RegisterHotKey(m_hWnd,1002,MOD_CONTROL|MOD_SHIFT,'a');           
    5.处理热键           
    在消息处理函数OnHotKey()中对热键进行处理,并可加入用户希望运行的程序代码       
    LRESULT   C****::OnHotKey(WPARAM     wParam,LPARAM     lParam)           
    if(wParam==1001||wParam==1002)           
    CWnd::SetForegroundWindow();//使得被激活窗口出现在前景           
    MessageBox("Hello!");           
                   //用户可在此添加代码           
    return     0;           
    6.程序运行完毕后解除热键           
    在OnDestroy()中通过UnRegisterHotKey()解除热键登记,释放系统资源.           
    UnRegisterHotKey(m_hWnd,1001);           
    UnRegisterHotKey(m_hWnd,1002);           
    7.编译并运行程序

方法三. 如果你用SDK,这儿有个例子   
#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,   NULL,   NULL))   !=   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   mark   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   marks     
                                        //   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;     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值