关于delphi 消息管理

weixin_39864128 2018-03-25 03:34:56
1、以模式窗体方式,弹出一个子窗体;
2、当鼠标在子窗体上移动时,将鼠标的位置、窗体位置及当前时间以消息(同步)的方式显示在主窗体的Caption中;
知识点:用sendmessage发送记录结构。
...全文
1201 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
1. 直接设置Form1.Caption更简单 2. 直接设置Form1.Caption可以跨平台,SetWindowText不可以
SupermanTm 2018-06-15
  • 打赏
  • 举报
回复
引用 12 楼 DelphiGuy 的回复:
这个真的没有必要用消息,因为Form1、Form2的代码都在主线程中运行,直接设置Form1.Caption就可以了。
直接设置Form1.Caption 本质上也是给 MainForm.Handle 发送了个 SetWindowText 的消息嘛
hj8090 2018-06-13
  • 打赏
  • 举报
回复
这种类型该用回调函数。
  • 打赏
  • 举报
回复
这个真的没有必要用消息,因为Form1、Form2的代码都在主线程中运行,直接设置Form1.Caption就可以了。
天行归来 2018-06-12
  • 打赏
  • 举报
回复
直接上代码,属于技术点代码,代码不做规范 思路:把子窗体鼠标位置以及窗体位置时间等存入结构体,sendMessage直接传结构体指针到主窗体。 主窗体代码

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure OnFormPositionMessage(var msg: TMessage); message WM_FORM_INFO;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  fm: TForm2;
begin
  try
    fm := TForm2.Create(self);
    fm.ShowModal;
  finally
    fm.Free;
  end;
end;

procedure TForm1.OnFormPositionMessage(var msg: TMessage);
var
  pos: TFormPosition;
begin
  pos := PFormPosition(msg.WParam)^;
  self.Caption := Format('mouseX:%d,mouseY:%d',[pos.mouseX,pos.mouseY]);
  //显示更多内容自己加即可
end;

end.
子窗体代码

unit Unit2;

interface

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

const
  WM_FORM_INFO = WM_USER+100;

type
  PFormPosition = ^TFormPosition;
  TFormPosition = record
    mouseX  : integer;
    mouseY  : integer;
    formTop : integer;
    formLeft: integer;
    nowTime : TDateTime;
  end;

type
  TForm2 = class(TForm)
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  pos: TFormPosition;
begin
  pos.mouseX  := x;
  pos.mouseY  := y;
  pos.formTop := self.Top;
  pos.formLeft:= self.Left;
  pos.nowTime := Date();
  SendMessage(Application.MainFormHandle,WM_FORM_INFO,Integer(@pos),0);
end;

end.
huixch 2018-06-11
  • 打赏
  • 举报
回复
如果不使用SendMessage而是使用PostMessage的话,可以在发送数据时分配内存, 接受到消息并处理后释放内存,效果和SendMessage一样。
weixin_39864128 2018-03-26
  • 打赏
  • 举报
回复
你好,我运行了你写的程序,在form1能够显示,但是这些信息应该显示在form2,不是form1,另外用记录创建显示变量。必须用sendmessage来执行
weixin_39864128 2018-03-26
  • 打赏
  • 举报
回复
二、消息管理 1、以模式窗体方式,弹出一个子窗体; 2、当鼠标在子窗体上移动时,将鼠标的位置、窗体位置及当前时间以消息(同步)的方式显示在主窗体的Caption中; 知识点:用sendmessage发送记录结构。 用try 创建一个子窗体,在子窗体中完成1、2;另外用用sendmessage发送记录结构。
weixin_39864128 2018-03-26
  • 打赏
  • 举报
回复
用sendmessage发送记录结构。
sanyuan35 2018-03-26
  • 打赏
  • 举报
回复
正需要,学习来了
SupermanTm 2018-03-26
  • 打赏
  • 举报
回复
引用 3 楼 weixin_39864128 的回复:
supermantm,你好,我怎么出错了?真郁闷!出现如下错误:Undeclared identifier: 'Form1Handle',另外最关键的是用:用sendmessage发送记录结构。
你在Unit1单元的implementation要uses Unit2哦,要不这个标识符在Unit1是无效的。 你不是要让Form1的caption上显示那些信息吗?能够直接setWindowText为什么还非要sendMessage ? 如果是作业性质的话,那么请独立完成,如果是你遇到应用问题,我上面的代码是测试过的
weixin_39864128 2018-03-25
  • 打赏
  • 举报
回复
supermantm,你好,我怎么出错了?真郁闷!出现如下错误:Undeclared identifier: 'Form1Handle',另外最关键的是用:用sendmessage发送记录结构。
SupermanTm 2018-03-25
  • 打赏
  • 举报
回复

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1Handle:= Self.Handle;
  Form2.ShowModal;
end;

end.

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  end;

var
  Form2: TForm2;
  Form1Handle: HWND;

implementation

{$R *.dfm}

procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  S: String;
begin
  S:= Format('Mouse:[%d,%d], Form2:[%d,%d,%d,%d], Time:', [X, Y, Left, Top, Width, Height]);
  S:= S + FormatDateTime('yyyy-mm-dd_hh:nn:ss', Now);
  SetWindowText(Form1Handle, PChar(S));
end;

end.
SupermanTm 2018-03-25
  • 打赏
  • 举报
回复
建议用 PostMessage 方法,不用阻塞,消息中可以用 LParam 传递一个指针指向要传递的结构,不过,既然是同一个进程里的,也可以直接访问主查体的Handle然后发送SetWindowText消息直接显示出来啦

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧