You need to use a "modeless" dialog box and handle the message loop yourself. Here's an example (not the best coding style, but you get the idea). Just make a dialog resource with an OK button (IDOK) and cancel button (IDCANCEL). Pushing the OK button toggles the window. Cancel closes the app.
Code:
#include <windows.h>
#define CLASS_NAME "AClass"
HWND g_hwndWnd;
HWND g_hwndDlg;
HINSTANCE g_hinst;
INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
if (g_hwndWnd == 0) {
g_hwndWnd = CreateWindowEx(
0, CLASS_NAME, "Example",
WS_OVERLAPPEDWINDOW,
200, 0, 300, 300,
NULL, NULL, g_hinst, NULL
);
if (g_hwndWnd == NULL) {
MessageBox(0, "Can't create window", 0, 0);
return TRUE;
}
ShowWindow(g_hwndWnd, SW_SHOW);
}else{
DestroyWindow(g_hwndWnd);
g_hwndWnd = 0;
}
return TRUE;
case IDCANCEL:
DestroyWindow(g_hwndDlg);
g_hwndDlg = 0;
PostQuitMessage(0);
return TRUE;
}
}
return FALSE;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CLOSE:
DestroyWindow(g_hwndWnd);
g_hwndWnd = 0;
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WinMain(HINSTANCE hinst, HINSTANCE unused, LPSTR cmd, int show) {
WNDCLASS wc = { 0 };
MSG msg;
BOOL bRet;
g_hinst = hinst;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hinst;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
RegisterClass(&wc);
g_hwndDlg = CreateDialog(hinst, MAKEINTRESOURCE(1001), 0, DialogProc);
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
if (bRet == -1) {
MessageBox(0, "Error in GetMessage", 0, 0);
return 0;
}
else if (!IsWindow(g_hwndDlg) || !IsDialogMessage(g_hwndDlg, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}