如何重载 WndProc

iamfancy 2001-09-25 08:01:08
我的 BCB5,程序中只有一个 FORM,需要重载 WndProc 并处理其中的消息,请问我该怎样重载 WndProc,我自己试了两次,都失败了,一次编译不通过,一次运行就发生异常。请大是门说详细一些。谢谢
...全文
306 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamfancy 2001-09-28
  • 打赏
  • 举报
回复
maquanjun(俺是菜鸟俺怕谁)
你的代码有点小问题,就是……你自己运行试试吧,改正方法很简单,就是把
TForm::WndProc(Message); 从 else 分支里放出来,去掉 else 分支。
maquanjun 2001-09-28
  • 打赏
  • 举报
回复
楼上的秃和尚是高手,你该多问问他
maquanjun 2001-09-28
  • 打赏
  • 举报
回复
这是我写的可以么?我也发给你
WndProc是TControl的一个虚函数,所有TControl的派生类都可以重载这个函数来响应消息。它的参数是通用的TMessage结构,它的功能是截获所有的Windows消息。应用的步骤如下:
第一步:在form的头文件中添加声明。
private:
void __fastcall WndProc(Messages::TMessage &Message);
第二步:编写消息处理函数。注意,由于WndProc截获所有的消息,所以对于不需要响应的消息必须要调用基类的WndProc进行处理。
void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
if( (Message.Msg == WM_SYSCOMMAND) &&
(Message.WParam == SC_SCREENSAVE) )
{
ShowMessage("接收到屏幕保护消息");
Message.Result = true;
}
else
TForm::WndProc(Message);
iamfancy 2001-09-28
  • 打赏
  • 举报
回复
maquanjun(俺是菜鸟俺怕谁) 可以编一个样例程序寄给我吗?
bianchenkr@21cn.com
ydx 2001-09-26
  • 打赏
  • 举报
回复
UP
comanche 2001-09-26
  • 打赏
  • 举报
回复
楼上说得没错,
但,这不是重载的概念吧...

另外还有种办法,就是子类化,如果只指对主Form, Application中有两个方法
HookMainWindow
和 UnhookMainWindow
xycleo 2001-09-25
  • 打赏
  • 举报
回复
void __fastcall TForm1::WndProc(Messages::TMessage&Message)
{ //重载wndproc,处理个各种系统消息
POINT MousePos;
if(Message.Msg==iconmessage)
{
if(Message.LParam==WM_LBUTTONDBLCLK)
{
Application->Terminate();//如果双击图标,则关闭应用程序
}
else if(Message.LParam==WM_LBUTTONDOWN)
{
Show();

}
else if(Message.LParam==WM_RBUTTONUP)
{
if (GetCursorPos(&MousePos))
{
PopupMenu1->PopupComponent=Form1;
SetForegroundWindow(Handle);
PopupMenu1->Popup(MousePos.x, MousePos.y);
}
}
return;
}
TForm::WndProc(Message);//对于其他的消息,调用基础类的WndProc函数让Windows进行缺省处理。
}


maquanjun 2001-09-25
  • 打赏
  • 举报
回复
All descendants of TControl, including TForm, contain a WndProc function that serves as the window procedure for the component. This function is virtual, which means that you can override the function to intercept messages. The argument to WndProc is a TMessage structure passed by reference that contains the message command ID and the WPARAM and LPARAM values.

Step 1: Add the WndProc declaration to your form's header.

private:
void __fastcall WndProc(Messages::TMessage &Message);

Step 2: Code the function. The derived WndProc should call the base class TForm::WndProc for all messages that you don't process. This function prevents the windows screensaver from starting.

void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
if( (Message.Msg == WM_SYSCOMMAND) &&
(Message.WParam == SC_SCREENSAVE) )
{
Application->MessageBox("Good bye screen saver", "SCR No", MB_OK);
Message.Result = true;
}
else
TForm::WndProc(Message);
}

13,870

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧