C++ MessageBox (Windows.h) - 它有什么作用以及如何使其不可见(或等效)?
当我遇到以下情况时:
MessageBox(NULL, NULL , "MessageBox", NULL);
出现一个消息框,我的程序按照我的期望运行,直到用户在消息框中单击“确定”,然后程序结束。我尝试放入无限循环以达到相同的效果,但这与消息框的工作方式不同。我不想要消息框的原因是它阻碍了用户对程序的查看,如果他们尝试关闭它,程序就会停止。所以我基本上只是想要一个隐形的消息框或具有相同效果的东西。
编辑:澄清一下,该程序是游戏的原型。我正在使用钩子来查找用户按下的键。这是该程序的简化版本:
#define WM_KEYDOWN 0x0100
#define _WIN32_WINNT 0x0500
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam){
PKBDLLHOOKSTRUCT keypointer = (PKBDLLHOOKSTRUCT)(lParam);
if (wParam==WM_KEYDOWN){
switch(keypointer->vkCode){
case VK_RIGHT:
cout << "**RIGHT**";
goto skip;
case VK_LEFT:
cout << "**LEFT**";
goto skip;
case VK_DOWN:
cout << "**DOWN**";
goto skip;
case VK_UP:
cout << "**UP**";
skip:
default:
cout << "";
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hInstance, 0);
MessageBox(NULL, NULL , "KLMBOX", NULL);
return 0;
}
我只想让该程序具有相同的功能,但不显示消息框!我不是专家,我只是在乱搞: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
When I have:
MessageBox(NULL, NULL , "MessageBox", NULL);
A message box comes up and my program works as I desire it to until the user clicks okay on the message box and then the program ends. I tried to put an infinite loop in to have the same effect, but this doesn't work in the same way as the message box does. The reason I don't want the message box is the fact that it obstructs the user's view of the program and if they try to close it then the program stops. So I basically just want to have an invisible message box or something with the same effect.
EDIT: To clarify, the program is a prototype for a game. I am using hooks to find what keys the user is pressing. Here is a simplified version of the program:
#define WM_KEYDOWN 0x0100
#define _WIN32_WINNT 0x0500
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam){
PKBDLLHOOKSTRUCT keypointer = (PKBDLLHOOKSTRUCT)(lParam);
if (wParam==WM_KEYDOWN){
switch(keypointer->vkCode){
case VK_RIGHT:
cout << "**RIGHT**";
goto skip;
case VK_LEFT:
cout << "**LEFT**";
goto skip;
case VK_DOWN:
cout << "**DOWN**";
goto skip;
case VK_UP:
cout << "**UP**";
skip:
default:
cout << "";
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hInstance, 0);
MessageBox(NULL, NULL , "KLMBOX", NULL);
return 0;
}
I just want the program to have the same functionality, but without displaying a message box! I'm not an expert I was just messing around with: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您正在寻找的是消息循环。 MessageBox() 在这里做了两件事:它显示对话框,但它还在内部提供自己的消息循环来处理对话框的输入。低级挂钩需要消息循环才能正常运行,并且您的代码可以与 MessageBox 配合使用,因为它提供的消息循环正在为您执行必要的消息处理。这就是为什么普通的无限循环不能作为替代品——它没有适当地处理消息。
这是一个可以剪切和粘贴的简单方法:
当您决定在代码中的其他位置(但仍在同一线程上)退出时 - 也许当您检测到挂钩中的某些组合键时 - 使用 PostQuitMessage();这将向线程队列发送一条 WM_QUIT 消息,当 GetMessage 检索该消息时,它将返回 0 并且循环将退出。
话虽如此,这首先并不是编写游戏的好方法。低级键盘挂钩太过分了,在这里并不合适。如果您想在 Windows 应用程序中获取键盘输入,最简单的方法就是创建自己的窗口,并且当按下/释放按键时它将接收 WM_KEYDOWN/UP 消息。
Sounds like what you are looking for is a message loop.
MessageBox()
is doing is two things here: it displays the dialog, but it's also supplying its own message loop internally to process input for the dialog. Low-level hooks require a message loop to function correctly, and your code works with MessageBox just because the message loop that it is supplying is doing the necessary message processing for you. And that's why a plain infinite loop doesn't work as a substitute - it's not processing the messages appropriately.Here's a simple one you can cut-and-paste:
When you decide elsewhere in your code (but still on the same thread) that you want to exit - perhaps when you detect some key combination in the hook - use PostQuitMessage(); this will post a WM_QUIT message to the thread's queue, and when GetMessage retrieves that, it will return 0 and the loop will exit.
Having said all of that, this isn't a good way to write a game in the first place; low-level keyboard hooks are overkill and not really appropriate here. If you want to get keyboard input in a windows app, the simplest thing to do is create your own window, and it will receive WM_KEYDOWN/UP messages as keys are pressed/released.
根据您的代码示例,您似乎正在尝试制作一款游戏,但其结构却完全错误。游戏通常有一个主循环,在每次迭代中您检查输入、更新游戏状态然后渲染。如果这是您想要的,我建议您研究现有的众多开放游戏引擎之一,并尝试一些输入教程。 OGRE 3d 可能是一个很好的起点,有很好的社区和很好的教程。
但是,如果这不是您想要的,并且您确实想使用钩子,则需要创建一个不同的线程。如果您创建无限循环,系统将永远无法抽出时间来处理挂钩。
也许做你想做的事情的最简单方法就是打开一个窗口并使其不可见。使用您现在正在使用的原始 win32 API 创建窗口涉及到并且需要相当多的代码来完成。这是一个教程,解释了所涉及的步骤。
但同样,如果您想制作一个简单的游戏,我建议利用现有的引擎。这样你就会取得更快的进步。祝你好运!
With your code sample, it looks like you're trying to make a game but structuring it all wrong. A game normally has a main loop in where each iteration you check the input, update the game state and then render. If it's this you want, I would suggest that you look into one of the many open game engines that are out there and try some of the input tutorials. OGRE 3d might be a good place to start, good community and good tutorials.
However, if that is not what you want and you indeed want to play around with hooks, you will need to create a different thread. If you create an infinite loop the system will never get around to handling the hooks.
Probably the easiest way to do what you want to do is to open a window and make it invisible. Creating a window using the raw win32 API that you're using right now is involved and takes quite a bit of code to do. Here is one tutorial that explains the steps involved.
But again, if you are looking to make a simple game, I suggest leveraging an existing engine. You'll make way faster progress that way. Good luck!
我不明白...你想要一个消息框吗?如果没有,为什么要调用 MessageBox()?我相信消息框是模态的,这意味着用户必须以某种方式确认它(单击“确定”/取消),然后才能返回到父界面。我相信它也是阻塞的,这意味着它不会在单独的线程上运行,并且会阻塞调用它的线程的执行,直到用户关闭它。
您的应用程序不应仅仅因为用户关闭消息框而关闭,除非您专门如此编写。我需要更多信息,可能还需要代码示例来告诉您为什么会发生这种情况(如果是无意的)。
I don't understand... do you want a message box or not? If not, why are you calling MessageBox()? I believe the message box is modal, meaning that the user MUST acknowledge it in some way (clicking okay/cancel) before they can return to the parent interface. I believe it is also blocking, meaning that it does not run on a separate thread and will block execution of the thread it was called on until the user closes it.
Your application should not be shutting down just because the user closes the message box unless that is how you have specifically written it. I would need more information and possibly code samples to tell you why that is happening if it is unintended.