WndProc WindowProc

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 + 2000;

__fastcall TMyListBoxDescendant::TMyListBoxDescendant(TComponent* Owner)
    : TListBox(Owner)
{
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  PostMessage(
    MyListBoxDescendant1->Handle,
    WM_STYLEMESSAGE,
    Integer(lbOwnerDrawFixed),
    0);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  PostMessage(
    MyListBoxDescendant1->Handle,
    WM_STYLEMESSAGE,
    Integer(lbStandard),
    0);
}

void __fastcall TForm1::SubClassRadioGroup1Click(TObject *Sender)
{
  MyListBoxDescendant1->ToggleSubClass(SubClassRadioGroup1->ItemIndex == 0);
}

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 = 2;   // 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(0, 0, bitmap->Width, bitmap->Height), clRed); // Render bitmap.
    Offset += bitmap->Width + 4;   // 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 + 30;;
  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(0, bitmap0);
  MyListBoxDescendant1->Items->AddObject("Butterfly", bitmap0);

  SubClassRadioGroup1->Items->Add("SubClassWndProc");
  SubClassRadioGroup1->Items->Add("WndProc");
  SubClassRadioGroup1->ItemIndex = 2;
}
时间: 2024-08-07 16:48:04

WndProc WindowProc的相关文章

眼见为实(2):介绍Windows的窗口、消息、子类化和超类化

眼见为实(2):介绍Windows的窗口.消息.子类化和超类化 这篇文章本来只是想介绍一下子类化和超类化这两个比较"生僻"的名词.为了叙述的完整性而讨论了Windows的窗口和消息,也简要讨论了进程和线程.子类化(Subclassing)和超类化(Superclassing)是伴随Windows窗口机制而产生的两个复用代码的方法.不要把"子类化.超类化"与面向对象语言中的派生类.基类混淆起来."子类化.超类化"中的"类"是指W

outdated: 40.Introduction to Physical Simulations

这一节为物理模拟绳子.在上一节中,三种物理模拟的基本运动作为这一节的基础.(直线运动, 弹性运动,万有引力下运动) 让绳子拥有弹性感,可以在每个质量节点中加入很小长度的弹簧.弹簧上力的变化可参考上节的弹性运动. 基本上所谓的重点就是绳子模拟类了,RopeSimulation类继承Simulation类(上节中). 类中定义了,每个弹簧,万有引力, 每节绳子连接处位置和速率,还有地面对绳子的抵抗力系数,地面摩擦力系数, 地面吸引力系数,地面高度,空气摩擦力系数.先来看个构造函数, RopeSimu

Pascal小游戏 俄罗斯方块怀旧版

俄罗斯方块怀旧版(注释版) {$APPTYPE GUI}{$MODE DELPHI}program WinPiece; usesWindows; constAppName = 'WinPiece';pm = 25; vardc : hdc;AMessage : Msg;hWindow: HWnd;hPen ,hBrush : longword;intNextPiece, intCurPiece,intTempPiece : longint;BigMap : array [0..11,-4..20

outdated: 48.ArcBall Rotation

认认真真地把这教程过一遍还真是费事啊... 终于到最后一章了,鼠标拖动可实现物体旋转. 下面为代码, #ifndef _ArcBall_h #define _ArcBall_h // Only support assertions in debug builds #ifdef _DEBUG #include <assert.h> #else #define assert(x) { } #endif // _DEBUG #include <GL\glut.h> #include &l

outdated: 41.Volumetric Fog &amp; IPicture Image Loading

雾气的实现在Cool Looking Fog中提到过,不过怎么实现雾气随距离的变化? 在Initialize()函数中首先设置fog的褪色,颜色,开始透明度和结尾透明度等等.如下, glEnable(GL_FOG); glFogi(GL_FOG_MODE, GL_LINEAR); // Fog fade is linear glFogfv(GL_FOG_COLOR, fogColor); // Set the color glFogf(GL_FOG_START, 0.0f); // Set th

outdated: 32.Picking, Alpha Blending, Alpha Testing, Sorting

一个射击类的小demo.分为三个文件,Previous.h.Previous.cpp和Main.cpp. 在Previous.cpp的CreateWindowGL中新增了AdjustWindowRectEx()函数,可以根据客户端的期望大小计算窗口大小,矩形通过函数创建一个理想大小的窗口. ChoosePixelFormat()函数试图匹配一个最接近的像素格式通过上下文给的像素格式规格. 在Mian.cpp文件的Selection()函数中,glGetIntegerv()函数返回一个选定后的参数

.NET Windows Form 改变窗体类名(Class Name)有多难?

研究WinForm的东西,是我的一个个人兴趣和爱好,以前做的项目,多与WinForm相关,然而这几年,项目都与WinForm没什么关系了,都转为ASP.NET MVC与WPF了.关于今天讨论的这个问题,以前也曾深入研究过,只是最近有朋友问到这个问题,就再挖挖这个坟(坑). 一.类名是啥? 打开神器SPY++,VS2013 在[工具]菜单里: VS2013之前的VS版本,在[开始菜单]里: 打开SPY++,点击标注的按钮, 在打开的窗口上,把雷达按钮拖到你想查看的窗口,就可以看到它的类名了,下面就

outdated: 35.Playing AVI Files In OpenGL

AVI视频文件读取,首先得建个AVI(AVISTREAMINFO)文件流指针,具体的结构, typedef struct { DWORD fccType; // Stream type DWORD fccHandler; // Compressor handler DWORD dwFlags; // Applicable flags for the stream DWORD dwCaps; // Capability flags; currently unused WORD wPriority;

VCL消息处理机制

http://www.cnblogs.com/railgunman/archive/2010/12/10/1902524.html#2868236 说到VCL中的消息处理就不能不提到TApplication,Windows会为每一个当前运行的程序建立一个消息队列,用来完成用户与程序的交互,正是通过Application完成了对Windows消息的集中处理! 首先通过Application.Run进入消息循环进行消息的处理,其中调用了HandleMessage. procedure TApplic