   api showWindow / getOpenFileName bug
  So I recently started doing some windows api programming to create a nice looking title menu for my game. I recently discovered a bug in my program and I really don't understand how it is occurring.Bug procedure:Click 'Resume Game' button on starting page, then click cancel on the getOpenFileName window that pops up. Next click either the 'Controls Configuration' or 'Credits' button. Lastly click the 'Return' button on the bottom right corner of the screen. Bug description:After doing the above procedure the parent window will be hidden from view.This only happens after you cancel out of the getOpenFileName window, otherwise clicking through the different buttons works as intended.Any other critics of my code are welcome (as I said I am new to api programming), but finding the cause / solution to the bug above is my first priority.// Dane RPG// INCLUDES ///////////////////////////////////////////////#include <windows.h>   // include all the windows headers#include <windowsx.h>  // include useful macros#include <commdlg.h>#include <stdio.h>#include <iostream>#include <math.h>#include <string>// DEFINES ////////////////////////////////////////////////// defines for windows#define WINDOW_CLASS_NAME "DaneGame"#define WIN32_LEAN_AND_MEAN  // just say no to MFC#define WINDOW_WIDTH 640#define WINDOW_HEIGHT480// PROTOTYPES /////////////////////////////////////////////////using namespace std;// game consoleint Game_Init(void *parms=NULL);int Game_Shutdown(void *parms=NULL);int Game_Main(void *parms=NULL);bool GetFolderName(void *parms=NULL);// GLOBALS ////////////////////////////////////////////////HWND main_window_handle  = NULL; // save the window handleHINSTANCE main_instance  = NULL; // save the instanceHWND title_handle[5], config_handle[25], credit_handle;// 0-9 = title_handle, 10-19 = static config_handle, 20-29 = edit config_handle,// 30-39 = button config_handle, 40-49 = control/return config_handle, 50 = creditOPENFILENAME ofn; //resume game filechar folderName[MAX_PATH] = "";  //holds game directory path// FUNCTIONS //////////////////////////////////////////////LRESULT CALLBACK WindowProc(HWND hwnd,  UINT msg,   WPARAM wparam,   LPARAM lparam){// this is the main message handler of the systemPAINTSTRUCT    ps; // used in WM_PAINTHDC  hdc;  // handle to a device contextLPSTR szMessage = "Text Message!"; // what is the messageswitch(msg)    {    case WM_CREATE:   {title_handle[0] = CreateWindowEx(0, // extended style   "button",   // class   "Resume Game", // title   WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,   320,150,    // intial x, initial y   300,60,  // initial width, height   hwnd,   // handle to parent   (HMENU)1,   // handle to menu   NULL,// instance of this application   NULL);    // extra creation parmstitle_handle[1] = CreateWindowEx(0,"button",   "New Game",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,   320,225,300,60,hwnd,(HMENU)2,NULL,NULL);   title_handle[2] = CreateWindowEx(0,"button",   "Controls Configuration",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,   320,300,300,60,hwnd,(HMENU)3,NULL,NULL);title_handle[3] = CreateWindowEx(0,"button",   "Credits",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,   320,375,145,60,hwnd,(HMENU)4,NULL,NULL);  title_handle[4] = CreateWindowEx(0,"button",   "Quit",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,   475,375,145,60,hwnd,(HMENU)5,NULL,NULL);    config_handle[0] = CreateWindowEx(WS_EX_STATICEDGE,"static",   "Move Up",WS_CHILD,   30,150,130,20,hwnd,(HMENU)10,NULL,NULL);config_handle[1] = CreateWindowEx(WS_EX_STATICEDGE,"static",   "   Move Down",WS_CHILD,   30,190,130,20,hwnd,(HMENU)11,NULL,NULL);  config_handle[2] = CreateWindowEx(WS_EX_STATICEDGE,"static",   "   Move Left",WS_CHILD,   30,230,130,20,hwnd,(HMENU)12,NULL,NULL);  config_handle[3] = CreateWindowEx(WS_EX_STATICEDGE,"static",   "   Move Right",WS_CHILD,   30,270,130,20,hwnd,(HMENU)13,NULL,NULL);   config_handle[4] = CreateWindowEx(WS_EX_STATICEDGE,"static",   " Accept",WS_CHILD,   30,310,130,20,hwnd,(HMENU)14,NULL,NULL); config_handle[5] = CreateWindowEx(WS_EX_STATICEDGE,"static",   " Cancel",WS_CHILD,   30,350,130,20,hwnd,(HMENU)15,NULL,NULL);    config_handle[6] = CreateWindowEx(WS_EX_STATICEDGE,"static",   "  Start",WS_CHILD,   30,390,130,20,hwnd,(HMENU)16,NULL,NULL); config_handle[7] = CreateWindowEx(0,"edit",   "config 20 text",WS_CHILD | BS_TEXT,   170,150,130,20,hwnd,(HMENU)20,NULL,NULL);config_handle[8] = CreateWindowEx(0,"edit",   "config 21 text",WS_CHILD | BS_TEXT,   170,190,130,20,hwnd,(HMENU)21,NULL,NULL); config_handle[9] = CreateWindowEx(0,"edit",   "config 22 text",WS_CHILD | BS_TEXT,   170,230,130,20,hwnd,(HMENU)22,NULL,NULL);   config_handle[10] = CreateWindowEx(0,"edit",   "config 23 text",WS_CHILD | BS_TEXT,   170,270,130,20,hwnd,(HMENU)23,NULL,NULL);  config_handle[11] = CreateWindowEx(0,"edit",   "config 24 text",WS_CHILD | BS_TEXT,   170,310,130,20,hwnd,(HMENU)24,NULL,NULL);   config_handle[12] = CreateWindowEx(0,"edit",   "config 25 text",WS_CHILD | BS_TEXT,   170,350,130,20,hwnd,(HMENU)25,NULL,NULL);  config_handle[13] = CreateWindowEx(0,"edit",   "config 26 text",WS_CHILD | BS_TEXT,   170,390,130,20,hwnd,(HMENU)26,NULL,NULL);   for(int i=7;i<14;i++)Edit_Enable(config_handle,FALSE);   config_handle[14] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,150,130,20,hwnd,(HMENU)30,NULL,NULL);   config_handle[15] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,190,130,20,hwnd,(HMENU)31,NULL,NULL);  config_handle[16] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,230,130,20,hwnd,(HMENU)32,NULL,NULL);    config_handle[17] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,270,130,20,hwnd,(HMENU)33,NULL,NULL);  config_handle[18] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,310,130,20,hwnd,(HMENU)34,NULL,NULL); config_handle[19] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,350,130,20,hwnd,(HMENU)35,NULL,NULL);   config_handle[20] = CreateWindowEx(0,"button",   "Update Key",WS_CHILD | BS_PUSHBUTTON,   310,390,130,20,hwnd,(HMENU)36,NULL,NULL);config_handle[21] = CreateWindowEx(0,"button",   "Control Scheme A",WS_CHILD | BS_PUSHBUTTON,   470,150,150,40,hwnd,(HMENU)40,NULL,NULL);   config_handle[22] = CreateWindowEx(0,"button",   "Control Scheme B",WS_CHILD | BS_PUSHBUTTON,   470,230,150,40,hwnd,(HMENU)41,NULL,NULL);  config_handle[23] = CreateWindowEx(0,"button",   "Control Scheme C",WS_CHILD | BS_PUSHBUTTON,   470,310,150,40,hwnd,(HMENU)42,NULL,NULL);    config_handle[24] = CreateWindowEx(0,"button",   "Return to Menu",WS_CHILD | BS_PUSHBUTTON,   470,390,150,40,hwnd,(HMENU)43,NULL,NULL);   credit_handle = CreateWindowEx(0,"button",   "Return to Menu",WS_CHILD | BS_PUSHBUTTON,   470,390,150,40,hwnd,(HMENU)50,NULL,NULL);// do initialization stuff here    return(0);    }  break;    case WM_PAINT:    {    // start painting    hdc = BeginPaint(hwnd, &ps); TextOut(hdc, 70, 50, szMessage, strlen(szMessage)); EndPaint(hwnd, &ps); // the window is now validated    // end painting    EndPaint(hwnd,&ps);    return(0);   }  break;  case WM_COMMAND:    {    if (LOWORD(wparam) == 1){  char szFileName[MAX_PATH] = "";  ZeroMemory(&ofn, sizeof(ofn));  ofn.lpstrFile = szFileName;  ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW  ofn.hwndOwner = hwnd;  ofn.lpstrFilter = "Save Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";  ofn.nMaxFile = MAX_PATH;  ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;  ofn.lpstrDefExt = "txt"; ofn.lpstrInitialDir = folderName;    if(GetOpenFileName(&ofn)){ // Do something usefull with the filename stored in szFileName   }    }    if (LOWORD(wparam) == 2){  Beep(100, 100);  Beep(100, 100);    }    if (LOWORD(wparam) == 3){  for(int i=0; i<sizeof(title_handle); i++)  ShowWindow(title_handle,SW_HIDE);  for(int i=0; i<sizeof(config_handle); i++)  ShowWindow(config_handle,SW_SHOW);  ShowWindow(credit_handle,SW_HIDE);  UpdateWindow(hwnd);    }    if (LOWORD(wparam) == 4){ for(int i=0; i<sizeof(title_handle); i++)  ShowWindow(title_handle,SW_HIDE);  ShowWindow(credit_handle,SW_SHOW);   UpdateWindow(hwnd); }    if (LOWORD(wparam) == 5){  SendMessage(hwnd,WM_CLOSE,0,0);    }    if (LOWORD(wparam) == 43){  for(int i=0; i<sizeof(title_handle); i++)  ShowWindow(title_handle,SW_SHOW);  for(int i=0; i<sizeof(config_handle); i++)  ShowWindow(config_handle,SW_HIDE);  ShowWindow(credit_handle,SW_HIDE);  UpdateWindow(hwnd);    }    if (LOWORD(wparam) == 50){  for(int i=0; i<sizeof(title_handle); i++)  ShowWindow(title_handle,SW_SHOW);  for(int i=0; i<sizeof(config_handle); i++)  ShowWindow(config_handle,SW_HIDE);  ShowWindow(credit_handle,SW_HIDE);  UpdateWindow(hwnd);    }    return 0;    } break;    case WM_DESTROY:    { // kill the application    PostQuitMessage(0);    return(0); }  break;    default:break;    }  // end switch// process any messages that we didn't take care ofreturn (DefWindowProc(hwnd, msg, wparam, lparam));}  // end WinProc// WINMAIN ////////////////////////////////////////////////int WINAPI WinMain(HINSTANCE hinstance,  HINSTANCE hprevinstance,  LPSTR lpcmdline,  int ncmdshow){// this is the winmain functionWNDCLASSEX winclass;  // this will hold the class we createHWNDhwnd;    // generic window handleMSG msg;    // generic messageHDC hdc;  // generic dcPAINTSTRUCT ps;// generic paintstructint start_window_x = 0;int start_window_y = 0;// if screen is large enough, center game windowif(GetSystemMetrics(SM_CXSCREEN) > WINDOW_WIDTH) {start_window_x = (int)((GetSystemMetrics(SM_CXSCREEN) - WINDOW_WIDTH)/2);}if(GetSystemMetrics(SM_CYSCREEN) > WINDOW_HEIGHT) {start_window_y = (int)((GetSystemMetrics(SM_CYSCREEN) - WINDOW_HEIGHT)/2);}// first fill in the window class structure// first fill in the window class structurewinclass.cbSize  = sizeof(WNDCLASSEX);winclass.style= CS_DBLCLKS | CS_OWNDC |CS_HREDRAW | CS_VREDRAW;winclass.lpfnWndProc    = WindowProc;winclass.cbClsExtra= 0;winclass.cbWndExtra= 0;winclass.hInstance = hinstance;winclass.hIcon= LoadIcon(NULL, IDI_APPLICATION);winclass.hCursor   = LoadCursor(NULL, IDC_ARROW);winclass.hbrBackground  = (HBRUSH)GetStockObject(GRAY_BRUSH);winclass.lpszMenuName   = NULL;winclass.lpszClassName  = WINDOW_CLASS_NAME;winclass.hIconSm   = LoadIcon(NULL, IDI_APPLICATION);// register the window classif (!RegisterClassEx(&winclass))    return(0);// create the windowif (!(hwnd = CreateWindowEx(0, // extended style   WINDOW_CLASS_NAME,   // class  "Dane RPG", // title   WS_OVERLAPPEDWINDOW | WS_VISIBLE,   start_window_x,    //initial x   start_window_y,    // initial y   WINDOW_WIDTH,WINDOW_HEIGHT,  // initial width, height   NULL,   // handle to parent   NULL,   // handle to menu   hinstance,// instance of this application   NULL)))    // extra creation parmsreturn(0);// save the window handle and instance in a globalmain_window_handle = hwnd;main_instance = hinstance;if(GetFolderName());// perform all game console specific initializationGame_Init();// enter main event loopwhile(1)    {    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))    {    // test if this is a quit   if (msg.message == WM_QUIT) break;    // translate any accelerator keys    TranslateMessage(&msg;    // send the message to the window proc    DispatchMessage(&msg;    } // end if  // main game processing goes here  //Game_Main();    }  // end while// shutdown game and release all resources//Game_Shutdown();// return to Windows like thisreturn(msg.wParam);}  // end WinMain///////////////////////////////////////////////////////////// T3DX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////////int Game_Init(void *parms){// this function is where you do all the initialization// for your game// return successreturn(1);}  // end Game_Init///////////////////////////////////////////////////////////////bool GetFolderName(void *parms){    int i,found = 0;    i = MAX_PATH;memset(folderName, '\0', MAX_PATH);    //reset folderName    GetModuleFileName(NULL,folderName,sizeof(folderName));while( found == 0 &&  i > 0){ i--; if (folderName == '\\')    found = i + 1;    }    folderName[found] = '\0';return 1; }
  The problem already occurs with entering Control Configuration and returning. Your major bug:sizeof( title_handle )That does NOT give you the array size, it returns the size of the array (20 instead of 5). You're calling ShowWindow on 15 HWNDs, one seems to be "luckily" your main HWND.You can try sizeof( title_handle ) / sizeof( title_handle[0] ).Same for sizeof( config_handle ).Other minor things:You're calling EndPaint twice.   ;Quote:Original post by EndurionThe problem already occurs with entering Control Configuration and returning. Your major bug:sizeof( title_handle )That does NOT give you the array size, it returns the size of the array (20 instead of 5). You're calling ShowWindow on 15 HWNDs, one seems to be "luckily" your main HWND.You can try sizeof( title_handle ) / sizeof( title_handle[0] ).Same for sizeof( config_handle ).Other minor things:You're calling EndPaint twice.Awesome thank you very much for your help. I'm a little rusty in C++ since I haven't used that language in a while and that bug was driving me crazy.Thanks again!   