技术总结

  1. 发送消息(PostMessage):

接收方和发送方都要定义参数:  Const  WM_UpProgress= WM_USER+27;

 

接收方:定义接收方法

procedure WM_UpProgress(var Msg: TMessage); message WM_USER+27;

方法实现:

procedure TfrmToolMain.WM_UpProgress(var Msg: TMessage);

var

  aCurrent, aCount: Integer;

  cancle:boolean;

begin

  //进度

  aCurrent:=msg.WParam;

  aCount :=msg.LParam;

  ProgressBar1.Position :=aCurrent*100 div aCount;

end;

 

发送方:发送消息: 发送方需要有接收方的handle.

if m_Handle > 0 then

    PostMessage(m_Handle,WM_UpProgress,aCurrent,aCount);

  1. 回调函数: A,B两个类, A:TForm类, B:Unit类 A中定义方法过程, 在B中调用执行.
    1. 首先要在B中(或共用类)中定义一个事件类型(TProgressFunc): 用于接收A中的方法.如下:
      TProgressFunc = reference to procedure(aInfo: String; aCurrent: Integer; aCount: Integer;

                 var cancle: Boolean);

  1. 在A中定义方法过程: 参数要和第一步中的参数完全一致.
    Procedure OnProgress(aInfo: String; aCurrent: Integer; aCount: Integer;var cancle: Boolean);
    方法实现:
    Procedure TfrmToolMain.OnProgress(aInfo: String; aCurrent: Integer; aCount: Integer;var cancle: Boolean);

var

  percent:word;

begin

  percent := aCurrent * 50 div aCount;

  ProgressBar1.Position := percent;

end;

  1. 在B中定义TProgressFunc类型的变量,用来接收A传过来的方法(OnProgress).

ProgressFunc:TProgressFunc;

  1. 赋值:  B. ProgressFunc := A. OnProgress;//不能加参数
    或者在实例化B时通过参数传递: b := TB.Create(OnProgress);//类型: TProgressFunc
  2. 创建自身对象,提供方法给外部使用.
    public class function LogIn():Boolean;

实现:

 

class function TfrmLogin.LogIn: Boolean;

begin

  //frmLogin :=TfrmLogin.Create(nil);

  if frmLogin.ShowModal()=mrOk then

    Result :=True

  else

    Result :=False;

end;

//外部调用

if TfrmLogin.LogIn()=False then

  begin

    Application.Terminate;

    exit;

  end;

 

  1.  

 

posted on 2018-09-14 10:19  liuweijie  阅读(140)  评论(0)    收藏  举报