发送消息执行方法函数(消息函数)

1. 定义方法函数:
   procedure InitRunParam(var Msg: TMessage); message WM_USER + 1110; //参数类型是固定的; message WM_USER + 1110 数字可变.或定义为常量:
  const
    WM_REFRESH_STATE = WM_USER + 5;//需要引用 Winapi.Messages

2. 发送消息
PostMessage(Handle, WM_USER + 1110, 0, 0);//程序启动时通过发消息创建托盘图标
PostMessage(frmMain.handle,WM_MYMESSAGE,0,integer(pchar('hello ! 你好!')));//发送字符串参数

3. 方法函数实现:实现部分可以不加message WM_USER + 1110;这一句. 且参数可以不使用.

procedure TfrmMain.InitRunParam(var Msg: TMessage);
var i: Integer;
begin
  logCtrl.Debug(nil, 'TfrmMain.InitRunParam in');
  for i := 1 to 3 do
  begin
    try
      RzTrayIcon.Enabled := True;
      Break;
    except on e: Exception do
      begin
        RzTrayIcon.Enabled := False;
        logCtrl.Debug(nil, 'TfrmMain.InitRunParam err:' + e.Message + ' 次数:' + IntToStr(i));
        if i >= 3 then
        begin
          Application.MessageBox('考试端服务程序初始化失败,请重新启动程序!',
            '提示信息', MB_OK + MB_ICONSTOP + MB_DEFBUTTON2);
          Application.Terminate;
        end else Sleep(500);
      end;
    end;
  end;
  try
    dmDB.udpServer.Active := True;
  except on e: Exception do
    logCtrl.Debug(nil, 'TfrmMain.InitRunParam udpServer启动失败:' + e.Message );
  end;
  logCtrl.Debug(nil, 'TfrmMain.InitRunParam out');
end;

原文地址:https://www.cnblogs.com/weijie-liu/p/9644888.html