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. [转]blocks编程

    原文地址:http://geeklu.com/2012/01/block/ 介绍 声明创建和调用 Block和变量 Block实际应用 1.介绍 Block是一个C Level的语法以及运行时的一个特 ...

  2. vps上搭建jupyter notebook远程服务

    安装anaconda 使用如下命令下载: wget https://repo.continuum.io/archive/Anaconda3-5.0.0.1-Linux-x86_64.sh 如果非roo ...

  3. stm32寄存器版学习笔记05 PWM

    STM32除TIM6和TIM7外都可以产生PWM输出.高级定时器TIM1和TIM8可以同时产生7路PWM,通用定时器可以产生4路PWM输出. 1.TIM1 CH1输出PWM配置步骤 ①开启TIM1时钟 ...

  4. Paths中的几个重要元素

    Paths中的几个重要元素 Points void CGContextMoveToPoint (    CGContextRef c,    CGFloat x,    CGFloat y ); 指定 ...

  5. python ctypes 和windows DLL互相调用

    图片项目

  6. altera官方推荐时钟使用方法

    Register Combinational Logic Outputs If you use the output from combinational logic as a clock signa ...

  7. linux 系统下配置maven环境

    1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.解压源码包 通过终端在/usr/l ...

  8. $x \rightarrow \infty$时多项式型函数的极限

    \[ \lim_{x \rightarrow \infty} \frac{\sqrt{4x^6-5x^5}-2x^3}{\sqrt[3]{27x^6+8x}} \\ =\lim_{x \rightar ...

  9. VMware harbor && minio 搭建企业docker私有镜像以及需要注意的问题

    1. docker harbor  配置      建议使用在线安装的模式(离线包太大了)    首先需要安装docker-compose .docker .mino (具体安装可以参考官网后者我的博 ...

  10. linux通过wget直接下载jdk,避免用户验证

    下载地址 :http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 下载语句: wget ...