Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#9

AW: Windows Messages in einem (TThread-)Thread empfangen

  Alt 3. Feb 2011, 13:50
probiers mal so
Delphi-Quellcode:
unit Unit2;

interface

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

type
  TMyThread = class(TThread)
  private
    FWNDHandle: Cardinal;
    procedure WndProc(var Msg: TMessage);
  protected
    procedure Execute; override;

  public
    Constructor Create(CreateSuspended: Boolean);Reintroduce;overload;
    Constructor Create; Reintroduce;overload;
    Destructor Destroy; override;
    property GetWNDHandle: Cardinal read FWNDHandle;
  end;

  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
  FMyThread :TMyThread;
    { Private-Deklarationen }
  public

  Fmsg:TMessage;
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}
constructor TMyThread.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);
  FWNDHandle := AllocateHWnd(WndProc);
end;

constructor TMyThread.Create;
begin
  inherited Create;
  FWNDHandle := AllocateHWnd(WndProc);
end;
destructor TMyThread.Destroy;
begin
  DeallocateHWnd(FWNDHandle);
  inherited;
end;

procedure TMyThread.Execute;
begin


  while not Terminated do
  begin
    Sleep(10);
  end;


end;

procedure TMyThread.WndProc(var Msg: TMessage);
begin
  OutputDebugString(PWideChar('TMyThread.WndProc - Msg: #' + IntToStr(Msg.Msg) + '-'+ IntToStr(Msg.WParam)));
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  FMyThread := TMyThread.Create(false);
end;

procedure TForm2.Button2Click(Sender: TObject);

begin
  Fmsg.Msg := 12345;
  FMsg.WParam := 2345;
  SendMessage(FMyThread.GetWNDHandle,FMsg.Msg,FMsg.WParam,FMsg.LParam);
end;

end.
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat