c++ - WinAPI window doesn't appear -
and can't figure out why. code:
#include <windows.h> #include <commctrl.h> #include <cstdio> #include <stdarg.h> #include <string> #include <cmath> #include <vector> #include "resources.hpp" using std::string; using std::vector; struct undostruct{ /* add later */}; char buffer[2048]; hwnd statusbar; hinstance hinst; vector<undostruct> undo; void show_error(const char* format,...){ va_list args; va_start(args,format); vsprintf(buffer,format,args); va_end(args); messagebox(null,buffer,"error",mb_ok);} hwnd create_tooltip(hwnd parent,char* tip,unsigned uid,unsigned extraflags=0){ hwnd tt=createwindowex(ws_ex_topmost,tooltips_class,null,ws_popup|tts_noprefix|tts_alwaystip,0,0,0,0,parent,null,null,null); setwindowpos(tt,hwnd_topmost,0,0,0,0,swp_nomove|swp_nosize|swp_noactivate); toolinfo ti; ti.cbsize=sizeof(toolinfo); ti.uflags=ttf_subclass|extraflags; ti.hwnd=parent; ti.hinst=null; ti.uid=uid; ti.lpsztext=tip; getclientrect(parent,&ti.rect); sendmessage(tt,ttm_addtool,0,(lparam)(lptoolinfo)&ti); return tt;} lresult callback wndproc(hwnd,uint,wparam,lparam); class font{ private: hfont hfont; public: font(const char* fname){ hfont=createfont(0,0,0,0,fw_normal,false,false,false,default_charset,out_default_precis,clip_default_precis,default_quality,default_pitch,fname);} ~font(){ deleteobject(hfont);} operator hfont(){ return hfont;}}courier("courier new"); bool get_filename(char* fname,int len,hwnd hwnd,bool save){ openfilename ofn; zeromemory(&ofn,sizeof(openfilename)); ofn.lstructsize=sizeof(openfilename); ofn.hwndowner=hwnd; ofn.lpstrfilter="text files (*.txt)\0*.txt\0all files (*.*)\0*.*\0\0"; ofn.lpstrfile=fname; ofn.nmaxfile=len; ofn.lpstrtitle="text editor"; ofn.flags=ofn_filemustexist|ofn_hidereadonly; if(save){ return getsavefilename(&ofn);} else{ return getopenfilename(&ofn);}} int winapi winmain(hinstance hprev,hinstance hinst,lpstr cmdline,int cmdshow){ wndclassex wcex; //haccel haccel=loadaccelerators(hinst,makeintresource(accels)); hwnd hwnd; msg msg; hinst=hinst; //register window wcex.cbsize=sizeof(wndclassex); wcex.style=cs_hredraw|cs_vredraw; wcex.lpfnwndproc=wndproc; wcex.cbclsextra=0; wcex.cbwndextra=0; wcex.hinstance=hinst; wcex.hicon=null; wcex.hcursor=null; wcex.hbrbackground=(hbrush)(color_window+1); wcex.lpszmenuname=makeintresource(mainmenu); wcex.lpszclassname="imageeditor"; wcex.hiconsm=null; if(!registerclassex(&wcex)){ show_error("error %i: failed register window.",getlasterror()); return -1;} //create window hwnd=createwindow("imageeditor","image editor",ws_visible|ws_overlappedwindow,cw_usedefault,cw_usedefault,cw_usedefault,cw_usedefault,null,null,hinst,null); if(!hwnd){ show_error("error %i: failed create window.",getlasterror()); return -2;} //show/update window showwindow(hwnd,cmdshow); updatewindow(hwnd); //initialize common controls /*initcommoncontrolsex iccex; iccex.dwicc=icc_win95_classes; iccex.dwsize=sizeof(initcommoncontrolsex); initcommoncontrolsex(&iccex);*/ //go main program loop while(getmessage(&msg,null,0,0)){ //if(!translateaccelerator(hwnd,haccel,&msg)){ translatemessage(&msg); dispatchmessage(&msg);}//} return msg.wparam;} lresult callback wndproc(hwnd hwnd,uint msg,wparam wparam,lparam lparam){ static int sizes[]={260,330,420,520}; switch(msg){ case wm_create: statusbar=createwindow(statusclassname,"",ws_child|ws_border|ws_visible,-100,-100,10,10,hwnd,null,hinst,null); if(!statusbar){ show_error("error %i: failed create statusbar.",getlasterror());} //description|characters|size|lines|line|column sendmessage(statusbar,sb_setparts,sizeof(sizes),(lparam)sizes); break; case wm_quit: destroywindow(hwnd); break; case wm_destroy: postquitmessage(0); break; case wm_command: //switch(loword(wparam)){} //break; default: return defwindowproc(hwnd,msg,wparam,lparam);} return 0;}
also, compiler doesn't recognize initcommoncontrolsex when should, commented out near end of winmain.
the bug in code need run initcommoncontrols before creating window. , forget initcommoncontrolsex() code, you'll better plain old initcommoncontrols.
remember check every function's return value , use getlasterror().
also, you're trying reinvent wheel , instead of rolling out own window creating procedure suggest take @ how others it, or use wtl, it's not difficult.
Comments
Post a Comment