Delphi窗體間傳送訊息或字串

2021-04-25 05:40:06 字數 1476 閱讀 4303

自定義過程/函式方法:

//傳送字串到指字句柄的視窗中 (接收窗體需用傳送時的訊息常量wm_copydata)

procedure sendmessagedata(ahandle: thandle; astr: string);

varsdata: tcopydatastruct;

begin

sdata.cbdata := length(astr) + 1;

//為傳遞的資料分配記憶體

getmem(sdata.lpdata,sdata.cbdata );

trystrcopy(sdata.lpdata,pchar(astr));

//傳送wm_copydata訊息

sendmessage(ahandle,wm_copydata,0,cardinal(@sdata));

finally

freemem(sdata.lpdata); //釋放資源

end;

end; 

//傳送訊息到指字句柄的視窗中

procedure sendmymessage(ahandle: thandle; amsg:word);

begin

sendmessage(ahandle,amsg,0,0)

end;

//------------------

呼叫:sendmessagedata(tform(findglobalcomponent('frmhint')).handle,'abc');

sendmymessage(tform(findglobalcomponent('frmhint')).handle, wm_user+1001);

'frmhint' 是你想要接收訊息的窗體的name 

//******************************===

在接收的窗體中定義如下常量及過程

const

wm_mydefmsg = wm_user+1001;

宣告部分:

procedure wmcopydata(var amsg: twmcopydata); message wm_copydata;

procedure receiedformmsg(var msg :tmsg); message wm_mydefmsg; 

過程實體:

//接收傳送的字串,即預定訊息wm_copydata

procedure wmcopydata(var amsg: twmcopydata);

var s : string;

begin

s := strpas(amsg.copydatastruct^.lpdata);

//to do something

end;

//接收預定的訊息

procedure receiedformmsg(var msg: tmsg);

begin

//make you want to do

end;

子類(子窗體)向主窗體傳送訊息

這裡演示的是,在主窗體 塊中,重寫攔截資訊方法。首先新建乙個類 這個類名定義為msg dllimport user32.dll entrypoint sendmessage private static extern intptr sendmessage int hwnd,int msg,intpt...

c 向MFC窗體傳送訊息

using system using system.collections.generic using system.linq using system.text using system.threading.tasks using system.runtime.interopservices na...

程序間傳遞訊息(傳送和接收系統訊息)

unit unit1 inte ce uses windows,messages,sysutils,variants,classes,graphics,controls,forms,dialogs,stdctrls type tform1 class tform button1 tbutton pr...