一、重新实现WndProc,注意名称要一致,查看继承链会发现WndProc是一个虚函数。
void __fastcall TForm1::WndProc(TMessage& message)
{
TForm::WndProc(message);
if(message.Msg==WM_CHAR)
ShowMessage(message.WParam);
}
二、添加消息映射
protected:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_CHAR, TMessage, OnChar)
END_MESSAGE_MAP(TForm)
void __fastcall TForm1::OnChar(TMessage & message)
{
//TODO: Add your source code here
ShowMessage(message.WParam);
}
三、子类化
WNDPROC OldProc;
LRESULT CALLBACK MyWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
if(message==WM_CHAR)
ShowMessage(wParam);
return OldProc(hwnd,message,wParam,lParam);
}
在窗体构造或创建事件中替换窗口过程
OldProc=(WNDPROC)GetWindowLongA(Handle,GWL_WNDPROC);
SetWindowLongA(Handle,GWL_WNDPROC,(LONG)MyWndProc);