Trap WM_COPYDATA from component.

Please discuss general Delphi programming topics here.

Trap WM_COPYDATA from component.

Postby Gome206 » February 1st, 2012, 2:22 pm

Hi!

I'm trying to write a component, to send string messages between applications by WM_COPYDATA.
I'd like trap the WM_COPYDATA, but isn't work like this:


TMyMessage = class(TComponent)
private
{ Private declarations }

protected
{ Protected declarations }

procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;

end;

Searching Google a lot, found some reference using wndproc. I tried it, but isn't working too.

TMyMessage = class(TComponent)

protected
{ Protected declarations }

procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;
procedure WndProc(var Msg: TMessage);

end;

procedure TMyMessage.WndProc(var Msg: TMessage);
var
p: PCopyDataStruct;
begin
//inherited;
if Msg.Msg = WM_COPYDATA then
WMCopyData(Msg);
end;

Please help, what is wrong? ](*,)

Göme206
Gome206
Member
Member
 
Posts: 1
Joined: February 1st, 2012, 2:13 pm

Re: Trap WM_COPYDATA from component.

Postby lexdean » February 1st, 2012, 10:30 pm

thier is a project on torry pages on your subject
except windows is not good at handling delphi objects

Lex Dean
lexdean
Active Member
Active Member
 
Posts: 16
Joined: May 25th, 2011, 12:04 am

Re: Trap WM_COPYDATA from component.

Postby Kambiz » February 4th, 2012, 9:30 am

Do not override WndProc. Delphi automatically calls WMCopyData, it doesn't need to be called manually inside the WndProc.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Trap WM_COPYDATA from component.

Postby lexdean » July 18th, 2012, 11:12 pm

I made my self such a Component
its has a server /terminal arrangement.
The component removes most the code from the form's code.

It transports a ID integer and a reference with ether:-
a string and integer for data
or a stream and transport pictures between programs

The ID integer is to remove broadcast copy messages from being received

I have made a execute and wait for the EXE to be installed

It’s on Tory pages.
lexdean
Active Member
Active Member
 
Posts: 16
Joined: May 25th, 2011, 12:04 am

Re: Trap WM_COPYDATA from component.

Postby arhangelsoft » July 19th, 2012, 3:05 pm

WM_ messages could be handled only by windows.
Your component need to create a window and organize message cycle for it and fork with it.

See my example:
Code: Select all
uses windows, messages;

var   
  wndClass: TWndClassEx;
  wndMain: HWND;
  wndMC: TMsg; //Message for my windows messages cycle


//Message handler
function WindowProc(wnd:HWND; Msg : Integer; Wparam:Wparam; Lparam:Lparam):Lresult;
 stdcall;
Begin

case msg of
wm_destroy :
  Begin
   PostQuitMessage(0); Exit;
   Result:=0;
  End;

  else Result:=DefWindowProc(wnd,msg,wparam,lparam);
end;

End;

var xPos,yPos,nWidth,nHeight : Integer;

begin
wndClass.cbSize:=sizeof(TWndClassEx);
wndClass.style:=cs_hredraw or cs_vredraw;
wndClass.lpfnWndProc:=@WindowProc;
wndClass.cbClsExtra:=0;
wndClass.cbWndExtra:=0;
wndClass.hInstance:=HInstance;
wndClass.hIcon:=LoadIcon(0,idi_application);
wndClass.hCursor:=LoadCursor(0,idc_arrow);
wndClass.hbrBackground:=COLOR_BTNFACE+1;
wndClass.lpszMenuName:=nil;
wndClass.lpszClassName:='TMP: Window';

RegisterClassEx(wndClass);


xPos:=100;
yPos:=150;
nWidth:=400;
nHeight:=250;


wndMain:=CreateWindowEx(0,'TMP: Window','Window',ws_overlappedwindow,xPos,yPos,nWidth,nHeight,0, 0,Hinstance,nil);
ShowWindow(MwndMain,CmdShow);

//message cycle
While GetMessage(wndMC,0,0,0) do
begin
  TranslateMessage(wndMC);
  DispatchMessage(wndMC);
end;

end.


Only what is need.
Description of used functions you can found on MSDN WinAPI reference

Why you don't use Memory Mapped Files? Window doesn't need it.
arhangelsoft
Junior Member
Junior Member
 
Posts: 39
Joined: February 16th, 2012, 9:56 pm
Location: Belarus, Minsk

Re: Trap WM_COPYDATA from component.

Postby lexdean » July 29th, 2012, 10:39 pm

go to about.com WM_COPYDATA
and start from scrach
lexdean
Active Member
Active Member
 
Posts: 16
Joined: May 25th, 2011, 12:04 am

Re: Trap WM_COPYDATA from component.

Postby dejavu.cucud » May 8th, 2013, 10:51 pm

simply, you can use SendMessage to send string into any destination window (on same application or different application). fill the wParam with any TEdit.Handle or another Text Control's handle.

const
{ User-defined message }
WM_EXCHANGE_TEXT = WM_USER + 2;

SendMessage(hWndDestination, Edit1.Handle, 0);

on the destination window, create a custom message method to catch the message. use GetWindowText() function, and fill HWND/handle parameter with wParam.

private
procedure ExchangeText(var Msg: TMessage); message WM_EXCHANGE_TEXT;

procedure TFormDest.ExchangeText(var Msg: TMessage);
var data: PChar;
begin
GetMem(data, 1024);
GetWindowText(Msg.wParam, data, 1024);
ShowMessage(StrPas(data));
FreeMem(data);
end;
dejavu.cucud
Active Member
Active Member
 
Posts: 6
Joined: May 8th, 2013, 6:06 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron