Establishment of the hot key...

类别:VC语言 点击:0 评论:0 推荐:

     WM_HOTKEY并不包含在Class Wizard中,如果要设置这个消息,必须手工添加。假设Project的名字是Temp,那么操作如下:
    1、在TempView.h文件中消息映射的说明处加入热键消息处理函数的声明。
           //{{AFX_MSG(CTempView)
           afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
           afx_msg void OnDestroy();
            afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
            afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
            afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
            afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
            afx_msg void OnKillFocus(CWnd* pNewWnd);
            afx_msg void OnMouseMove(UINT nFlags, CPoint point);
            afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
            //}}AFX_MSG
            LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);//此句为加入内容,声明热键消息处理函数。
            DECLARE_MESSAGE_MAP()

           2、 接着在TempView.cpp中消息映射的定义处是用消息映射宏指定热键消息WM_HOTKEY的消息处理函数。
            BEGIN_MESSAGE_MAP(CTempView, CView)
             //{{AFX_MSG_MAP(CTempView)
             ON_WM_CHAR()
             ON_WM_DESTROY()
             ON_WM_CREATE()
             ON_WM_KEYDOWN()
             ON_WM_KEYUP()
             ON_WM_HSCROLL()
             ON_WM_KILLFOCUS()
             ON_WM_MOUSEMOVE()
             ON_WM_LBUTTONUP()
             //}}AFX_MSG_MAP
            ON_MESSAGE(WM_HOTKEY,OnHotKey) //此句为手工加入内容,指定了热键消息的处理函数。
            END_MESSAGE_MAP()
           3、在 OnCreate()函数中登记热键[*]。
           int CTempView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
           {
            if (CView::OnCreate(lpCreateStruct) == -1)
             return -1;
 
            // TODO: Add your specialized creation code here
           ::RegisterHotKey(m_hWnd,1001,MOD_CONTROL|MOD_ALT,'*');//登记热键
            return 0;
           }
           4、在TempView.cpp中编写OnHotKey()函数,当按下热键[*]时程序变为前台,并跳出一个消息框。
            LRESULT CTempView::OnHotKey(WPARAM wParam,LPARAM lParam)
            {
                if(wParam==1001)
                    MessageBox("I have been activated!!");
                return 0;
            }
            5、注销热键,释放系统资源.
            void CTempView::OnDestroy() 
            {
             CView::OnDestroy();
 
             // TODO: Add your message handler code here
             UnregisterHotKey(m_hWnd,1001);//注销热键,释放系统资源.

            }

本文地址:http://com.8s8s.com/it/it541.htm