子类化窗口辅助类NativeWindow

Delphi 专栏收录该内容
2 篇文章 0 订阅

子类化窗口在高级开发中的重要利器。受c#中NativeWindow的启发,实现了下面这样一个简单的类,来帮助更容易地对窗口实现子类化。

unit NativeWindow;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

Type
  TzwNativeWindow = class
  private
    m_OldWinProc:FARPROC  ;
    m_NewWinProc:FARPROC  ;
    m_Handle:THandle;
  protected
    procedure WndProc(var Message: TMessage); virtual;
  public
    constructor Create;
    destructor Destroy;override;
    procedure AssignHandle(WndHandle:THandle);
    procedure ReleaseHandle;
    property Handle:THandle read m_Handle;
  end;

implementation

{ TzwNativeWindow }

procedure TzwNativeWindow.AssignHandle(WndHandle: THandle);
begin
  if (m_Handle<>WndHandle) then
  begin
    ReleaseHandle;
  end;
  m_Handle := WndHandle;
  m_NewWinProc := MakeObjectInstance (WndProc );
  m_OldWinProc := pointer(GetWindowLong (m_Handle ,GWL_WNDPROC ));
  SetWindowLong(m_Handle , GWL_WNDPROC, LongInt(m_NewWinProc));
end;

constructor TzwNativeWindow.Create;
begin
  m_Handle := INVALID_HANDLE_VALUE;
  m_OldWinProc := nil;
  m_NewWinProc := nil;
end;

destructor TzwNativeWindow.Destroy;
begin
  ReleaseHandle;
  inherited;
end;

procedure TzwNativeWindow.ReleaseHandle;
begin
  if (m_Handle<>INVALID_HANDLE_VALUE) then
  try
    SetWindowLong(m_Handle,GWL_WNDPROC,LongInt(m_OldWinProc));
  finally
    m_Handle := INVALID_HANDLE_VALUE;
    m_OldWinProc := nil;
    m_NewWinProc := nil;
  end;
end;

procedure TzwNativeWindow.WndProc(var Message: TMessage);
begin
  with Message do
  begin
    Result := CallWindowProc(m_OldWinProc , m_Handle , Msg, wParam, lParam);
  end;
end;

end.

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值