waitmessageu.pas

来自「Delphi Win32核心API参考光盘源码 本书包含了常用的Windows」· PAS 代码 · 共 61 行

PAS
61
字号
unit WaitMessageU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{this function places the application into a message loop
 that will break only when the left mouse button is clicked
 on the client area of the form.  the user will be unable to
 resize or move the form until then.}
procedure TForm1.Button1Click(Sender: TObject);
var
  TheMessage: TMSG;       // holds message info
  MouseClicked: boolean;  // general loop control variable
begin
  {initialize the loop control variable}
  MouseClicked := FALSE;

  {place the application into a loop until a mouse button is clicked}
  while not MouseClicked do
  begin
    {empty the message queue}
    while PeekMessage(TheMessage, Handle, 0, 0, PM_REMOVE) do;

    {suspend the thread until a new message is placed in the queue}
    WaitMessage();

    {a new message has just dropped into the queue. retrieve it.}
    PeekMessage(TheMessage, Handle, 0, 0, PM_REMOVE);

    {if the new message was a mouse click, break out of the loop}
    if TheMessage.Message=WM_LBUTTONDOWN then
    begin
      MouseClicked := TRUE;
      {indicate that the message was a mouse click}
      ShowMessage('A message was received, resume execution');
    end;
  end;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?