[英]Can I use my own message loop in windows?
I am building a C++ program, on windows, using Visual Studio. 我正在使用Visual Studio在Windows上构建一个C ++程序。 It relies on a COM base API, that sends windows message for notification.
它依赖于COM基本API,它发送Windows消息以进行通知。
To process those messages, I see two possibilities: 为了处理这些消息,我发现了两种可能性:
I don't know what is best, or if there is another way to process the messages (there is probably a windows function that can launch the loop) 我不知道什么是最好的,或者是否有另一种方法来处理消息(可能有一个可以启动循环的Windows函数)
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Yes, you can. 是的你可以。 Every thread can have one message loop and you don't need any windows to receive messages or send them (see
PostThreadMessage
). 每个线程都可以有一个消息循环,您不需要任何窗口来接收消息或发送它们(请参阅
PostThreadMessage
)。
There is nothing wrong with using this method if your application is event-driven. 如果您的应用程序是事件驱动的,则使用此方法没有任何问题。
It is not just for your own benefit, COM requires you to create a message loop. 它不仅仅是为了您自己的利益,COM 要求您创建一个消息循环。 COM needs it to handle apartment threaded COM servers, an expensive word for "components that don't support multi-threading".
COM需要它来处理公寓线程COM服务器,这是“不支持多线程的组件”的一个昂贵的词。 The vast majority of them don't.
绝大多数人没有。
It is best to create a window, it doesn't have to be visible. 最好创建一个窗口,它不必是可见的。 That gives you a HWND that you can use in your SendMessage() calls.
这为您提供了一个可以在SendMessage()调用中使用的HWND。 The window procedure you write can process the messages.
您编写的窗口过程可以处理消息。 From there, it gets to be easy to create a minimal user interface, with Shell_NotifyIcon for example.
从那里,可以很容易地创建一个最小的用户界面,例如Shell_NotifyIcon。 Always nice when you can display a notification when something goes wrong.
当出现问题时可以显示通知,总是很好。 So much better then an event in a log that nobody ever looks at.
比日志中没有人看过的事件好得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.