zoukankan      html  css  js  c++  java
  • Delphi中为事件赋值(把某个单元的过程赋值给事件)

    把普通方法赋值给事件或者类中的方法

    --开发环境:D7

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

    unit Unit1;

    interface

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

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    public
    procedure MyFormbuttonClick(Sender:TObject);
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation
    uses
    Unit2;
    {$R *.dfm}

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    MybuttonClick(Sender);
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    var
    vMybuttonClick:TNotifyEvent;
    begin
    TMethod(vMybuttonClick).Code:=@MybuttonClick ; //Unit2中的MybuttonClick
    Button1.OnClick:=vMybuttonClick;
    //Button1.OnClick:=MyFormbuttonClick ; //MyFormbuttonClick可以直接复制
    end;

    procedure TForm1.MyFormbuttonClick(Sender: TObject);
    begin
    MessageBox(0,PChar('AAA'),PChar('提示'),MB_OK);
    end;

    end.

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

    --------------Form1开始

    object Form1: TForm1
    Left = 745
    Top = 544
    Width = 203
    Height = 156
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    OnCreate = FormCreate
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 48
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    end
    object Button2: TButton
    Left = 48
    Top = 80
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
    end
    end

    ----------------------Form1结束

    -------------------------------Unit2开始

    unit Unit2;

    interface

    uses
    Windows;
    procedure MybuttonClick(Sender:TObject);
    implementation


    procedure MybuttonClick(Sender:TObject);
    begin
    MessageBox(0,PChar('AAA'),PChar('提示'),MB_OK);

    end;
    end.

    -------------------------------Unit2结束

  • 相关阅读:
    JAVA JDK配置
    jsoncpp的使用
    VS2015 +Qt5 串口工具
    Unable to convert MySQL date/time value to System.DateTime问题解决方案
    datagridview的一些设置
    C# 如何使用长度来切分字符串
    (备忘)打开office2010总是在配置进度
    (备忘)卸载微软自带输入法
    (备忘)怎么去除WinRAR弹窗广告?
    winfrom弹出窗口用timer控件控制倒计时20秒后关闭
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14771824.html
Copyright © 2011-2022 走看看