c++ - ReOpen window from main window. -
i have main window open. created me using visual studio. attempting have main screen 9 12 buttons. these buttons open other windows. attempting 1 button "inventorybtn". inventory window open , able close it. second time try open window error. seems window registered , cant recreated. doing incorrectly? stepped thru , aw "destroy:" being called. second time try show inventory window handle uninitialized.
#include "stdafx.h" #include "vehmanager.h" #include "m_inventory.h" #define max_loadstring 100 #define invwindowbtn 101 #define invbtn 103 using namespace boost::gregorian; hinstance hinst; wchar sztitle[max_loadstring]; wchar szwindowclass[max_loadstring]; atom myregisterclass(hinstance hinstance); bool initinstance(hinstance, int); lresult callback wndproc(hwnd, uint, wparam, lparam); int_ptr callback about(hwnd, uint, wparam, lparam); lresult callback invwindowprocess(hwnd, uint, wparam, lparam); hwnd handleforinvwin; wndclassex wxinv; void createinventorywindow(); int apientry wwinmain(_in_ hinstance hinstance, _in_opt_ hinstance hprevinstance, _in_ lpwstr lpcmdline, _in_ int ncmdshow) { unreferenced_parameter(hprevinstance); unreferenced_parameter(lpcmdline); loadstringw(hinstance, ids_app_title, sztitle, max_loadstring); loadstringw(hinstance, idc_vehmanager, szwindowclass, max_loadstring); myregisterclass(hinstance); if (!initinstance (hinstance, ncmdshow)) { return false; } haccel hacceltable = loadaccelerators(hinstance, makeintresource (idc_vehmanager)); msg msg; while (getmessage(&msg, nullptr, 0, 0)) { if (!translateaccelerator(msg.hwnd, hacceltable, &msg)) { translatemessage(&msg); dispatchmessage(&msg); } } return (int) msg.wparam; } atom myregisterclass(hinstance hinstance) { wndclassexw wcex; wcex.cbsize = sizeof(wndclassex); wcex.style = cs_hredraw | cs_vredraw; wcex.lpfnwndproc = wndproc; wcex.cbclsextra = 0; wcex.cbwndextra = 0; wcex.hinstance = hinstance; wcex.hicon = loadicon(hinstance, makeintresource(idi_vehmanager)); wcex.hcursor = loadcursor(nullptr, idc_arrow); wcex.hbrbackground = (hbrush)(color_window+1); wcex.lpszmenuname = makeintresourcew(idc_vehmanager); wcex.lpszclassname = szwindowclass; wcex.hiconsm = loadicon(wcex.hinstance, makeintresource(idi_small)); return registerclassexw(&wcex); } bool initinstance(hinstance hinstance, int ncmdshow) { hinst = hinstance; hwnd hwnd = createwindoww(szwindowclass, sztitle, ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, nullptr, nullptr, hinstance, nullptr); if (!hwnd) { messageboxa(null, "main window creation failed.", "904.vehiclemanager", mb_ok | mb_iconinformation); return false; } showwindow(hwnd, ncmdshow); updatewindow(hwnd); return true; } lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam) { hfont hfdefault; switch (message) { case wm_create: { hwnd hinvbtn = createwindowex(null, l"button", l"inventory", ws_tabstop | ws_visible | ws_child | bs_defpushbutton, 50, 220, 100, 24, hwnd, (hmenu)invbtn, getmodulehandle(null), null); if (hinvbtn == null) messageboxa(hwnd, "could not create inventory button", "904.vehclemanager", null); hfdefault = (hfont)getstockobject(default_gui_font); sendmessage(hinvbtn, wm_setfont, (wparam)hfdefault, makelparam(false, 0)); } case wm_command: { int wmid = loword(wparam); switch (wmid) { case invbtn: { createinventorywindow(); break; } case idm_about: dialogbox(hinst, makeintresource(idd_aboutbox), hwnd, about); break; case idm_exit: destroywindow(hwnd); break; default: return defwindowproc(hwnd, message, wparam, lparam); } } break; case wm_rbuttondown: messagebox(null, l"right button mouse clicks not allowed.", l"904.vehiclemanager", null); break; case wm_paint: { paintstruct ps; hdc hdc = beginpaint(hwnd, &ps); endpaint(hwnd, &ps); } break; case wm_destroy: postquitmessage(0); break; default: return defwindowproc(hwnd, message, wparam, lparam); } return 0; } // message handler box. int_ptr callback about(hwnd hdlg, uint message, wparam wparam, lparam lparam) { unreferenced_parameter(lparam); switch (message) { case wm_initdialog: return (int_ptr)true; case wm_command: if (loword(wparam) == idok || loword(wparam) == idcancel) { enddialog(hdlg, loword(wparam)); return (int_ptr)true; } break; } return (int_ptr)false; } void createinventorywindow() { zeromemory(&wxinv, sizeof(wndclassex)); wxinv.cbclsextra = null; wxinv.cbsize = sizeof(wndclassex); wxinv.cbwndextra = null; wxinv.hbrbackground = (hbrush)color_window; wxinv.hcursor = loadcursor(null, idc_arrow); wxinv.hicon = null; wxinv.hiconsm = null; wxinv.hinstance = hinst; wxinv.lpfnwndproc = (wndproc)invwindowprocess; wxinv.lpszclassname = l"inventory"; wxinv.lpszmenuname = null; wxinv.style = cs_hredraw | cs_vredraw; if (!registerclassex(&wxinv)) { int nresult = getlasterror(); messagebox(null, l"inventory window class registration failed.", l"904.vehiclemanager", mb_iconexclamation); return; } handleforinvwin = createwindowex(null, wxinv.lpszclassname, l"open window 2", ws_overlappedwindow, 200, 170, 640, 480, null, null, hinst, null); if (!handleforinvwin) { messagebox(null, l"error inventory window handle.", l"904.vehmanager", mb_iconexclamation); return; } showwindow(handleforinvwin, sw_shownoactivate); updatewindow(handleforinvwin); } lresult callback invwindowprocess(hwnd hwnd, uint message, wparam wparam, lparam lparam) { hfont hfdefault; switch (message) { case wm_create: { hwnd hinvbtn = createwindowex(null, l"button", l"close", ws_tabstop | ws_visible | ws_child | bs_defpushbutton, 50, 220, 100, 24, hwnd, (hmenu)invbtn, getmodulehandle(null), null); if (hinvbtn == null) messageboxa(hwnd, "could not create inventory button", "904.vehclemanager", null); hfdefault = (hfont)getstockobject(default_gui_font); sendmessage(hinvbtn, wm_setfont, (wparam)hfdefault, makelparam(false, 0)); break; } case wm_destroy: { break; } case wm_command: case invbtn: { destroywindow(hwnd); break; } break; } return defwindowproc(hwnd, message, wparam, lparam); }
Comments
Post a Comment