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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
//#define TCLib
#include <windows.h>
#ifdef TCLib
#include "stdio.h"
#else
#include <stdio.h>
#endif
#include "HiDpiEx04.h"
#define Debug
#ifdef Debug
FILE* fp=NULL;
#endif
void SetMyProcessDpiAware()
{
BOOL (__stdcall* pFn)(void);
HINSTANCE hInstance=LoadLibrary(L"user32.dll");
if(hInstance)
{
pFn=(BOOL (__stdcall*)(void))GetProcAddress(hInstance,"SetProcessDPIAware");
if(pFn)
pFn();
FreeLibrary(hInstance);
}
}
#ifdef Debug
BOOL IsMyProcessDpiAware()
{
BOOL (__stdcall* pFn)(void);
BOOL blnReturn=FALSE;
HINSTANCE hInstance=LoadLibrary(L"user32.dll");
if(hInstance)
{
pFn=(BOOL (__stdcall*)(void))GetProcAddress(hInstance,"IsProcessDPIAware");
if(pFn)
blnReturn=pFn();
FreeLibrary(hInstance);
}
return blnReturn;
}
#endif
LRESULT fnWndProc_OnCreate(WndEventArgs& Wea)
{
BOOL blnDpiAware=FALSE;
DpiData* pDpiData=NULL;
double dblFont=8.0;
char szBuffer[16];
HANDLE hHeap=NULL;
HFONT hFont=NULL;
double dpiX,dpiY;
HDC hDC=NULL;
HWND hCtl;
#ifdef Debug
fp=fopen("Output.txt","w");
fprintf(fp,"Entering fnWndProc_OnCreate()\r\n");
fprintf(fp," Wea.hWnd = %p\r\n",Wea.hWnd);
#endif
Wea.hIns = ((LPCREATESTRUCT)Wea.lParam)->hInstance;
hHeap = GetProcessHeap();
blnDpiAware = IsProcessDPIAware();
#ifdef Debug
fprintf(fp," blnDpiAware = %d\r\n",blnDpiAware);
#endif
hDC = GetDC(NULL);
dpiX = (double)GetDeviceCaps(hDC, LOGPIXELSX);
dpiY = (double)GetDeviceCaps(hDC, LOGPIXELSY);
pDpiData = (DpiData*)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(DpiData));
if(!pDpiData)
return -1;
SetWindowLongPtr(Wea.hWnd,0*sizeof(void*),(LONG_PTR)pDpiData);
pDpiData->rx = dpiX/96.0;
pDpiData->ry = dpiY/96.0;
#ifdef Debug
fprintf(fp," pDpiData = %p\r\n",pDpiData);
#ifdef TCLib
FltToCh(szBuffer,dpiX,8,3,'.',true);
fprintf(fp," dpiX = %s\r\n",szBuffer);
FltToCh(szBuffer,dpiY,8,3,'.',true);
fprintf(fp," dpiY = %s\r\n",szBuffer);
FltToCh(szBuffer, pDpiData->rx, 6, 3, '.',true);
fprintf(fp," pDpiData->rx = %s\r\n",szBuffer);
FltToCh(szBuffer, pDpiData->ry, 6, 3, '.',true);
fprintf(fp," pDpiData->ry = %s\r\n",szBuffer);
#else
fprintf(fp," dpiX = %f\r\n",dpiX);
fprintf(fp," dpiY = %f\r\n",dpiY);
fprintf(fp," pDpiData->rx = %6.3f\n",pDpiData->rx);
fprintf(fp," pDpiData->ry = %6.3f\n",pDpiData->ry);
#endif
#endif
MoveWindow(Wea.hWnd,SizX(200),SizY(100),SizX(300),SizY(124),FALSE);
hFont=CreateFont(-MulDiv(dblFont,dpiY,72),0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,0,0,0,0,L"Tahoma");
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);
ReleaseDC(Wea.hWnd,hDC);
#ifdef Debug
fprintf(fp,"Leaving fnWndProc_OnCreate()\r\n\r\n");
#endif
return 0;
}
LRESULT btnSubmit_Click(WndEventArgs& Wea)
{
MessageBox(Wea.hWnd,L"You Clicked The Submit Button",L"Button Click Report",MB_OK);
return 0;
}
LRESULT btnCancel_Click(WndEventArgs& Wea)
{
MessageBox(Wea.hWnd,L"You Clicked The Cancel Button",L"Button Click Report",MB_OK);
return 0;
}
LRESULT fnWndProc_OnCommand(WndEventArgs& Wea)
{
switch(LOWORD(Wea.wParam))
{
case IDC_SUBMIT:
return btnSubmit_Click(Wea);
case IDC_CANCEL:
return btnCancel_Click(Wea);
}
return DefWindowProc(Wea.hWnd, WM_CTLCOLOREDIT, Wea.wParam, Wea.lParam);
}
LRESULT fnWndProc_OnDestroy(WndEventArgs& Wea)
{
DpiData* pDpiData=NULL;
BOOL blnFree=FALSE;
HFONT hFont=NULL;
#ifdef Debug
fprintf(fp,"Entering fnWndProc_OnDestroy()\r\n");
fprintf(fp," Wea.hWnd = %p\r\n",Wea.hWnd);
#endif
pDpiData=(DpiData*)GetWindowLongPtr(Wea.hWnd,0*sizeof(void*));
blnFree=HeapFree(GetProcessHeap(),0,pDpiData);
#ifdef Debug
fprintf(fp," pDpiData = %p\r\n",pDpiData);
fprintf(fp," blnFree = %d\r\n",blnFree);
#endif
hFont=(HFONT)GetWindowLongPtr(Wea.hWnd,GWLP_USERDATA);
blnFree=DeleteObject(hFont);
#ifdef Debug
fprintf(fp," hFont = %p\r\n",hFont);
fprintf(fp," blnFree(hFont1) = %d\r\n",blnFree);
#endif
PostQuitMessage(0);
#ifdef Debug
fprintf(fp,"Leaving fnWndProc_OnDestroy()\r\n");
fclose(fp);
#endif
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 wWinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPTSTR lpszArgument, int iShow)
{
wchar_t szClassName[]=L"HiDpiEx04";
MSG messages;
WNDCLASS wc;
HWND hWnd;
memset(&wc,0,sizeof(WNDCLASS));
SetMyProcessDpiAware();
wc.lpszClassName = szClassName, wc.lpfnWndProc = fnWndProc;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION), wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wc.hInstance = hIns, wc.hCursor = LoadCursor(NULL,IDC_ARROW),
wc.cbWndExtra = sizeof(void*);
RegisterClass(&wc);
hWnd=CreateWindow(szClassName,L"Dpi Aware",WS_OVERLAPPEDWINDOW,0,0,0,0,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
if(!IsDialogMessage(hWnd,&messages))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
return messages.wParam;
}
|