-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
165 lines (159 loc) · 4.46 KB
/
Source.cpp
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
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "legacy_stdio_definitions")
#include <windows.h>
#include <odbcinst.h>
#define DEFAULT_DPI 96
#define SCALEX(X) MulDiv(X, uDpiX, DEFAULT_DPI)
#define SCALEY(Y) MulDiv(Y, uDpiY, DEFAULT_DPI)
#define POINT2PIXEL(PT) MulDiv(PT, uDpiY, 72)
TCHAR szClassName[] = TEXT("Window");
BOOL GetScaling(HWND hWnd, UINT* pnX, UINT* pnY)
{
BOOL bSetScaling = FALSE;
const HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
if (hMonitor)
{
HMODULE hShcore = LoadLibrary(TEXT("SHCORE"));
if (hShcore)
{
typedef HRESULT __stdcall GetDpiForMonitor(HMONITOR, int, UINT*, UINT*);
GetDpiForMonitor* fnGetDpiForMonitor = reinterpret_cast<GetDpiForMonitor*>(GetProcAddress(hShcore, "GetDpiForMonitor"));
if (fnGetDpiForMonitor)
{
UINT uDpiX, uDpiY;
if (SUCCEEDED(fnGetDpiForMonitor(hMonitor, 0, &uDpiX, &uDpiY)) && uDpiX > 0 && uDpiY > 0)
{
*pnX = uDpiX;
*pnY = uDpiY;
bSetScaling = TRUE;
}
}
FreeLibrary(hShcore);
}
}
if (!bSetScaling)
{
HDC hdc = GetDC(NULL);
if (hdc)
{
*pnX = GetDeviceCaps(hdc, LOGPIXELSX);
*pnY = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(NULL, hdc);
bSetScaling = TRUE;
}
}
if (!bSetScaling)
{
*pnX = DEFAULT_DPI;
*pnY = DEFAULT_DPI;
bSetScaling = TRUE;
}
return bSetScaling;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hButton;
static HWND hEdit;
static HFONT hFont;
static UINT uDpiX = DEFAULT_DPI, uDpiY = DEFAULT_DPI;
switch (msg)
{
case WM_CREATE:
hButton = CreateWindow(TEXT("BUTTON"), TEXT("取得"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)IDOK, ((LPCREATESTRUCT)lParam)->hInstance, 0);
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), 0, WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_READONLY, 0, 0, 0, 0, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, 0);
SendMessage(hWnd, WM_APP, 0, 0);
PostMessage(hWnd, WM_COMMAND, IDOK, 0);
break;
case WM_SIZE:
MoveWindow(hButton, POINT2PIXEL(10), POINT2PIXEL(10), POINT2PIXEL(256), POINT2PIXEL(32), TRUE);
MoveWindow(hEdit, POINT2PIXEL(10), POINT2PIXEL(50), LOWORD(lParam) - POINT2PIXEL(20), HIWORD(lParam) - POINT2PIXEL(60), TRUE);
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
SetWindowText(hEdit, 0);
TCHAR szBuf[1024 * 4];
WORD wBufOut = 0;
SQLGetInstalledDrivers(szBuf, _countof(szBuf), &wBufOut);
LPWSTR pszBuf = szBuf;
do
{
SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)pszBuf);
SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)L"\r\n");
pszBuf = wcschr(pszBuf, L'\0') + 1;
} while (pszBuf[0] != L'\0');
SendMessage(hEdit, EM_SETSEL, 0, -1);
SetFocus(hEdit);
}
break;
case WM_NCCREATE:
{
const HMODULE hModUser32 = GetModuleHandle(TEXT("user32.dll"));
if (hModUser32)
{
typedef BOOL(WINAPI* fnTypeEnableNCScaling)(HWND);
const fnTypeEnableNCScaling fnEnableNCScaling = (fnTypeEnableNCScaling)GetProcAddress(hModUser32, "EnableNonClientDpiScaling");
if (fnEnableNCScaling)
{
fnEnableNCScaling(hWnd);
}
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
case WM_APP:
GetScaling(hWnd, &uDpiX, &uDpiY);
DeleteObject(hFont);
hFont = CreateFontW(-POINT2PIXEL(10), 0, 0, 0, FW_NORMAL, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, L"MS Shell Dlg");
SendMessage(hButton, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, 0);
break;
case WM_DPICHANGED:
SendMessage(hWnd, WM_APP, 0, 0);
break;
case WM_DESTROY:
DeleteObject(hFont);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR pCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
0,
LoadCursor(0,IDC_ARROW),
(HBRUSH)(COLOR_WINDOW + 1),
0,
szClassName
};
RegisterClass(&wndclass);
HWND hWnd = CreateWindow(
szClassName,
TEXT("インストール済のデータベースドライバ一覧"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
0,
0,
hInstance,
0
);
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}