1、以模式表單方式,彈出一個子表單;
2、當滑鼠在子表單上移動時,將滑鼠的位置、表單位置及當前時間以訊息(同步)的方式顯示在主表單的Caption中;
知識點:用sendmessage發送記錄結構。
uj5u.com熱心網友回復:
建議用 PostMessage 方法,不用阻塞,訊息中可以用 LParam 傳遞一個指標指向要傳遞的結構,不過,既然是同一個行程里的,也可以直接訪問主查體的Handle然后發送SetWindowText訊息直接顯示出來啦uj5u.com熱心網友回復:
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.
uj5u.com熱心網友回復:
supermantm,你好,我怎么出錯了?真郁悶!出現如下錯誤:Undeclared identifier: 'Form1Handle',另外最關鍵的是用:用sendmessage發送記錄結構。uj5u.com熱心網友回復:
你在Unit1單元的implementation要uses Unit2哦,要不這個識別符號在Unit1是無效的。
你不是要讓Form1的caption上顯示那些資訊嗎?能夠直接setWindowText為什么還非要sendMessage ?
如果是作業性質的話,那么請獨立完成,如果是你遇到應用問題,我上面的代碼是測驗過的
uj5u.com熱心網友回復:
用sendmessage發送記錄結構。uj5u.com熱心網友回復:
二、訊息管理1、以模式表單方式,彈出一個子表單;
2、當滑鼠在子表單上移動時,將滑鼠的位置、表單位置及當前時間以訊息(同步)的方式顯示在主表單的Caption中;
知識點:用sendmessage發送記錄結構。
用try 創建一個子表單,在子表單中完成1、2;另外用用sendmessage發送記錄結構。
uj5u.com熱心網友回復:
你好,我運行了你寫的程式,在form1能夠顯示,但是這些資訊應該顯示在form2,不是form1,另外用記錄創建顯示變數。必須用sendmessage來執行uj5u.com熱心網友回復:
正需要,學習來了uj5u.com熱心網友回復:
如果不使用SendMessage而是使用PostMessage的話,可以在發送資料時分配記憶體, 接受到訊息并處理后釋放記憶體,效果和SendMessage一樣。uj5u.com熱心網友回復:
直接上代碼,屬于技術點代碼,代碼不做規范思路:把子表單滑鼠位置以及表單位置時間等存入結構體,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.
uj5u.com熱心網友回復:
這種型別該用回呼函式。uj5u.com熱心網友回復:
這個真的沒有必要用訊息,因為Form1、Form2的代碼都在主執行緒中運行,直接設定Form1.Caption就可以了。uj5u.com熱心網友回復:
直接設定Form1.Caption 本質上也是給 MainForm.Handle 發送了個 SetWindowText 的訊息嘛
uj5u.com熱心網友回復:
1. 直接設定Form1.Caption更簡單2. 直接設定Form1.Caption可以跨平臺,SetWindowText不可以
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/41308.html
標籤:VCL組件開發及應用
上一篇:snap服務端和客戶端如何聯調
下一篇:小白求助 希望大神們可以幫幫我
