⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postthreadmessageu.pas

📁 DelphiWin32核心API参考光盘内容.是学习书籍中的源码,便于学习.
💻 PAS
字号:
unit PostThreadMessageU;

interface

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

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

const
  NewMessage = WM_USER+1;     // a new user defined message

var
  Form1: TForm1;

implementation

{$R *.DFM}

function ThreadFunction(Parameter: Pointer): Integer; StdCall;
var
  DC: HDC;     // holds a device context
  Msg: TMsg;   // a message information structure
begin
  {create a message loop}
  while (GetMessage(Msg, 0, 0, 0)) do
  begin
    {if the retrieved message is our user defined message...}
    if Msg.Message = NewMessage then
    begin
      {...retrieve a handle to the device context}
      DC := GetDC(Form1.Handle);

      {set the background mode to be transparent}
      SetBkMode(DC, TRANSPARENT);

      {display text indicating that the message was received}
      TextOut(DC, 10, 10, 'User message seen by thread', 27);

      {release the device context}
      ReleaseDC(Form1.Handle, DC);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ThreadId: DWORD;    // holds the new thread ID
begin
  {create a new thread}
  ThreadHandle := CreateThread(nil, 0, @ThreadFunction, nil, 0, ThreadId);

  {make sure that the thread was created correctly}
  if ThreadHandle = 0 then
  begin
    ShowMessage('New thread not started');
    Halt;
  end;

  {pause for 100 milliseconds}
  Sleep(100);

  {post the user defined message to the thread}
  PostThreadMessage(ThreadId, NewMessage, 0, 0);
end;

end.

⌨️ 快捷键说明

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