1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
//Main.cpp // Program Shows How To Code
#include <windows.h> // High DPI (Dots Per Inch)
#include <cstdio> // Aware Apps
#include "DpiAware.h"
#define MyDebug
void SetMyProcessDpiAware() // 2000/XP Doesn't Have SetProcessDPIAware()
{ // So Only Call Function If Its Present
BOOL (__stdcall *pFn)(void);
HINSTANCE hInstance=LoadLibrary(L"user32.dll");
if(hInstance)
{
pFn=(BOOL (__stdcall*)(void))GetProcAddress(hInstance,"SetProcessDPIAware");
if(pFn)
pFn();
FreeLibrary(hInstance);
}
}
long fnWndProc_OnCreate(WndEventArgs& Wea) // Constructor For Main Window. Processes
{ // WM_CREATE Message
float sngFont, dpiX, dpiY, rxRatio, ryRatio;
HFONT hFont=NULL;
HWND hCtl=NULL;
FILE* fp=NULL;
HDC hDC;
#ifdef MyDebug
fp=fopen("Output.txt","w");
fprintf(fp,"Entering fnWndProc_OnCreate()\n");
fprintf(fp," Wea.hWnd = %p\n",Wea.hWnd);
fprintf(fp," IsProcessDPIAware() = %d\n",IsProcessDPIAware());
#endif
Wea.hIns=((LPCREATESTRUCT)Wea.lParam)->hInstance;
sngFont=8.0; // Set Font Size To 8.0. Get Handle To Device Context For Calls
hDC = GetDC(NULL); // To GetDeviceCaps(). This Latter Function Obtains Pixel Resolution
dpiX=(float)GetDeviceCaps(hDC, LOGPIXELSX); // Of Current Display Setting. Divide This By 96 To Obtain
dpiY=(float)GetDeviceCaps(hDC, LOGPIXELSY); // Scaling Factor. Note SizX() and SizY() macros in DpiAware.h.
rxRatio=(dpiX/96); // These Are Used To Scale ALL App Window/Control x, y, cx, and cy
ryRatio=(dpiY/96); // Dimensions. Note Use in MoveWindow() and CreateWindowEx() calls
#ifdef MyDebug // Below.
fprintf(fp," dpiX = %8.0f\n",dpiX); // When the app is set up this way, it will respond
fprintf(fp," dpiY = %8.0f\n",dpiY); // correctly to any system wide changes the user
fprintf(fp," rxRatio = %8.2f\n",rxRatio); // makes to DPI/Resolution settings in Control Panel.
fprintf(fp," ryRatio = %8.2f\n",ryRatio);
#endif
MoveWindow(Wea.hWnd, SizX(200), SizY(100), SizX(300), SizY(124), FALSE);
hFont=CreateFont(-MulDiv((int)sngFont,(int)dpiY,72),0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,0,0,0,0,L"Tahoma");
ReleaseDC(NULL,hDC);
SetWindowLongPtr(Wea.hWnd,GWLP_USERDATA,(LONG_PTR)hFont);
hCtl=CreateWindowEx(0,L"static",L"User Name",WS_CHILD|WS_VISIBLE|SS_RIGHT,SizX(10),SizY(12),SizX(90),SizY(16),Wea.hWnd,(HMENU)-1,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"",WS_CHILD|WS_VISIBLE|WS_TABSTOP,SizX(110),SizY(10),SizX(160),SizY(20),Wea.hWnd,(HMENU)IDC_USERNAME,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(0,L"static",L"Password",WS_CHILD|WS_VISIBLE|SS_RIGHT,SizX(10),SizY(36),SizX(90),SizY(16),Wea.hWnd,(HMENU)-1,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"",WS_CHILD|WS_VISIBLE|WS_TABSTOP,SizX(110),SizY(34),SizX(160),SizY(20),Wea.hWnd,(HMENU)IDC_PASSWORD,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(0,L"button",L"Submit",WS_CHILD|WS_VISIBLE|WS_TABSTOP,SizX(120),SizY(60),SizX(60),SizY(20),Wea.hWnd,(HMENU)IDC_SUBMIT,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(0,L"button",L"Cancel",WS_CHILD|WS_VISIBLE|WS_TABSTOP,SizX(200),SizY(60),SizX(60),SizY(20),Wea.hWnd,(HMENU)IDC_CANCEL,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
#ifdef MyDebug
fprintf(fp,"Leaving fnWndProc_OnCreate()\n");
fclose(fp);
#endif
return 0;
}
long fnWndProc_OnCommand(WndEventArgs& Wea)
{
switch(LOWORD(Wea.wParam))
{
case IDC_SUBMIT:
MessageBox(Wea.hWnd,L"You Clicked The Submit Button!",L"Button Click Report!",MB_OK);
break;
case IDC_CANCEL:
MessageBox(Wea.hWnd,L"You Clicked The Cancel Button!",L"You Want Out!",MB_OK);
SendMessage(Wea.hWnd,WM_CLOSE, 0, 0);
break;
}
return 0;
}
long fnWndProc_OnDestroy(WndEventArgs& Wea)
{
HFONT hFont=NULL;
hFont=(HFONT)GetWindowLongPtr(Wea.hWnd,GWLP_USERDATA);
if(hFont)
DeleteObject(hFont);
PostQuitMessage(0);
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].iMsg==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
wchar_t szClassName[]=L"DpiAware";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
memset(&wc,0,sizeof(wc));
SetMyProcessDpiAware();
wc.lpszClassName = szClassName; wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof(WNDCLASSEX); wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW; wc.cbWndExtra = sizeof(void*);
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,0,0,0,0,0,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
if(!IsDialogMessage(hWnd,&messages))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
return (int)messages.wParam;
}
| |