简体   繁体   English

C ++ MessageBox(Windows.h)-它做什么,如何使其不可见(或等效)?

[英]C++ MessageBox (Windows.h) - What does it do an how can I make it invisible (or equivalent)?

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 我不是一个专家,我只是在胡闹: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms632589( v=vs.85) .aspx

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. MessageBox()在这里做的是两件事:它显示对话框,但它也在内部提供自己的消息循环以处理对话框的输入。 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. 低级挂钩需要一个消息循环才能正常运行,并且您的代码与MessageBox一起工作只是因为它提供的消息循环正在为您执行必要的消息处理。 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: 这是一个可以剪切并粘贴的简单示例:

MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

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() ; 当您在代码的其他地方(但仍在同一线程上)决定要退出时-也许当您在挂钩中检测到某些组合键时,请使用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. 这会将WM_QUIT消息发布到线程的队列中,当GetMessage检索到该消息时,它将返回0并退出循环。


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. 如果要在Windows应用程序中获取键盘输入,最简单的操作是创建自己的窗口,当按下/释放键时,它将接收WM_KEYDOWN / UP消息。

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. OGRE 3d可能是一个不错的起点,良好的社区和良好的教程。

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. 使用您现在正在使用的原始win32 API创建窗口涉及到此过程,并且需要花费大量代码。 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! 祝好运!

I don't understand... do you want a message box or not? 我不明白...您是否想要一个消息框? If not, why are you calling MessageBox()? 如果没有,为什么要调用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. 我需要更多信息,可能还需要代码示例来告诉您,如果不希望这样做,为什么会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM