如何在主进程内向线程发消息,线程里又如何接收???
恳请给个简单的例子。
恳请给个简单的例子。
解决方案 »
- 如何让memo控件有canvas方法呢??
- 小小项目需要软件界面设计,内行请进来留下联系方式
- 有在太原工作的兄弟吗?请进
- 小弟我真的真的没有可用分,只能深深的祝大家身体健康,事业进步
- 请问如何删除新建的delphi工程自带的资源图片内容?
- 控制面板中的删除,更改按钮被禁
- 如何剪裁JPG图片的某一部分并保存成到文件?
- InstallShield的安装程序中,要安装MSDE(SQL SERVER桌面板),如何实现??
- 查询一张表所得结果存到同一个数据库另一张表中,来不成功!!??
- 请delphi高手帮忙改错
- 百分求解!!!关于在StringGrid控件中拖放的问题
- 宏调用--调用另一FORM上的控件,使其动作(在线等)
然后用sendmessage(handle,...)或postmessage()接收消息简单的你可以直接重载WndPro函数
procedure WndProc(var Message: TMessage); override;对于线程里也是一样的 ,你可以用主窗体来接收消息
线程单元:
unit Unit2;interfaceuses
Windows, Messages, Classes;const
//自军定义消息;
MSG_MYMSG = WM_USER + 100;type
TMyThread = class(TThread)
private
FMainHandle: THandle; //调用本线程的主线程句柄;
protected
procedure Execute; override;
public
constructor Create(AHandle: THandle);
end;implementation{ TMyThread }constructor TMyThread.Create(AHandle: THandle);
begin
FMainHandle := AHandle;
inherited Create(True);
end;procedure TMyThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate := True;
while not Terminated do
begin
//这里放你的处理;
if FMainHandle <> 0 then //反馈进度;
PostMessage(FMainHandle, MSG_MYMSG, 0, 0);
end;
end;end.窗体单元:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit2;type
TForm1 = class(TForm)
private
protected
procedure WndProc(var Message: TMessage); override;
public
end;var
Form1: TForm1;implementation{$R *.DFM}{ TForm1 }{ TForm1 }procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg = MSG_MYMSG then
begin
//在这里处理消息;
end
else inherited;
end;end.
idThread: DWORD; 目标线程的ID;
Msg: UINT; 要发送的消息的数值;(一般是以WM_开头的常量)
wParam: WPARAM; 第一个消息参数(其值和具体的消息有关);
lParam: LPARAM 第二个消息参数(其值和具体的消息有关);例子:在SvcMgr.pas单元中:
procedure TService.Controller(CtrlCode: DWord);
begin
PostThreadMessage(ServiceThread.ThreadID, CM_SERVICE_CONTROL_CODE, CtrlCode, 0);
if ServiceThread.Suspended then ServiceThread.Resume;
end;PeekMessage:用于从消息队列中获取一个消息,一般用于程序的主消息循环;
lpMsg: TMsg; 指向消息结构的指针;
hWnd: HWND; 窗口句柄,如果指定,则只接收指定窗口的消息;
wMsgFilterMin,wMsgFilterMax: UINT 指定接收消息范围,如指定则只接收指定范围内的消息;
wRemoveMsg: UINT 消息处理标志,指定消息处理后是否从队列中移去。例子:Forms.pas单元:
function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end
else
FTerminate := True;
end;
end;注:用PostThreadMessage发送的消息只能用GetMessage才可以接收到。