• Delphi自定义窗口之一(不可见窗口)


    -------------------------D7-

    仅仅是学习,不要太较真,哈哈

    资料来源忘记了

    --------------------------------

    -----------unit开始

    -------------------------------MyCustomForm_u开始--------------

    unit MyCustomForm_u;

    interface
    uses Windows,Classes,Messages;

    const MY_MESSAGE = WM_USER + 1;

    type
    TMyCustomForm=class(TComponent)
    private
    FHandle: HWND;
    procedure MainWndProc(var Message: TMessage);// message MY_MESSAGE;{自定义一个窗口过程}
    procedure DoMY_MESSAGE(var Msg: TMessage); message MY_MESSAGE;
    public
    constructor Create(AOwner: TComponent);override;
    destructor Destroy;override;
    published
    property Handle: HWND read FHandle; {发布一个Handle属性,这样别人可 以给它发送消息}
    end;

    implementation

    { TMyCustomForm }

    constructor TMyCustomForm.Create(AOwner: TComponent);
    begin
    inherited;
    FHandle := Classes.AllocateHWnd(MainWndProc);

    {AllocateHWnd是Classes单元的全局过程,需要一个TWndMethod类型的参数(即一个窗
    口过程);其作用是创建一个不可见窗口并将参数传入的窗口过程关联于它,返回所创建窗
    口的句柄}
    end;

    destructor TMyCustomForm.Destroy;
    begin
    if FHandle <> 0 then
    Classes.DeallocateHWnd(FHandle); {销毁已创建的窗口}
    inherited;
    end;

    procedure TMyCustomForm.DoMY_MESSAGE(var Msg: TMessage);
    begin
    Messagebox(0,'接到自定义消息0','自定义',MB_OK);
    end;

    procedure TMyCustomForm.MainWndProc(var Message: TMessage);
    begin
    {if Message.Msg = WM_CLOSE then
    MessageBox(0, '收到消息WM_CLOSE', '收到消息', MB_OK)
    else
    DefaultHandler(Message);
    if Message.Msg=MY_MESSAGE then
    Messagebox(0,'接到自定义消息1','自定义',MB_OK); }
    Dispatch(Message);
    end;

    end.

    --------------------------------MyCustomForm_u结束------------

    ---------------Unit1开始--------------------------

    unit Unit1;

    interface

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

    const MY_MESSAGE_01 = WM_USER + 11;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    private
    procedure DoMY_MESSAGE(var Msg: TMessage); message MY_MESSAGE_01;
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation
    uses MyCustomForm_u;
    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var
    DemoMsgCom: TMyCustomForm;
    begin
    DemoMsgCom := TMyCustomForm.Create(nil);
    SendMessage(DemoMsgCom.Handle, MY_MESSAGE, 0, 0);
    SendMessage(DemoMsgCom.Handle, WM_CLOSE, 0, 0);
    FreeAndNil(DemoMsgCom);
    end;

    procedure TForm1.DoMY_MESSAGE(var Msg: TMessage);
    begin
    Messagebox(0,'接到自定义消息','自定义',MB_OK);
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    SendMessage(self.Handle, MY_MESSAGE_01, 0, 0);
    end;

    end.

    -----------------Unit1结束----------------------

    ---------------------------Unit结束

    ------------Form

    object Form1: TForm1
    Left = 918
    Top = 676
    Width = 259
    Height = 256
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 80
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
    end
    object Button2: TButton
    Left = 80
    Top = 112
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
    end
    end

    --------------Form

  • 相关阅读:
    Vue3手册译稿
    Vue3手册译稿
    Vue3手册译稿
    Vue3手册译稿
    Vue3手册译稿
    Vue3手册译稿
    C# 多线程与异步的使用方法笔记
    PetaPoco 5.1.306 的生成模板加注释
    RichEditDocumentServer打印记录
    RichEditDocumentServer 打印份数
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14627912.html
Copyright © 2020-2023  润新知