본문 바로가기
Delphi Tip/Windows

델파이 윈도우 메세지 받는 방법

by MonoSoft 2021. 8. 11.
728x90
반응형

델파이 윈도우 메세지 받는 방법

 

- 보내는 통신규약은 아래와 같다고 가정.

SendMessage(FindWindow(nil, 'frmDebug'), WM_USER+123, 0, lParam(LongInt(메세지)));

혹은 PostMessage

 

1. TApplicationEvents 이용
2. 폼에디터에 TApplicationEvents 를 하나 박아넣음
3. 박아넣은 TApplicationEvents 의 OnMessage 함수를 만든다

 

아래와 같은 모양으로.

 

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);

begin

if Msg.message=WM_USER+123 then begin

Memo1.Lines.Add(PChar(Msg.lParam));

end;

end;

 

특징

- F1 을 눌러서 헬프를 보면 알 수 있지만,

SendMessage 로 보낸 건 못받아먹음.

PostMessage 로 해야 함.

 

메세지별 이벤트 핸들러 이용

- 각 메세지별 이벤트 핸들러를 만든다. 아래의 모양으로.

 

<선언부>

procedure WM_USER123(var MSG: TMessage); message WM_USER+123;

 

<구현부>

procedure TForm1.WM_USER123(var MSG: TMessage);

begin

Memo1.Lines.Add(PChar(MSG.LParam));

end;

 

 

TForm 의 WndProc 을 오버라이딩해서 사용하는 방법

- 아래와 같은 모양으로 구성.

<선언부>

procedure WndProc(var Message: TMessage); override;

 

<구현부>

procedure TForm1.WndProc(var Message: TMessage);

begin

if Message.Msg=WM_USER+123 then begin

Memo1.Lines.Add(PChar(Message.LParam));

end;

inherited;

end;

728x90
반응형

댓글