Einzelnen Beitrag anzeigen

Benutzerbild von Sprint
Sprint

Registriert seit: 18. Aug 2004
Ort: Edewecht
712 Beiträge
 
Delphi 5 Professional
 
#3

Re: String von Thread an Hauptformular schicken

  Alt 21. Mai 2005, 01:45
Zitat von mumu:
ich möchte einen string aus meinem thread an mein hauptformular übergeben und dort weiterarbeiten.
Delphi-Quellcode:
unit Unit1;

interface

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

const
  WM_USER_THREADMESSAGE = WM_USER + $101;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure WMUserThreadMessage(var Message: TMessage); message WM_USER_THREADMESSAGE;
  public
    { Public-Deklarationen }
  end;

  TMyThread = class(TThread)
  private
    FMainFormHandle: HWND;
  protected
    procedure Execute; override;
  public
    property MainFormHandle: HWND read FMainFormHandle write FMainFormHandle;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{--------------------------------------------------------------------------------------------------}

{ TMyThread }

procedure TMyThread.Execute;
var
  S: String;
begin
  S := 'Hello World!';
  SendMessage(FMainFormHandle, WM_USER_THREADMESSAGE, Length(S), LPARAM(PChar(S)));
end;

{--------------------------------------------------------------------------------------------------}

{ TForm1 }

procedure TForm1.WMUserThreadMessage(var Message: TMessage);
var
  S: String;
begin
  SetLength(S, Message.WParam);
  StrLCopy(PChar(S), PChar(Message.LParam), Message.WParam);
  ShowMessage(S);
end;

{--------------------------------------------------------------------------------------------------}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TMyThread.Create(True) do
  begin
    FreeOnTerminate := True;
    MainFormHandle := Self.Handle;
    Resume;
  end;
end;

{--------------------------------------------------------------------------------------------------}

end.
Ciao, Sprint.

"I don't know what I am doing, but I am sure I am having fun!"
  Mit Zitat antworten Zitat