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. native 方法列表说明

    方法列表说明 关于static const JNINativeMethod method_table[]方法列表的原型如下: typedef struct { const char* name; co ...

  2. iOS8扩展插件开发配置 [转载]

    一.iOS8扩展插件概述 WWDC14除了发布了OS X v10.10和switf外,iOS8.0也开始变得更加开放了.说到开放,当然要数应用扩展(App Extension)了.顾名思义,应用扩展允 ...

  3. IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)

    Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...

  4. turtle海龟作图

    个人心得:python这么火是有他的原因的,突然发现他的语言很有趣,库函数也很多. 这次是第一步,简单了解了一下turtle函数 import turtle import time def drawS ...

  5. Linux驱动程序接口

    §1. Linux驱动程序接口 系统调用是操作系统内核与应用程序之间的接口,设备驱动程序则是操作系统内核与机器硬件的接口.几乎所有的系统操作最终映射到物理设备,除了CPU.内存和少数其它设备,所有的设 ...

  6. php中strstr、strchr、strrchr、substr、stristr

    一.strstr 和 strcchr的区别 strstr   显示第一次找到,要查找的字符串,以及后面的字符串. strrchr 显示最后一次找到,要查找的字符串,以及后面的字符串. 二.strstr ...

  7. php基础语法(变量)

    PHP常用表现形式: 1.<?php .....这里是php代码 ?> 2.<? .....这里是php代码 ?> 此形式依赖于php.ini中的一项设置: short_ope ...

  8. c++中接口

    C++中,通过类实现面向对象的编程,而在基类中只给出纯虚函数的声明,然后在派生类中实现纯虚函数的具体定义的方式实现接口,不同派生类实现接口的方式也不尽相同,从而实现多态. 我们需要遵循一些规则: 声明 ...

  9. Redis简单介绍与安装

    Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序. Redis有三个主要特点,使它优越于其它键值数据存储系统 - 1) Redis将其数据库完全保存在内 ...

  10. 初探babel转换器的安装与使用

    一.配置.babelrc文件(没有名字的文件) Babel的配置文件是.babelrc,存放在项目的根目录下.使用Babel的第一步,就是配置这个文件. 基本格式如下: { "presets ...