카테고리 없음
[시스템] Message Queue에 특정 메시지가 있는지 검사
정보모음1
2023. 9. 14. 07:41
반응형
// 실행방법:
// '시작' 버튼을 클릭하면 타이머가 1초 간격으로 발생하며
// 이때 시스템적으로 WM_TIMER 메시지(시간 간격이 지났음을 표시)가
// 발생하는데 이 WM_TIMER 가 message queue 에 있는지 검사하는
// 예제입니다
// 단, Application.ProcessMessages 때문에 WM_TIMER 가 처리될 수
// 있으므로 타이머 발생횟수와 WM_TIMER 발견 횟수는 다를 수 있습니다
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
FStop: Boolean;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 1000;
Button1.Caption := '시작';
Button2.Caption := '중지';
Label1.Caption := '0';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Msg: TMsg;
begin
Timer1.Enabled := True;
FStop := False;
while True do
begin
if FStop then
break;
// Application.ProcessMessages 에 의해 WM_TIMER 가 처리될 수 있음
Application.ProcessMessages;
// 특정 메시지가 queue에 있는지 검사한다
// WM_TIMER: 타이머의 시간 간격이 지났을때 발생되는 메시지
if PeekMessage(Msg,
0, // all window
WM_TIMER, // search only WM_TIMER (first message)
WM_TIMER, // search only WM_TIMER (last message)
PM_NOREMOVE) then // 검사만 하는 것으로 제거하지는 않는다
begin
// queue에서 WM_TIMER 메시지를 발견한 횟수
Label1.Caption := IntToStr(StrToIntDef(Label1.Caption, 0) + 1);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled := False;
FStop := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
MessageBeep(0);
end;
end.
반응형