一个C++Bulider 消息过程处理WndProc的问题

En•Nam 2013-05-08 11:26:33
TForm类继承于TCustomForm类。在TCustomForm类中,WndProc是一个Protected的Virtual函数。TForm类中未有另外实现WndProc。TForm1类(也就是我们创建新的工程时默认生成的)继承于TForm类。

好了,问题就来了,当我在TForm1中重新实现WndProc时,编译提示在Obj中已经有了这个WndProc。
而且我在TForm1的实例Form1中通过TCustomForm::WndProc调用时,提示TCustomForm::WndProc是不可见的,不是说好了Protected可以在子类中被访问吗。
那么,我如果自己重现WndProc处理一些特定的消息过后,其它的消息我应该传给谁去处理呢?我看到很多书上有写着是TForm::WndProc。而这里编译时实际上指向了TCustomForm::WndProc,提示不可见,不能调用。
...全文
446 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaojian19890807 2014-11-06
  • 打赏
  • 举报
回复
你们的代码是怎么放的 我的代码添加进去以后一直提示 stack overflow
淮北小山 2013-05-27
  • 打赏
  • 举报
回复
是啊,我刚刚也丢了TForm1 ::了。
En•Nam 2013-05-08
  • 打赏
  • 举报
回复
引用 7 楼 fxworld 的回复:
void __fastcall WndProc(TMessage& Message) 改为: void __fastcall TForm1::WndProc(TMessage& Message)
啊哈,这样也行的。把TForm1::忘了。真让人蛋痛啊。所以还是得仔细啊。谢谢了。
En•Nam 2013-05-08
  • 打赏
  • 举报
回复
引用 4 楼 ksrsoft 的回复:
TForm1是直接父继承与TForm,不是TCustomForm
方法三 重载 WndProc

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

 

void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
 
      Caption = Now();
    
    TForm::WndProc(Message);
}
我发现了,过程的实现必须要在定义class的内部。只要不在 class { }内的,都不行。谢谢!
fxworld 2013-05-08
  • 打赏
  • 举报
回复
void __fastcall WndProc(TMessage& Message) 改为: void __fastcall TForm1::WndProc(TMessage& Message)
En•Nam 2013-05-08
  • 打赏
  • 举报
回复
错误提示是: [C++ Error] Unit1.cpp(26): E2247 '_fastcall TCustomForm::WndProc(TMessage &)' is not accessible [C++ Error] Unit1.cpp(26): E2283 Use . or -> to call '_fastcall TCustomForm::WndProc(TMessage &)'
En•Nam 2013-05-08
  • 打赏
  • 举报
回复
引用 3 楼 fxworld 的回复:
应该是你的代码出错,通过实例对象不可以调用 protected 成员,只能调用 public 成员。把你的代码贴出来看看。
首先我在头文件中添加了过程WndProc说明: class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations void __fastcall WndProc(TMessage& Message); //重现过程WndProc public: // User declarations __fastcall TForm1(TComponent* Owner); virtual __fastcall }; 然后在.cpp文件中实现过程代码段: //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall WndProc(TMessage& Message) { switch(Message.Msg) { case WM_LBUTTONDOWN: ShowMessage("Mouse Left Clicked!"); break; default:break; } TForm::WndProc(Message); }
缘中人 2013-05-08
  • 打赏
  • 举报
回复
TForm1是直接父继承与TForm,不是TCustomForm
方法三 重载 WndProc

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

 

void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
 
      Caption = Now();
    
    TForm::WndProc(Message);
}
fxworld 2013-05-08
  • 打赏
  • 举报
回复
应该是你的代码出错,通过实例对象不可以调用 protected 成员,只能调用 public 成员。把你的代码贴出来看看。
En•Nam 2013-05-08
  • 打赏
  • 举报
回复
引用 1 楼 fxworld 的回复:
你用public继承就可以了 class TForm1 : public TForm { };
是的,BCB自动生成的Form都是以public继承的。但是不行。
fxworld 2013-05-08
  • 打赏
  • 举报
回复
你用public继承就可以了 class TForm1 : public TForm { };

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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