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. spring mvc框架web.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  2. 13.Python接口自动化测试 -- 豆瓣

    1.代码如下所示: import requests,unittest import json import HTMLTestRunner class TestDouBan(unittest.TestC ...

  3. Hadoop序列化与Writable接口(二)

    Hadoop序列化与Writable接口(二) 上一篇文章Hadoop序列化与Writable接口(一)介绍了Hadoop序列化,Hadoop Writable接口以及如何定制自己的Writable类 ...

  4. Python——你应该知道这些

    1. Python的出生 1989年 Guido van Rossum开始编写Python语言编辑器(龟叔为了打发无聊的圣诞节) 1991年 第一个Python编译器诞生(正式诞生) 1994年 Py ...

  5. mysql的partition分区

    前言:当一个表里面存储的数据特别多的时候,比如单个.myd数据都已经达到10G了的话,必然导致读取的效率很低,这个时候我们可以采用把数据分到几张表里面来解决问题.方式一:通过业务逻辑根据数据的大小通过 ...

  6. C/S模式与B/

    网络程序开发的两种计算模式--C/S模式与B/S模式.两种各有千秋,用于不同场合. C/S适用于专人使用,安全性要求较高的系统: B/S适用于交互性比较频繁的场合,容易被人们所接受,倍受用户和软件开发 ...

  7. bfs判断子图是否连通

    int judge() { int v[13] = { 0 }; queue<int> myq; myq.push(ans[0]); v[ans[0]] = 1; while (!myq. ...

  8. 以太坊客户端Geth命令用法

    命令用法 geth [选项] 命令 [命令选项] [参数…] 命令: account 管理账户attach 启动交互式JavaScript环境(连接到节点)bug 上报bug Issuesconsol ...

  9. linux下 tomcat 日志乱码/中文链接404

    1 日志乱码: JDK引用的设置 Java引用参数添加”-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8” 将上面参数添加到Catalina.sh中JAVA ...

  10. java web 程序---注册页面密码验证

    <%@ page language="java" import="java.util.*" pageEncoding="gb2312" ...