中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi 截获非可视化组件的消息

来源:国外 关于:Peter Johnson 发布时间:2007-07-03   [收藏] [推荐]
Problem/Question/Abstract:
Sometimes we need a non-windowed component (i.e. one that isn't derived from TWinControl) to receive Windows messages - but non-windowed component don't have window handles. For example suppose we are developing a non-visual component that registers our application as a clipboard viewer so the application can respond to changes in the clipboard. To get information about clipboard changes our component needs to receive messages from Windows.
Answer:
The Delphi library function AllocateHWnd is used to create a hidden window for us and the related DeallocateHWnd disposes of the window when we've finished with it.
The hidden window needs a window procedure. We can use a method of our component class to provide the window procedure. AllocateHWnd takes a reference to the method its parameter - it takes care of the problem of registering the method as a window procedure for us. In the method we handle the messages we are interested in and hand the rest off to Windows using the DefWindowProc API call.
The following code gives the skeleton of how to use AllocateHWnd. First, here's the class declaration from the interface section of code:
type
  // Our class derived from TComponent
  // (or another ancestor class)
  TMyClass = class(TComponent)
  private
    FHWnd: HWND;
    // field to store the window handle
  {...}
  protected
    procedure WndMethod(var Msg: TMessage); virtual;
    // the window proc - called by Windows to handle
    // the given message
  {...}
  public
    constructor Create(AOwner: TComponent); override;
    // create window proc here
    destructor Destroy; override;
    // free window proc here
  {...}
  end;
And here's the implementation details:
TMyClass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  {... }
  // Create the window
  FHWnd := AllocateHWnd(WndMethod);
  { ...}
end;
TMyClass.Destroy;
begin
  {...}
  // Destroy the window
  DeallocateHWnd(FHWnd);
  {...}
  inherited Destroy;
end;
TMyClass.WndMethod(var Msg: TMessage);
var
  Handled: Boolean;
begin
  // Assume we handle message
  Handled := True;
  case Msg.Msg of
    WM_SOMETHING: DoSomething;
    // Code to handle a message
    WM_SOMETHINGELSE: DoSomethingElse;
    // Code to handle another message
  {...}
  else
    // We didn't handle message
    Handled := False;
  end;
  if Handled then
    // We handled message - record in message result
    Msg.Result := 0
  else
    // We didn't handle message
    // pass to DefWindowProc and record result
    Msg.Result := DefWindowProc(FHWnd, Msg.Msg,
      Msg.WParam, Msg.LParam);
end;
Of course, we could just use the Windows API to create a window the hard way and provide a windows procedure. But it is more difficult to use a method (rather than a simple procedure) as a window procedure if we do it this way. The clever features about AllocateHWnd are that (a) it creates the hidden window for us and (b) it allows us to use a method, rather than a simple procedure as the window procedure -- and a method is more useful since it has access to the class's private data.

[浏览: 次]   
上一篇:delphi 通过消息发送字符到其它应用程序   下一篇:delphi 如何截获窗口最大化消息
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 消息是由谁来发出又由谁来完成
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932