SubClassWndProc

This example shows how to use the WndProc method and the WindowProc property to subclass a custom control's window procedure. This example subclasses the window procedure of a TListBox descendant to respond to a user-defined message called WM_STYLEMESSAGE. The subclassed window procedure can be turned on or off by pressing an option button.

class TMyListBoxDescendant : public TListBox
{
__published: IDE-managed Components
void __fastcall SubClassWndProc(Messages::TMessage &Message);
void __fastcall ToggleSubClass(bool On);
void __fastcall OnDrawItemProc(
TWinControl *Control, int Index, const TRect &Rect,
TOwnerDrawState State);
private: // User declarations
public: // User declarations
__fastcall TMyListBoxDescendant(TComponent* Owner);
}; TForm1 *Form1;
TMyListBoxDescendant *MyListBoxDescendant1;
Graphics::TBitmap *bitmap0; const WM_STYLEMESSAGE = WM_USER + ; __fastcall TMyListBoxDescendant::TMyListBoxDescendant(TComponent* Owner)
: TListBox(Owner)
{
} void __fastcall TForm1::Button1Click(TObject *Sender)
{
PostMessage(
MyListBoxDescendant1->Handle,
WM_STYLEMESSAGE,
Integer(lbOwnerDrawFixed),
);
} void __fastcall TForm1::Button2Click(TObject *Sender)
{
PostMessage(
MyListBoxDescendant1->Handle,
WM_STYLEMESSAGE,
Integer(lbStandard),
);
} void __fastcall TForm1::SubClassRadioGroup1Click(TObject *Sender)
{
MyListBoxDescendant1->ToggleSubClass(SubClassRadioGroup1->ItemIndex == );
} void __fastcall TMyListBoxDescendant::SubClassWndProc(Messages::TMessage &Message)
{
if (Message.Msg == WM_STYLEMESSAGE)
Style = (TListBoxStyle)Message.WParam;
else
WndProc(Message);
} void __fastcall TMyListBoxDescendant::ToggleSubClass(bool On)
{
if (On)
WindowProc = SubClassWndProc;
else
WindowProc = WndProc;
} #include <memory> //For STL auto_ptr class void __fastcall TMyListBoxDescendant::OnDrawItemProc(TWinControl *Control, int Index,
const TRect &Rect, TOwnerDrawState State)
{
Graphics::TBitmap *bitmap; // Temporary variable for the item's bitmap
int Offset = ; // Default text offset width // Note that you draw on the list box's canvas, not on the form
TCanvas *canvas = dynamic_cast<TListBox *>(Control)->Canvas;
canvas->FillRect(Rect); // Clear the rectangle.
bitmap = dynamic_cast<Graphics::TBitmap *>((dynamic_cast<TListBox *>(Control))->Items->Objects[Index]);
if (bitmap)
{
canvas->BrushCopy(
Bounds(Rect.Left + Offset, Rect.Top, bitmap->Width, bitmap->Height),
bitmap, Bounds(, , bitmap->Width, bitmap->Height), clRed); // Render bitmap.
Offset += bitmap->Width + ; // Add four pixels between bitmap and text.
}
// Display the text
canvas->TextOut(Rect.Left + Offset, Rect.Top, dynamic_cast<TListBox *>(Control)->Items->Strings[Index]);
} __fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
MyListBoxDescendant1 = new TMyListBoxDescendant(Form1); // The owner cleans this up.
MyListBoxDescendant1->Visible = True;
MyListBoxDescendant1->Parent = Form1;
MyListBoxDescendant1->Visible = True;
MyListBoxDescendant1->Left =
SubClassRadioGroup1->Left + SubClassRadioGroup1->Width + ;;
MyListBoxDescendant1->Top = SubClassRadioGroup1->Top;
MyListBoxDescendant1->Height = SubClassRadioGroup1->Height;
MyListBoxDescendant1->OnDrawItem =
MyListBoxDescendant1->OnDrawItemProc; static std::auto_ptr<Graphics::TBitmap> _bitmap0Cleaner(bitmap0 = new Graphics::TBitmap);
ImageList1->GetBitmap(, bitmap0);
MyListBoxDescendant1->Items->AddObject("Butterfly", bitmap0); SubClassRadioGroup1->Items->Add("SubClassWndProc");
SubClassRadioGroup1->Items->Add("WndProc");
SubClassRadioGroup1->ItemIndex = ;
}

动态转换:

dynamic_cast

TForm2 * p=dynamic_cast<TForm2*>(Form2);

dynamic_cast<TListBox *>

控件的WndProc WindowProc的更多相关文章

  1. 子类化窗口控件的窗口过程(系统级替换,与直接替换控件的WndProc方法不是一回事)

    要说灵活性,自然是比不上Delphi自带的覆盖WndProc,或者替换WndProc方法. unit Unit1; interface uses Windows, Messages, SysUtils ...

  2. C# 控件双缓冲控制 ControlStyles 枚举详解

    ControlStyles 枚举 .NET Framework 4    指定控件的样式和行为. 此枚举有一个 FlagsAttribute 特性,通过该特性可使其成员值按位组合. 命名空间:  Sy ...

  3. .Net WinForm 控件键盘消息处理剖析

    在WinForm控件上我们可以看到很多关于键盘消息处理的方法,比如OnKeyDown, OnKeyPress, ProcessCmdKey, ProcessDialogKey,IsInputKey等等 ...

  4. C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈

    研究了一下KeyDown,KeyPress 和 KeyUp 的学问.让我们带着如下问题来说明: 1.这三个事件的顺序是怎么样的? 2.KeyDown 触发后,KeyUp是不是一定触发? 3.三个事件的 ...

  5. WinForm 控件键盘消息处理剖析(转)

    一直想整理键盘事件的调用顺序,刚好看见园子里的这篇文章,写的不错,就转载了:http://www.cnblogs.com/tedzhao/archive/2010/09/07/1820557.html ...

  6. 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!

    其实很简单: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, D ...

  7. VCL控件组件大都应该重载TWinControl的虚函数WndProc来进行处理窗口消息的工作

    TWinControl的构造函数中会调用MakeObjectInstance并且传递MainWndProc作为窗口消息处理函数,而MainWndProc则会调用虚函数WndProc来处理窗口消息.留个 ...

  8. Delphi对象变成Windows控件的前世今生(关键是设置句柄和回调函数)goodx

    ----------------------------------------------------------------------第一步,准备工作:预定义一个全局Win控件变量,以及一个精简 ...

  9. TWinControl的刷新过程(5个非虚函数,4个覆盖函数,1个消息函数,默认没有双缓冲,注意区分是TCustomControl还是Windows原生封装控件,执行流程不一样)

    前提条件:要明白在TWinControl有以下四个函数的存在,注意都是虚函数: procedure Invalidate; override;procedure Update; override;pr ...

随机推荐

  1. 卡诺模型(KANO Model)

    卡诺模型是一种研究影响顾客满意度因素的方法,在软件工程中可以用来辅助做需求分析和优化产品的质量. 作为一种方法,卡诺模型将某一种特性的存在程度作为横坐标,越大表示某个功能或特性做得越多,越小则表示做得 ...

  2. iOS设备是否越狱的判断代码

    苹果是非常看重产品的安全性的,所以给用户设计了一套复杂的安全机制.这让喜爱自由,崇尚一切开放的程序员们极度不爽,于是越狱就成了苹果和黑客们反复斗法的场所.总体来说,越狱可以让我们随意安装.共享应用,但 ...

  3. 《selenium2 python 自动化测试实战》(21)——unittest单元测试框架解析

    unittest是展开自动化测试的基础——这个框架很重要! 我们先自己写一个测试类: 1.被测试类 Widthget.py: # coding: utf-8 class Widthget:    de ...

  4. 接口测试框架——第六篇-读Excel封装方法

    谢谢@小麦苹果的提醒,才发现我借口测试少写了一个文件,今天给大家补上: common->service->excel_case_data.py # coding: utf-8 import ...

  5. DispatcherServlet的初始化(二)

    DispatcherServlet的初始化在springmvc的启动中有讲过,这一篇在上一篇的基础上接着讲.DispatcherServlet作为springmvc的前端控制器,还需要初始化其他的模块 ...

  6. python模块--如何相互调用自己写的模块

    一.模块相互调用同级目录调用时的两种方法 import module print(module.add(3,8)) from module import add print(add(2,4)) 同级目 ...

  7. 使用 lego生成 Let's Encrypt 证书

    1. 工具 https://github.com/xenolf/lego   2. 使用 命令生成新的 lego --email="foo@bar.com" --domains=& ...

  8. 【转】Ubuntu中Vmware Tools的安装与卸载

    原文网址:http://blog.csdn.net/huanghe423/article/details/7005611 Vmware Tools是VMware提供的一套非常人性化的程序,可以用来解决 ...

  9. Docker安装ShowDoc

    ShowDoc就是一个非常适合IT团队的在线文档分享工具,它可以加快团队之间沟通的效率. 一.下载showDoc资源 打开 https://github.com/star7th/showdoc 复制其 ...

  10. cocos2dx 3.0 编译工程

    下载完2dx,运行setup.py,参考设置ANDROID_SDK,NDK_ROOT,ANT_ROOT变量 创建工程 cocos new testGame -p com.game.test -l cp ...