回 帖 发 新 帖 刷新版面

主题:win32程序中注册热键问题 ?急……

我想在我的win32程序中使用热键来达到隐藏窗体和显示窗体的目的。搜索以前的帖子看到一篇BCB下的用法:
 //头文件中   
  ......   
  private: //   User   declarations   
                  void   __fastcall   ReceiveMessage(TMessage   &MyMsg);   
                  BEGIN_MESSAGE_MAP   
                      MESSAGE_HANDLER(WM_HOTKEY,   TMessage,   ReceiveMessage)   
                  END_MESSAGE_MAP(TForm)   
  ......   
  //单元文件中   
  ......   
  void   __fastcall   TForm1::FormCreate(TObject   *Sender)   
  {   
      int HotKeyId   =   GlobalAddAtom("MyHotKey")   -   0xC000;   
      RegisterHotKey(Handle,   HotKeyId,   MOD_CONTROL,   0x41);   
  }   
  ......   
  void   __fastcall   TForm1::ReceiveMessage(TMessage   &MyMsg)   
  {   
      if((MyMsg.LParamLo   ==   MOD_CONTROL)   &&   (MyMsg.LParamHi   ==   0x41))   
      {   
          //按下Ctrl+A将执行的代码   
      }   
      TForm::Dispatch(&MyMsg);   
  }   
但是我将它复制到我的程序后发现没有作用。然后用MessageBox函数看了下是否执行到了FormCreate函数,发现程序就没有执行到这里。我的问题:

1、如何修改程序使之能执行FormCreate函数?

2、我查了下MSDN中关于RegisterHotKey的用法,发现只要HotKeyId是个常数即可,但是为什么还要int HotKeyId   =   GlobalAddAtom("MyHotKey")   -   0xC000; 一下呢?(查询了N多有关RegisterHotKey的例子,发现里面都有用到这种方法)

3、RegisterHotKey函数中的第一个参数Handle之前并没有定义,可是为什么还能正常编译过去呢?

4、窗体的隐藏和显示是否可以直接这样:
TForm1->DOHIDE();//hide window
TForm1->DOSHOW();//show window?

5、如果该程序要用vc6。0编译,请问应该如何修改?

小弟初学win32编程,还请大家帮忙,最好能符完整代码,也好让我认识的更清楚。谢谢!

回复列表 (共3个回复)

沙发

lz的第三个问题:Handle估计是TForm1 class的成员,因为我没用BCB,所以不大清楚。但mfc中的情况跟这里相似。所以我觉得 Handle is 类成员,当TForm1调用constructor function的时候,生成一个窗口并把它的句柄存到私有成员变量Handle中去了。lz可以看看我下面关于RegisterHotKey的注释。

以下是vc代码:
#include <afxwin.h>

class CSimpleApp : public CWinApp
{
    BOOL InitInstance();
};

class CSimpleWnd : public CFrameWnd
{
    int m_nIDHotKey;
public:
    CSimpleWnd();
    LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);
    ~CSimpleWnd();

    DECLARE_MESSAGE_MAP()
};

BOOL CSimpleApp::InitInstance()
{
    m_pMainWnd=new CSimpleWnd();
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

CSimpleWnd::CSimpleWnd()
{
    Create(NULL,"Window Application");

    // Get an atom with string "MyHotKey". In fact , an atom is just an ID corresponds with
    // a string. the system maintain the relationship in the atom table.

    //We should use atom because it's unique and will not confilct with other ID which other
    //application may use while you are trying the use that ID to register hotkey event.
   //GlobalAddAtom return number in the range between 0xc000 and 0xffff,

    m_nIDHotKey   =   GlobalAddAtom("MyHotKey"); 
    
    //Tell System to put a message in our message queue when there is hotkeys pressed;
    //You can register multiple hotkeys for your windows
    RegisterHotKey(m_hWnd,   m_nIDHotKey,   MOD_CONTROL,   0x41);
    //m_hWnd is private member of CFrameWnd,it's initialized by it's constructor function with 
    //the handle to the window created actually with WINAPI FUCTION CreateWindowEx();
    //Therefore we can use it safely.
}

LRESULT CSimpleWnd::OnHotKey(WPARAM wParam,LPARAM lParam)
{
    // below determine what the hotkey is like, if you have register more than one hotkeys
    // In fact you can use m_nIDHotKey hotkey identifier which we save before we register it,
    // to compare against wParam which is passed by the system and hold the value of the hotkey ID
    if((   LOWORD(lParam) ==  MOD_CONTROL)   &&   ( HIWORD(lParam)   ==   0x41))   
      {   
        MessageBox("You press CTL+A","Hot Key MessageBox",MB_OK);
      }   

    return 0;
}

CSimpleWnd::~CSimpleWnd()
{
    //Unregister hotkey
    UnregisterHotKey(m_hWnd,m_nIDHotKey);
}

BEGIN_MESSAGE_MAP(CSimpleWnd,CFrameWnd)

    //Keep an eye on the message queue for WM_HOTKEY message,
    //There there is any,call our own function to deal with it.
    ON_MESSAGE(WM_HOTKEY,OnHotKey)    

END_MESSAGE_MAP()

//Run the application
CSimpleApp App;

板凳

寻找一直寻找机会的人!
一个趋势中的趋势的项目!
一个直销和传销终结者的项目!
一个帮助普通创业者成功的项目!
   我们在互联网上正在拓展一个生意项目,现在正在寻求合作伙伴。我们认为成功的关键是你是什么人?还有你和谁在一起。我们在乎的是--您是否是有激情、有梦想的人。有意者请跟我们联系,我们和您谈谈,给您讲解一些生意概念,或许我们可以合作。一个集即时网游、交友,娱乐游戏,网络品牌代理商城,互联网平台项目招商进行中……详情QQ空间资料 http://user.qzone.qq.com/909065437/infocenter
网商咨询 摩客天宇QQ 909065437 

3 楼

最新互助式项目。C易特元亨QQ919169870

这是一个互助的模式。
不推广人也有钱赚的。分A B两个网。
不推广的人在A网。推广的人 在B网。
B网推广的人帮助A网。
A网赚取6000元出局。
B网就多了。
详情了解加QQ919169870 
  http://blog.sina.com.cn/s/blog_6b19ec920100liv6.html
C易特奖励制度 
http://blog.sina.com.cn/cyite6
C易特更多资料博客

我来回复

您尚未登录,请登录后再回复。点此登录或注册