c++ - WinMain/Win32 Window not displaying, but shows up in Proccesses tab of Task Manager -
i'm newbie c++, , i'm stuck on displaying window. i'm not getting errors, window not displaying on desktop. when open task manager, appears under 'proccesses' tab. haven't been finding solutions problem, appreciated. thanks! :)
**note: i'm using microsoft visual studio 2012 **note: not newbie c++, more creating win32 application
#include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h> static tchar windowclass[] = l"window"; lresult callback windowproc( hwnd winh, uint msg, wparam wparam, lparam lparam ) { switch (msg) { paintstruct pntstruct; static hdc hdc; case wm_paint: { beginpaint( winh, &pntstruct ); textout(hdc, 5, 5, l"hello, world!", _tcslen(l"hello, world!")); //pntstruct.rcpaint endpaint( winh, &pntstruct ); } break; case wm_size: { } break; case wm_move: { } break; case wm_destroy: { } break; case wm_close: { } break; default: { return defwindowproc(winh, msg, wparam, lparam); } break; case wm_activateapp: { if (wm_activateapp) { outputdebugstringa("wm_activeapp->true"); } else { outputdebugstringa("wm_activeapp->false"); } } break; } return 0; }; int winapi winmain( hinstance window, hinstance previnstance, lpstr cmd, int cmdshow ) { wndclassex wclass; //wclass.cbsize = sizeof(wndclass); wclass.style = cs_hredraw | cs_vredraw | cs_owndc; wclass.lpfnwndproc = windowproc; wclass.cbclsextra = 0; wclass.cbwndextra = 0; wclass.hinstance = window; //wclass.hicon; todo: create icon //wclass.hcursor; //wclass.hbrbackground = (hbrush)(color_window+1); wclass.lpszmenuname = null; wclass.lpszclassname = (lpctstr)windowclass; // hicon hiconsm; registerclassex(&wclass); hwnd createwin = createwindow( windowclass, l"name of window", ws_visible | ws_overlappedwindow, cw_usedefault, cw_usedefault, cw_usedefault,//width:[todo]->make custom width fit window cw_usedefault,//height:[todo]->make custom width fit window 0, 0, window, 0 ); showwindow(createwin, cmdshow); updatewindow(createwin); msg message; while (getmessage(&message, null, 0, 0) > 0) { translatemessage(&message); dispatchmessage(&message); }; return 0; };
there lots of things wrong code.
you declaring windowclass tchar[], initializing wchar_t[]. same thing lpstring parameter of textout(). work if unicode defined project, otherwise compiler error. when use tchar, need wrap string literals text() macro use correct character type. otherwise, stop using tchar , use unicode apis everything. need use tchar if code needs support both ansi (win9x/me) , unicode (nt4+) compilations. nobody supports win9x/me anymore, new code should focus on unicode apis.
you not zeroing contents of wndclassex structure, of fields have intentionally commented out , not assigned values (most importantly, cbsize field) contain random values stack. cause registerclassex() fail, not checking for. avoid problem, 0 out api structures before using them. important structures grow in size on time (when newer windows releases introduce new structure fields). such structures typically have cbsize field api knows version of structure using, must provide accurate value. , need 0 out unused fields not unexpected behavior api.
you not checking if createwindow() fails, such side effect of registerclassex() failing.
your windowproc() supposed pass unhandled messages defwindowproc(), not doing that. of case blocks discarding messages windows cannot process them. want? doubt it. in particular, default behavior of defwindowproc() wm_close destroy window, triggering wm_destroy message.
your wm_destroy handler not calling postquitmessage() put wm_quit message calling thread's message queue getmessage() can return 0 break message loop , let app exit.
your wm_paint handler not using hdc beginpaint() provides you, drawing using uninitialized hdc variable.
with of said, try more this:
#include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h> // or: remove static tchar windowclass[] = text("window"); // or: static wchar windowclass[] = l"window"; lresult callback windowproc(hwnd hwnd, uint umsg, wparam wparam, lparam lparam) { switch (umsg) { case wm_paint: { static const tchar* helloworld = text("hello, world!"); // or: const wchar* helloworld = l"hello, world!"; paintstruct pntstruct = {0}; hdc hdc = beginpaint(hwnd, &pntstruct); textout(hdc, 5, 5, helloworld, _tcslen(helloworld)); // or: textoutw(hdc, 5, 5, helloworld, lstrlenw(helloworld)); endpaint(hwnd, &pntstruct); break; } case wm_size: { //... break; } case wm_move: { //... break; } case wm_destroy: { postquitmessage(0); break; } case wm_close: { //... break; } case wm_activateapp: { if (wm_activateapp) { outputdebugstring(text("wm_activeapp->true")); // or: outputdebugstringw(l"wm_activeapp->true"); } else { outputdebugstring(text("wm_activeapp->false")); // or: outputdebugstringw(l"wm_activeapp->false"); } break; } } return defwindowproc(hwnd, umsg, wparam, lparam); } int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) { wndclassex wclass = {0}; // or: wndclassexw wclass.cbsize = sizeof(wclass); wclass.style = cs_hredraw | cs_vredraw | cs_owndc; wclass.lpfnwndproc = &windowproc; wclass.cbclsextra = 0; wclass.cbwndextra = 0; wclass.hinstance = hinstance; wclass.hicon = null; // todo: create icon wclass.hcursor = null; wclass.hbrbackground = null;//(hbrush)(color_window+1); wclass.lpszmenuname = null; wclass.lpszclassname = windowclass; wclass.hiconsm = null; if (!registerclassex(&wclass)) // or: registerclassexw() { // error! use getlasterror() find out why... return 0; } hwnd hcreatewin = createwindow( // or: createwindoww() windowclass, text("name of window"), // or: l"name of window" ws_visible | ws_overlappedwindow, cw_usedefault, cw_usedefault, cw_usedefault,//width:[todo]->make custom width fit window cw_usedefault,//height:[todo]->make custom width fit window 0, 0, hinstance, 0 ); if (!hcreatewin) { // error! use getlasterror() find out why... return 0; } showwindow(hcreatewin, ncmdshow); updatewindow(hcreatewin); msg message; while (getmessage(&message, null, 0, 0) > 0) { translatemessage(&message); dispatchmessage(&message); }; return 0; };
Comments
Post a Comment