Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tac-Tics

Win32, Messages, Menus and MessageBoxes

This topic is 5597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Today was the first day of finals at our school and so we got half the day off. I wisely spent my time trying to better understand the Win32 API. So far, I have the basic framework for my to-be game up. So far, it has a window and a menu with OpenGL rendering to the surface. However, I came across a peculiar bug in my program. When I click on the About menu I created and generate a WM_COMMAND, ID_ABOUT message, I want to have the game make a popup message box to just say "this is my game, blah blah blah". However, sometimes if you click on the OK button, it beeps, closes, and the message box appears AGAIN! I hold down the enter or spacebar button and repeatedly click on it with the mouse, and it just keeps reappearing. However, when I move the messagebox across the screen a little and THEN hit the button, it goes away like it should and doesn''t come back. I tried a few different things to test it. I tried opening 2 different message boxes in a row, and the problem went away, and I tried to Sleep(...) it (it went away only for longer periods of Sleep, Sleep(1) did nothing). Does anyone have any clue as to what''s going wrong here? It''s weirding me out. If you think code would help fix my problem, just say the word and I''ll post it. "You TK''ed my chicken!"

Share this post


Link to post
Share on other sites
Advertisement
Odd. The bug went away by itself =-[ I must have intimidated it... or maybe Windows unbroke itself. Windows breaks a lot.

"You TK'ed my chicken!"

*EDIT*
Gawd, it came back =-( ok, here's my code.


  
#include "win.h"
#include "game.h"

HWND g_Hwnd;
HDC g_Hdc;
BOOL g_Done = false;
HGLRC g_Hglrc;
HMENU g_Hmenu;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
char str[256];
int pixFormat;

static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(pfd),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0, 0, 0, 0
};

switch (msg)
{

case WM_CREATE:
g_Hmenu = GetMenu(g_Hwnd);
g_Hdc = GetDC(hwnd);

pixFormat = ChoosePixelFormat(g_Hdc, &pfd);
SetPixelFormat(g_Hdc, pixFormat, &pfd);

g_Hglrc = wglCreateContext(g_Hdc);
wglMakeCurrent(g_Hdc, g_Hglrc);

glViewport(32, 0, WIN_HEIGHT, WIN_HEIGHT - 32);
glMatrixMode(GL_PROJECTION);
glOrtho(-1, 1, -1, 1, 1, 100);
// glFrustum(-0.4, 0.4, -0.3, 0.3, 0.3, 100000);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


return 0;

case WM_KEYDOWN:
if (wp == 'Q' || wp == 'q')
{
PostMessage(g_Hwnd, WM_CLOSE, 0, 0);
}
if (wp == 'M' || wp == 'm')
{
if (GetMenu(g_Hwnd) != NULL)
{
SetMenu(g_Hwnd, NULL);
}
else
{
SetMenu(g_Hwnd, LoadMenu(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDR_MENU) ) );
}
}
return 0;

case WM_COMMAND:
switch (LOWORD(wp) )
{
case ID_FILE_EXIT:
PostMessage(g_Hwnd, WM_CLOSE, 0, 0);
break;

case ID_PAUSE_GAME:
if (g_GameState == GAME_RUNNING)
{
g_GameState = GAME_PAUSED;
glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT / 2);
}
else
{
g_GameState = GAME_RUNNING;
glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
}
break;

case ID_ABOUT:
strcpy(str, "This is the about box");
MessageBox(NULL, str, "Box", MB_OK);
break;

case ID_PLAY_MUSIC:
if (! PlaySound("data\\mmadness.mid", GetModuleHandle(NULL),
SND_LOOP | SND_FILENAME | SND_ASYNC) )
{
MessageBox(NULL, "Cannot play sound", NULL, MB_OK);
}
break;

}
return 0;

case WM_CLOSE:
DestroyWindow(g_Hwnd);
return 0;


case WM_DESTROY:
wglMakeCurrent(g_Hdc, NULL);
wglDeleteContext(g_Hglrc);

PostQuitMessage(0);
return 0;
}

return DefWindowProc(hwnd, msg, wp, lp);
}

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)
{
MSG msg;

WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_EVILBLACKMARBLE) );
wc.hIconSm = LoadIcon(hinst, MAKEINTRESOURCE(IDI_EVILBLACKMARBLE) );
wc.hInstance = hinst;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = WIN_CLASSNAME;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.style = CS_VREDRAW | CS_HREDRAW;

if (! RegisterClassEx(&wc) )
{
MessageBox(NULL, "ERROR", NULL, MB_OK);
return 0;
}

g_Hwnd = CreateWindowEx(
NULL,
WIN_CLASSNAME,
WIN_TITLE,
WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX,

CW_USEDEFAULT,
CW_USEDEFAULT,
WIN_WIDTH,
WIN_HEIGHT,
NULL,
NULL,
hinst,
NULL);

if (! g_Hwnd)
{
MessageBox(NULL, "ERROR", NULL, MB_OK);
return 0;
}

ShowWindow(g_Hwnd, showcmd);

while (! g_Done)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{
if (msg.message == WM_QUIT)
{
g_Done = true;
}
}

switch (g_GameState)
{
case GAME_RUNNING:
GameLoop();
break;
case GAME_PAUSED:
PausedLoop();
break;
}
}



return msg.message;
}


[edited by - Tac-Tics on January 23, 2003 3:45:09 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!