#include "windows.h" #include "commctrl.h" #define Assert(x) { if (!(x)) _asm int 3 } #include "CDC.H" ///////////////////////////////////////////////////////////////////////////// // CreateDlgControls - lays out a dialog template // // Win95 doesn't support CreateWindowExW (UNICODE version) so I had to // make some changes to this function. BOOL CreateDlgControls( HWND hWnd, HWND hwndParent, LPCTSTR res_id, HINSTANCE hInst) { typedef unsigned short int wchar; HRSRC hrsrc; HGLOBAL hg; BYTE *pb; wchar *pwch; int cb; int i; DLGTEMPLATE *pdlt; char buf[50]; char *p; int fsize = -1; char fname[128]; #pragma pack(1) struct _DLGITEMTEMPLATE { DWORD style; DWORD dwExtendedStyle; short x; short y; short cx; short cy; unsigned short id; WORD wUnk2; WORD classID; } *pdlit; #pragma pack() HFONT hFont = (HFONT)GetStockObject(ANSI_VAR_FONT); int base_x,base_y; // Calculate appropriate dialog base units { HDC hdc; TEXTMETRIC tm; hdc = GetDC(NULL); SelectObject(hdc,hFont); GetTextMetrics(hdc,&tm); ReleaseDC(NULL,hdc); base_x = tm.tmAveCharWidth+1; base_y = tm.tmHeight; } hrsrc = FindResource(hInst,res_id,RT_DIALOG); if (!hrsrc) { MessageBox(hWnd,"Couldn't load window controls layout.","Error",MB_OK); return FALSE; } cb = SizeofResource(hInst,hrsrc); hg = LoadResource(hInst,hrsrc); pb = (BYTE *)LockResource(hg); pdlt = (DLGTEMPLATE *)pb; pb += sizeof(DLGTEMPLATE); // Adjust window size { RECT r; int dlg_height; DWORD dwStyle = GetWindowLong(hWnd,GWL_STYLE); Assert(pdlt->x==0 && pdlt->y==0); if (pdlt->style & WS_MINIMIZEBOX) dwStyle |= WS_MINIMIZEBOX; if (pdlt->style & WS_MAXIMIZEBOX) dwStyle |= WS_MAXIMIZEBOX; if (pdlt->style & WS_SYSMENU) dwStyle |= WS_SYSMENU; SetWindowLong(hWnd,GWL_STYLE,dwStyle); dlg_height = MulDiv(pdlt->cy,base_y,8); if (pdlt->style & WS_CAPTION) dlg_height += GetSystemMetrics(SM_CYCAPTION); GetWindowRect(hWnd,&r); ScreenToClient(hwndParent,(LPPOINT)&r); MoveWindow(hWnd,r.left,r.top,MulDiv(pdlt->cx,base_x,4),dlg_height,FALSE); } // switch to wide characters pwch = (wchar *)pb; while (*pwch++); // skip the MENU NAME while (*pwch++); // skip the CLASS NAME p = buf; while (*pwch) // skip the CAPTION { *p++ = (char)*pwch; pwch++; } *p = '\0'; SetWindowText(hWnd,buf); // set the window CAPTION // Skip the font information if present if (pdlt->style & DS_SETFONT) { ((unsigned short *)pwch)++; fsize = *pwch; p = fname; do { *p = *(char *)pwch; p++; } while (*pwch++); *p = 0; } // Loop through the controls in the template for (i=0; i < pdlt->cdit; i++) { // int cch; char szClass[30];; HWND hControl; // Align to a DWORD boundary pwch = (wchar *)(((long)pwch + 3) & ~3); pdlit = (struct _DLGITEMTEMPLATE *)pwch; // Set pointer to UNICODE string for class name if (0xffff == pdlit->wUnk2) { pwch = (wchar *)&pdlit[1]; switch (pdlit->classID) { case 0x0080: ::lstrcpy(szClass,"BUTTON"); break; case 0x0081: ::lstrcpy(szClass,"EDIT"); pdlit->dwExtendedStyle |= WS_EX_CLIENTEDGE; break; case 0x0082: ::lstrcpy(szClass,"STATIC"); break; case 0x0083: ::lstrcpy(szClass,"LISTBOX"); break; case 0x0084: ::lstrcpy(szClass,"SCROLLBAR"); break; case 0x0085: ::lstrcpy(szClass,"COMBOBOX"); break; case 0x0079: ::lstrcpy(szClass,WC_LISTVIEW); break; case 0x000f: ::lstrcpy(szClass,UPDOWN_CLASS); break; default: Assert(FALSE); } } else { int j; pwch = &pdlit->wUnk2; //pszClass = "SysListView32"; for (j=0; *pwch; j++) { szClass[j] = *(char *)pwch; pwch++; } szClass[j] = '\0'; if (j & 1) pwch++; } // Skip the title... p = buf; while (*pwch) { *p++ = (char)*pwch; pwch++; } pwch++; *p++ = '\0'; if ((p-buf) & 1) pwch++; // Use UNICODE CreateWindowEx function DWORD dwStyle = pdlit->style | WS_CHILD; hControl = CreateWindowEx(pdlit->dwExtendedStyle, szClass, buf, dwStyle, MulDiv(pdlit->x,base_x,4), MulDiv(pdlit->y,base_y,8), MulDiv(pdlit->cx,base_x,4), MulDiv(pdlit->cy,base_y,8), hWnd, (HMENU)pdlit->id, hInst, NULL); if (!hControl) { char buf[100]; wsprintf(buf,"CreateWindow for control failed with GetLastError==%d",GetLastError()); MessageBox(hWnd,buf,"Error",MB_OK); return FALSE; } SendMessage(hControl,WM_SETFONT,(WPARAM)hFont,MAKELPARAM(0,0)); } return TRUE; } ///////////////////////////////////////////////////////////////////////////// // Shamelessly stolen from Win32 sample generic.c // // //////////////////////////////////////////////////////////////////////////// BOOL CenterWindow(HWND hwndChild, HWND hwndParent) { RECT rChild, rParent; int wChild, hChild, wParent, hParent; int wScreen, hScreen, xNew, yNew; HDC hdc; // Get the Height and Width of the child window GetWindowRect (hwndChild, &rChild); wChild = rChild.right - rChild.left; hChild = rChild.bottom - rChild.top; // Get the Height and Width of the parent window GetWindowRect (hwndParent, &rParent); wParent = rParent.right - rParent.left; hParent = rParent.bottom - rParent.top; // Get the display limits hdc = GetDC (hwndChild); wScreen = GetDeviceCaps (hdc, HORZRES); hScreen = GetDeviceCaps (hdc, VERTRES); ReleaseDC (hwndChild, hdc); // Calculate new X position, then adjust for screen xNew = rParent.left + ((wParent - wChild) /2); if (xNew < 0) { xNew = 0; } else if ((xNew+wChild) > wScreen) { xNew = wScreen - wChild; } // Calculate new Y position, then adjust for screen yNew = rParent.top + ((hParent - hChild) /2); if (yNew < 0) { yNew = 0; } else if ((yNew+hChild) > hScreen) { yNew = hScreen - hChild; } return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER); } unsigned g_rand; WORD myrand(void) { g_rand = 1664525L * g_rand + 1013904223L; return HIWORD(g_rand); } void mysrand(void) { g_rand = ::timeGetTime(); }