Search code examples
c++winapimessageboxinvisible

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


Solution

  • 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:

    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(); 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.