DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Code Snippet: Windows manipulation library (http://www.daniweb.com/forums/thread217393.html)

wavsyntax Jun 3rd, 2009 7:03 am
Windows manipulation library
 
This is my library for win32 apps.

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. /***********************************************************
  4.   -WINDOWS INITIALIZATION-
  5.  ***********************************************************/
  6. typedef BOOL (WINAPI* SLWA)(HWND, COLORREF, BYTE, DWORD);
  7. SLWA SetLayeredWindowAttributes;
  8. /***********************************************************
  9.   -WINDOWS MESSSAGES-
  10.  ***********************************************************/
  11. #define CNM_CHANGE 900000
  12. /***********************************************************
  13.   -WINDOWS SYSTEM DIALOG FUNCTIONS-
  14.  ***********************************************************/
  15. BOOL ChooseColorDlg(HWND hwnd,CHOOSECOLOR* chc,DWORD flags=CC_FULLOPEN,COLORREF cf[16]=NULL){
  16. chc->lStructSize=sizeof(CHOOSECOLOR);
  17. chc->hwndOwner=hwnd;
  18. if(cf==NULL){
  19. COLORREF cf2[16];
  20. for(int i=0;i<16;i++){
  21. cf2[i]=0x00FFFFFF;
  22. }
  23. chc->lpCustColors=cf2;
  24. }
  25. else{
  26. chc->lpCustColors=cf;
  27. }
  28. chc->Flags=flags;
  29. return ChooseColor(chc);
  30. }
  31.  
  32. BOOL OpenFileDlg(OPENFILENAME* ofn,HWND hwnd,char* buff,char* defExt=".txt\0",char* filter="Plain Text (.TXT)\0*.txt\0Rich Text Format (.RTF)\0*.rtf\0All Files\0*.*\0\0",int filterIndex=0){
  33. ofn->lStructSize=sizeof(OPENFILENAME);
  34. ofn->hwndOwner=hwnd;
  35. ofn->hInstance=GetModuleHandle(0);
  36. ofn->lpstrFilter=filter;
  37. ofn->lpstrDefExt=defExt;
  38. ofn->lpstrFile=buff;
  39. ofn->nMaxFile=256;
  40. ofn->Flags=OFN_EXPLORER;
  41. ofn->nFilterIndex=filterIndex;
  42. return GetOpenFileName(ofn);
  43. }
  44.  
  45. BOOL SaveFileDlg(OPENFILENAME* ofn,HWND hwnd,char* buff,char* defExt=".txt\0",char* filter="Plain Text (.TXT)\0*.txt\0Rich Text Format (.RTF)\0*.rtf\0All Files\0*.*\0\0",int filterIndex=0){
  46. ofn->lStructSize=sizeof(OPENFILENAME);
  47. ofn->hwndOwner=hwnd;
  48. ofn->hInstance=GetModuleHandle(0);
  49. ofn->lpstrFilter=filter;
  50. ofn->lpstrDefExt=defExt;
  51. ofn->lpstrFile=buff;
  52. ofn->nMaxFile=256;
  53. ofn->Flags=OFN_EXPLORER;
  54. ofn->nFilterIndex=filterIndex;
  55. return GetSaveFileName(ofn);
  56. }
  57. /***********************************************************
  58.   -WINDOWS PROGRAM DEBUGGING FUNCTIONS-
  59.  ***********************************************************/
  60. #ifndef de_error_windowsh
  61. void UseConsole(){
  62. int hCrt;
  63. FILE *hf;
  64. AllocConsole();
  65. hCrt=_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE),_O_TEXT);
  66. hf = fdopen(hCrt,"w");
  67. *stdout=*hf;
  68. setvbuf(stdout,NULL,_IONBF,0);
  69. }
  70.  
  71. void DebugError(){
  72. LPVOID lpMsgBuf;
  73. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0,NULL);
  74. std::cout<<"ERROR: "<<(const CHAR*)lpMsgBuf<<std::endl;
  75. LocalFree( lpMsgBuf );
  76. }
  77. #endif
  78.  
  79. #define GetWindowTitle(hwnd,cVar) \
  80. char cVar[GetWindowTextLength(hwnd)+1]; \
  81. GetWindowText(hwnd,cVar,GetWindowTextLength(hwnd)+1);
  82.  
  83. #define GetWindowClassName(hwnd,cVar) \
  84. char cVar[257]; \
  85. GetClassName(hwnd,cVar,256);
  86.  
  87. #ifndef de_error_windowsh
  88. void itest(int i,HWND hwnd=0){
  89. char integer[10];
  90. itoa(i,integer,10);
  91. MessageBox(hwnd,integer,"Integer Testing",MB_OK);
  92. }
  93. #endif
  94. /***********************************************************
  95.   -WINDOW CREATION AND MANIPULATION-
  96.  ***********************************************************/
  97. class WINCLASSEX{
  98. public:
  99.  
  100. WNDCLASSEX wc;
  101. WINCLASSEX(HINSTANCE hinst,WNDPROC wproc,LPSTR cls,int cx=0,int wx=0){
  102. this->wc.lpfnWndProc=wproc;
  103. this->wc.cbClsExtra=cx;
  104. this->wc.cbSize=sizeof(WNDCLASSEX);
  105. this->wc.cbWndExtra=wx;
  106.  
  107. this->wc.lpszMenuName=0;
  108. this->wc.lpszClassName=cls;
  109. this->wc.style=CS_DBLCLKS;
  110. this->wc.hInstance=hinst;
  111.  
  112. this->wc.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
  113. this->wc.hIcon=LoadIcon(0,IDI_APPLICATION);
  114. this->wc.hIconSm=LoadIcon(0,IDI_APPLICATION);
  115. this->wc.hCursor=LoadCursor(0,IDC_ARROW);
  116. }
  117. WNDCLASSEX GetStruct(){
  118. return this->wc;
  119. }
  120. void Register(){
  121. if(!RegisterClassEx(&this->wc)){
  122. MessageBox(0,"The specified window could not be registered",this->wc.lpszClassName,MB_OK);
  123. }
  124. }
  125. };
  126.  
  127. void alert(char* msg="",char* title="Alert!"){
  128. MessageBox(0,msg,title,MB_OK);
  129. }
  130.  
  131. void GetWndCursorPos(HWND hwnd,POINT* pt){
  132. GetCursorPos(pt);
  133. ScreenToClient(hwnd,pt);
  134. }
  135.  
  136. void SetClipboard(UINT format,HANDLE obj,HWND hwnd=0){
  137. OpenClipboard(hwnd);
  138. EmptyClipboard();
  139. SetClipboardData(format,obj);
  140. CloseClipboard();
  141. }
  142.  
  143. HANDLE GetClipboard(HWND hwnd,UINT format){
  144. OpenClipboard(hwnd);
  145. HANDLE rv=GetClipboardData(format);
  146. CloseClipboard();
  147. return rv;
  148. }
  149.  
  150. void SetWindowAlpha(HWND hwnd,DWORD alpha){
  151. HMODULE u32dll=GetModuleHandle("USER32.DLL");
  152. SetLayeredWindowAttributes=(SLWA)GetProcAddress(u32dll, "SetLayeredWindowAttributes");
  153. if(SetLayeredWindowAttributes){
  154. SetLayeredWindowAttributes(hwnd,0,alpha,0x02);
  155. }
  156. FreeLibrary(u32dll);
  157. }
  158.  
  159. #define StructToUserdata(HW,PTR) SetWindowLong(HW,GWL_USERDATA,(LONG)PTR);
  160.  
  161. #define UserdataToStruct(HW,TYPE,VARNAME) TYPE* VARNAME=new TYPE;\
  162. VARNAME=(TYPE*)GetWindowLong(HW,GWL_USERDATA);
  163.  
  164. #define GetWndClientRect(hwnd,rect) RECT rect;GetWindowRect(hwnd,&rect);
  165.  
  166. #define AddMenuItem(menu,txt,id){\
  167. AppendMenu(menu,MF_STRING,id,txt);\
  168. }
  169.  
  170. #define TrackMenu(hwnd,menu){\
  171. POINT pt;\
  172. GetWndCursorPos(hwnd,&pt);\
  173. TrackPopupMenu(menu,TPM_LEFTALIGN,pt.x,pt.y,0,hwnd,NULL);\
  174. }
  175.  
  176.  
  177. /***********************************************************
  178.   -WINDOWS GDI-
  179.  ***********************************************************/
  180.  
  181. COLORREF rgb(COLORREF cr){
  182. return ((DWORD) (((BYTE) (GetBValue(cr)) | ((WORD) (GetGValue(cr)) << 8)) | (((DWORD) (BYTE) (GetRValue(cr))) << 16)));
  183. }
  184.  
  185. COLORREF rgb(short r,short g,short b){
  186. return rgb(RGB(r,g,b));
  187. }
  188.  
  189. HBITMAP RectToPlainBitmap(const RECT* r,COLORREF color=RGB(255,255,255)){
  190. int w=r->right-r->left;
  191. int h=r->bottom-r->top;
  192. COLORREF* map=(COLORREF*)calloc(sizeof(COLORREF),w*h);
  193. for(int i=0;i<(w*h);i++){
  194. map[i]=color;
  195. }
  196. return CreateBitmap(w,h,1,32,map);
  197. }
  198.  
  199. HBITMAP RectToBitmap(HDC hdc,const RECT* r){
  200. int w=r->right-r->left;
  201. int h=r->bottom-r->top;
  202. COLORREF* map=(COLORREF*)calloc(sizeof(COLORREF),w*h);
  203. int c=0;
  204. for(int y=r->top;y<r->bottom;y++){
  205. for(int x=r->left;x<r->right;x++){
  206. map[c]=GetPixel(hdc,x,y);
  207. c++;
  208. }
  209. }
  210. return CreateBitmap(w,h,1,32,map);
  211. }
  212.  
  213.  
  214. HBITMAP WindowToBitmap(HWND hwnd){
  215. HDC hdcWnd,hdcBitmap;
  216. int w=100,h=100;
  217. HBITMAP hbmBitmap;
  218. HBITMAP hbmOld;
  219. hdcWnd=GetDC(hwnd);
  220. if(hdcWnd){
  221. RECT wnd;
  222. GetClientRect(hwnd,&wnd);
  223. w=wnd.right;
  224. h=wnd.bottom;
  225. hbmBitmap=(HBITMAP)CreateCompatibleBitmap(hdcWnd,w,h);
  226. if(!hbmBitmap){
  227. if(hdcWnd){
  228. RECT wnd;
  229. GetWindowRect(hwnd,&wnd);
  230. w=wnd.right;
  231. h=wnd.bottom;
  232. hbmBitmap=CreateCompatibleBitmap(hdcWnd,w,h);
  233. }
  234. }
  235. if(!hbmBitmap){
  236. std::cout<<"Bitmap missing! "<<std::endl;
  237. }
  238. hdcBitmap=CreateCompatibleDC(hdcWnd);
  239. hbmOld=(HBITMAP)SelectObject((HDC)hdcBitmap,(HGDIOBJ)(HBITMAP)hbmBitmap);
  240. BitBlt(hdcBitmap,0,0,w,h,hdcWnd,0,0,SRCCOPY);
  241. ReleaseDC(hwnd,hdcWnd);
  242. DeleteDC(hdcBitmap);
  243. }
  244. ReleaseDC(hwnd,hdcWnd);
  245. return hbmBitmap;
  246. }
  247.  
  248.  
  249. //Color=Transparent color [For windows that don't want to be rectangles]
  250. HRGN BitmapToRegion(HBITMAP hbm,COLORREF color=RGB(255,255,255)){
  251. HDC hdc=CreateCompatibleDC(NULL);
  252. BITMAP bm;
  253. SelectObject(hdc,hbm);
  254. GetObject(hbm,sizeof(bm),&bm);
  255. BeginPath(hdc);
  256. MoveToEx(hdc,0,0,NULL);
  257. for(int y=0;y<bm.bmHeight;y++){
  258. for(int x=0;x<bm.bmWidth;x++){
  259. COLORREF clr=GetPixel(hdc,x,y);
  260. if(clr!=color && x>0){
  261. LineTo(hdc,x,y);
  262. }
  263. else{
  264. MoveToEx(hdc,x+1,y,NULL);
  265. }
  266. }
  267. }
  268. EndPath(hdc);
  269. WidenPath(hdc);
  270. HRGN rgn=PathToRegion(hdc);
  271. DeleteDC(hdc);
  272. return rgn;
  273. }
  274.  
  275.  
  276.  
  277.  
  278. HBITMAP __fastcall AreaToBitmap(int x,int y,int w,int h){
  279. HDC hdc=GetWindowDC(HWND_DESKTOP);
  280. HBITMAP hbm;
  281. COLORREF map[w*h];
  282. x+=1;
  283. y-=1;
  284. w-=1;
  285. h-=1;
  286. int tempx=x;
  287. int tempy=y;
  288. for(int i=0;i<w*h+1;i++){
  289. if(x==w){
  290. y++;
  291. x=tempx;
  292. }
  293. if(y==h){
  294. return CreateBitmap(w-x,h-tempy,1,32,map);
  295. }
  296. map[i]=rgb(GetPixel(hdc,x,y));
  297. x++;
  298. }
  299. return CreateBitmap(w,h,1,32,map);
  300. }
  301.  
  302. void DrawRect(HDC hdc,const RECT* rect){
  303. MoveToEx(hdc,rect->left,rect->top,NULL);
  304. LineTo(hdc,rect->right,rect->top);
  305. MoveToEx(hdc,rect->right,rect->top,NULL);
  306. LineTo(hdc,rect->right,rect->bottom);
  307. MoveToEx(hdc,rect->right,rect->bottom,NULL);
  308. LineTo(hdc,rect->left,rect->bottom);
  309. MoveToEx(hdc,rect->left,rect->bottom,NULL);
  310. LineTo(hdc,rect->left,rect->top);
  311. }
  312.  
  313.  
  314.  
  315. void DrawEllipse(HDC hdc,const RECT* rect){
  316. Arc(hdc,rect->left,rect->top,rect->right,rect->bottom,0,0,0,0);
  317. }
  318.  
  319. void RefreshWindow(HWND hwnd,UINT flags=RDW_ERASE|RDW_INVALIDATE,HRGN hrgn=0){
  320. RECT client;
  321. GetClientRect(hwnd,&client);
  322. UpdateWindow(hwnd);
  323. RedrawWindow(hwnd,&client,hrgn,flags);
  324. }
  325.  
  326.  
  327. int GetLBSelText(HWND hwnd,char* buff=NULL){
  328. int index=SendMessage(hwnd,LB_GETCURSEL,0,0);
  329. int len=SendMessage(hwnd,LB_GETTEXTLEN,index,0);
  330. if(buff!=NULL){
  331. SendMessage(hwnd,LB_GETTEXT,index,LPARAM(buff));
  332. }
  333. return len;
  334. }
  335.  
  336. int AddLBString(HWND hwnd,char* str){
  337. SendMessage(hwnd,LB_ADDSTRING,0,LPARAM(str));
  338. }
  339.  
  340. /***********************************************************
  341.   -WINDOW ENUMERATION-
  342.  ***********************************************************/
  343. namespace WINENUMDATA{
  344. HWND hwnd;
  345. int currentIndex;
  346. };
  347.  
  348. BOOL WINENUM__EnumChildWindowsProc(HWND hwndIndex,LPARAM lParam){
  349. if(hwndIndex){
  350. if(lParam==WINENUMDATA::currentIndex){
  351. WINENUMDATA::hwnd=hwndIndex;
  352. return FALSE;
  353. }
  354. WINENUMDATA::currentIndex++;
  355. return TRUE;
  356. }
  357. else{
  358. WINENUMDATA::hwnd=(HWND)NULL;
  359. return FALSE;
  360. }
  361. }
  362.  
  363. BOOL WINENUM__CountChildWindowsProc(HWND hwndIndex,LPARAM lParam){
  364. if(hwndIndex){
  365. WINENUMDATA::currentIndex++;
  366. return TRUE;
  367. }
  368. else{
  369. return FALSE;
  370. }
  371. }
  372.  
  373. class WINENUM{
  374. private:
  375. void ClearData(){
  376. WINENUMDATA::currentIndex=0;
  377. WINENUMDATA::hwnd=(HWND)NULL;
  378. }
  379. public:
  380. WINENUM(){};
  381. HWND ChildWindow(HWND hwndOwner,int index){
  382. this->ClearData();
  383. EnumChildWindows(hwndOwner,(WNDENUMPROC)WINENUM__EnumChildWindowsProc,index);
  384. return WINENUMDATA::hwnd;
  385. }
  386. int CountChildWindows(HWND hwndOwner){
  387. this->ClearData();
  388. EnumChildWindows(hwndOwner,(WNDENUMPROC)WINENUM__CountChildWindowsProc,0);
  389. return WINENUMDATA::currentIndex;
  390. }
  391. };

All times are GMT -4. The time now is 5:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC