简体   繁体   English

我可以在Windows中使用自己的消息循环吗?

[英]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: 为了处理这些消息,我发现了两种可能性:

  • Create a windows form and call doModal on it which should process the messages, but since I don't want to use any UI, it's not what I want to do 创建一个Windows窗体并在其上调用doModal来处理消息,但由于我不想使用任何UI,这不是我想要做的
  • make my own loop for processing messages 制作我自己的循环来处理消息

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.

相关问题 我应该使用DirectInput还是Windows消息循环? - Should I use DirectInput or Windows message loop? 我可以在自己的应用程序中使用Windows资源管理器中的搜索框控件吗? - Can I use the search box control seen in Windows Explorer in my own application? 如何在Windows中编写自己的“文件系统”? - How can I write my own 'filesystem' within Windows? 使用Windows消息循环在我正在编写的库中接收事件 - Use Windows message loop to receive an event in a library I'm writing 我可以在'setDefaultButton'中使用自己的用户定义的字符串吗 - Can i use my own user defined string in 'setDefaultButton' 当我可以使用默认的TimerQueue时,为什么要使用自己的TimerQueue? - Why should I use my own TimerQueue when I can use the default one? 如何在我的主程序c ++中使用我自己制作的Array头文件? - How can I use my own made Array header file in my main program c++? 什么时候应该使用自己的名称空间? - When I should use my own namespaces? 如何使用我从源代码构建的库而没有错误,但不能为我自己的项目编译? - How can I use the library that I have built without error from source, but not compiling for my own project? WinAPI - 带有自己回调的消息循环 - WinAPI - message loop with own callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM