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
|
// cl NewDpi6.cpp /O1 /Os kernel32.lib user32.lib gdi32.lib 92,672 Bytes; Release; x64; VC19; LIBCMT Static Linkage /MT
// cl NewDpi6.cpp /O1 /Os /GS- TCLib.lib kernel32.lib user32.lib gdi32.lib 9,216 Bytes; Release; x64; VC19; TCLib Static Linkage
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <Windows.h>
//#define TCLib
#ifdef TCLib
#pragma warning(disable:4996 4565 4244 4100)
#include "stdlib.h"
#include "stdio.h"
#else
#pragma warning(disable:4244 4100 4996)
#include <stdlib.h>
#include <stdio.h>
#endif
#include "NewDpi6.h"
HFONT FindFont(WndEventArgs& Wea, int& iDpi)
{
HWND hCtl = NULL;
HFONT hFont = NULL;
HFONT hTmp = NULL;
HDC hDC = NULL;
int iFont = 8;
TEXTMETRIC tm;
RECT rc;
int nHeight;
int iDiff;
hFont=(HFONT)GetWindowLongPtr(Wea.hWnd,GWLP_USERDATA);
if(hFont)
DeleteObject(hFont);
hCtl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"",WS_CHILD,Siz(110),Siz(10),Siz(160),Siz(20),Wea.hWnd,(HMENU)IDC_TEST,Wea.hIns,0);
GetClientRect(hCtl,&rc);
DestroyWindow(hCtl);
hDC = GetDC(NULL);
do
{
nHeight=-MulDiv(iFont, iDpi, 72.0);
hFont=CreateFont(nHeight,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,0,0,0,0,L"Tahoma");
hTmp=(HFONT)SelectObject(hDC,hFont);
GetTextMetrics(hDC,&tm);
iDiff=rc.bottom-tm.tmHeight;
if(iDiff<=1)
{
SetWindowLongPtr(Wea.hWnd,GWLP_USERDATA,(LONG_PTR)hFont);
SelectObject(hDC,hTmp);
break;
}
else
{
DeleteObject(SelectObject(hDC,hTmp));
iFont++;
}
}while(true);
ReleaseDC(Wea.hWnd,hDC);
return hFont;
}
void SizeWindows(HWND hWnd, RECT* pRect, int& iDpi)
{
HFONT hFont = NULL;
HWND hCtl = 0;
MoveWindow(hWnd,pRect->left,pRect->top,pRect->right-pRect->left,pRect->bottom-pRect->top,TRUE);
hFont=(HFONT)GetWindowLongPtr(hWnd,GWLP_USERDATA);
hCtl=GetDlgItem(hWnd,IDC_lblUserName);
MoveWindow(hCtl,Siz(10),Siz(12),Siz(90),Siz(16),TRUE);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
SetWindowText(hCtl,L"User Name");
hCtl=GetDlgItem(hWnd,IDC_txtUserName);
MoveWindow(hCtl,Siz(110),Siz(10),Siz(160),Siz(20),TRUE);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=GetDlgItem(hWnd,IDC_lblPassWord);
MoveWindow(hCtl,Siz(10),Siz(36),Siz(90),Siz(16),TRUE);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
SetWindowText(hCtl,L"Password");
hCtl=GetDlgItem(hWnd,IDC_txtPassWord);
MoveWindow(hCtl,Siz(110),Siz(34),Siz(160),Siz(20),TRUE);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=GetDlgItem(hWnd,IDC_btnSubmit);
MoveWindow(hCtl,Siz(120),Siz(60),Siz(60),Siz(20),TRUE);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
SetWindowText(hCtl,L"Submit");
hCtl=GetDlgItem(hWnd,IDC_btnCancel);
MoveWindow(hCtl,Siz(200),Siz(60),Siz(60),Siz(20),TRUE);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
SetWindowText(hCtl,L"Cancel");
return;
}
LRESULT fnWndProc_OnCreate(WndEventArgs& Wea)
{
int iDpi = 0;
HWND hCtl = NULL;
HFONT hFont = NULL;
RECT rc;
Wea.hIns = ((LPCREATESTRUCT)Wea.lParam)->hInstance;
iDpi = GetDpiForWindow(Wea.hWnd);
hFont=FindFont(Wea, iDpi);
hCtl=CreateWindowEx(0,L"static",L"User Name",WS_CHILD|WS_VISIBLE|SS_RIGHT,0,0,0,0,Wea.hWnd,(HMENU)IDC_lblUserName,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"",WS_CHILD|WS_VISIBLE|WS_TABSTOP,0,0,0,0,Wea.hWnd,(HMENU)IDC_txtUserName,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(0,L"static",L"Password",WS_CHILD|WS_VISIBLE|SS_RIGHT,0,0,0,0,Wea.hWnd,(HMENU)IDC_lblPassWord,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"",WS_CHILD|WS_VISIBLE|WS_TABSTOP,0,0,0,0,Wea.hWnd,(HMENU)IDC_txtPassWord,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(0,L"button",L"Submit",WS_CHILD|WS_VISIBLE|WS_TABSTOP,0,0,0,0,Wea.hWnd,(HMENU)IDC_btnSubmit,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
hCtl=CreateWindowEx(0,L"button",L"Cancel",WS_CHILD|WS_VISIBLE|WS_TABSTOP,0,0,0,0,Wea.hWnd,(HMENU)IDC_btnCancel,Wea.hIns,0);
SendMessage(hCtl,WM_SETFONT,(WPARAM)hFont,0);
rc.left=Siz(200), rc.top=Siz(100), rc.right=Siz(500), rc.bottom=Siz(224);
SizeWindows(Wea.hWnd, &rc, iDpi);
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)
{
DestroyWindow(Wea.hWnd);
return 0;
}
LRESULT fnWndProc_OnCommand(WndEventArgs& Wea)
{
switch(LOWORD(Wea.wParam))
{
case IDC_btnSubmit:
return btnSubmit_Click(Wea);
case IDC_btnCancel:
return btnCancel_Click(Wea);
}
return DefWindowProc(Wea.hWnd, WM_CTLCOLOREDIT, Wea.wParam, Wea.lParam);
}
LRESULT fnWndProc_OnDpiChanged(WndEventArgs& Wea)
{
int iDpi = 0;
RECT* pRect = NULL;
iDpi=LOWORD(Wea.wParam);
pRect=(RECT*)Wea.lParam;
FindFont(Wea, iDpi);
SizeWindows(Wea.hWnd,pRect,iDpi);
return 0;
}
LRESULT fnWndProc_OnDestroy(WndEventArgs& Wea)
{
HFONT hFont = NULL;
hFont=(HFONT)GetWindowLongPtr(Wea.hWnd,GWLP_USERDATA);
DeleteObject(hFont);
PostQuitMessage(0);
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(size_t 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"NewDpi6";
WNDCLASS wc;
HWND hWnd;
MSG Msg;
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
memset(&wc,0,sizeof(WNDCLASS));
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),
RegisterClass(&wc);
hWnd=CreateWindow(szClassName,L"DpiAware V2",WS_OVERLAPPEDWINDOW,0,0,0,0,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&Msg,NULL,0,0))
{
if(!IsDialogMessage(hWnd,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return (int)Msg.wParam;
}
|