在TThread中创建一个窗口 [英] Creating a Window Inside TThread

查看:122
本文介绍了在TThread中创建一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在两个独立的项目之间发送消息,但我的问题是,我试图使接收器运行在一个TThread对象中,但是WndProc不会从一个对象内部工作,必须是一个函数,是否有创建一个可以在线程内处理邮件的TThread中的一个窗口?

im trying to send a message between 2 separate projects, but my problem is that im trying to make the receiver run inside a TThread Object, but WndProc wont work from inside an Object, must be a function, is there anyway to create a window inside a TThread that can process messages inside the thread?

这里是我的意思

function TDataThread.WindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
 Result := 0;
 case uMsg of
   WM_DATA_AVA: MessageBox(0, 'Data Avaibale', 'Test', 0);
  else Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
 end;
end;

Procedure TDataThread.Create(const Title:String);
begin
 HAppInstance := HInstance;
 with WndClass do
 begin
  Style := 0;
  lpfnWndProc := @WindowProc;          //The Error Lies here (Variable Required)
  cbClsExtra := 0;
  cbWndExtra := 0;
  hInstance := HAppInstance;
  hIcon := 0;
  hCursor := LoadCursor(0, IDC_ARROW);
  hbrBackground := COLOR_WINDOW;
  lpszMenuName := nil;
  lpszClassName := 'TDataForm';
 end;
 Windows.RegisterClass(WndClass);
 MainForm := CreateWindow('TDataForm', PAnsiChar(Title), WS_DLGFRAME , XPos, YPos, 698, 517, 0, 0, hInstance, nil);
end;

我需要一个表单,所以我可以从另一个应用程序获取它的句柄如果需要,使用FindWindow和FindWindowEx

i need to have a form so i can get its handle from another application Using FindWindow and FindWindowEx if needed

推荐答案

在后台线程中运行wndproc可以在Win32中完成,但被广泛认为是一个坏主意。

Running a wndproc in a background thread can be done in Win32, but it's widely regarded as a bad idea.

为此,您必须确保您的后台线程包含消息分派循环:GetMessage / TranslateMessage / DispatchMessage。您必须确保在后台线程中处理消息的窗口句柄将在后台线程(后台线程的上下文中调用CreateWindow)及其所有子窗口中创建。而且你必须确保你的后台线程经常调用它的消息循环,除了正在做的任何事情之外(哪一种都打败了使用后台线程的目的!)

To do it, you must ensure that your background thread contains a message dispatch loop: GetMessage/TranslateMessage/DispatchMessage. You must ensure that the window handle you want to process messages in the background thread is created on the background thread (CreateWindow is called in the context of the background thread) and all its child windows as well. And you must ensure that your background thread calls its message loop frequently in addition to whatever else it's doing (which kinda defeats the purpose of using a background thread!)

如果你的后台线程没有消息循环,在后台线程上创建的窗口句柄将永远不会收到任何消息,所以不会发生任何事情。

If your background thread doesn't have a message loop, the window handles that are created on the background thread will never receive any messages, so nothing will happen.

现在,为什么您不应该这样做:Windows是消息驱动的,这意味着它们本质上是一个合作的多任务调度系统。每个GUI窗口应用程序必须在主线程中有一个消息循环才能完成任务。该消息循环将支持几乎任何数量的窗口,全部在主线程上。正确实现的UI在主线程中不会执行任何阻止执行的操作,因此消息循环将始终准备就绪并响应。

Now then, why you shouldn't do this: Windows are message-driven, which means they are inherently a cooperatively multitasked dispatch system. Every GUI windows app has to have a message loop in the main thread to get anything done. That message loop will support virtually any number of windows, all on the main thread. A properly implemented UI will not do anything in the main thread to block execution, so the message loop will always be ready and responsive.

所以如果现有的消息循环主线程将处理您的所有窗口消息传递需求,而不会阻塞或冻结,为什么要通过在后台线程中运行第二条消息循环来使您的生活变得更加复杂?使用后台线程没有任何优势。

So if the existing message loop on the main thread will handle all your window messaging needs without blocking or freezing, why would you want to make your life more complicated by trying to run a second message loop in a background thread? There is no advantage to using a background thread.

这篇关于在TThread中创建一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆