-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
206 lines (201 loc) · 5.13 KB
/
Copy pathSource.cpp
File metadata and controls
206 lines (201 loc) · 5.13 KB
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
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "dwmapi")
#include <windows.h>
#include <dwmapi.h>
#include "resource.h"
#define CLIENT_WIDTH 300
#define CLIENT_HEIGHT 128
#define FONT_SIZE (-75)
#define BKCOLOR RGB(134, 161, 110)
TCHAR szClassName[] = TEXT("clock");
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HGLOBAL hGlobal = NULL;
static HANDLE hFontResource = NULL;
static HFONT hFont = NULL;
static SYSTEMTIME st = {};
static WCHAR szTime[32] = {};
static HDC hMemDC = 0;
static HBITMAP hBitmap;
static HBITMAP hOldBitmap;
switch (msg)
{
case WM_CREATE:
{
HRSRC hRes = FindResource(0, MAKEINTRESOURCE(IDR_FONT1), RT_FONT);
if (!hRes)
{
return -1;
}
DWORD dwSize = SizeofResource(0, hRes);
if (!dwSize)
{
return -1;
}
hGlobal = LoadResource(0, hRes);
if (!hGlobal)
{
return -1;
}
LPVOID pBuffer = LockResource(hGlobal);
if (!pBuffer)
{
return -1;
}
DWORD dwFonts;
hFontResource = AddFontMemResourceEx(pBuffer, dwSize, 0, &dwFonts);
if (!hFontResource)
{
return -1;
}
}
hFont = CreateFontW(FONT_SIZE, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, L"DigitalNumber");
SendMessage(hWnd, WM_TIMER, 0, 0);
SetTimer(hWnd, 0x1234, 1, NULL);
break;
case WM_TIMER:
{
SYSTEMTIME st_ = {};
double vt = 0.0, vt_ = 0.0;
GetLocalTime(&st_);
SystemTimeToVariantTime(&st, &vt);
SystemTimeToVariantTime(&st_, &vt_);
if (vt != vt_) {
st = st_;
wsprintfW(szTime, L"%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond);
// 描画
RECT rect = {};
GetClientRect(hWnd, &rect);
// 背景塗りつぶし
{
HBRUSH hBrush = CreateSolidBrush(BKCOLOR);
FillRect(hMemDC, &rect, hBrush);
DeleteObject(hBrush);
}
// 文字描画
{
HFONT hOldFont = (HFONT)SelectObject(hMemDC, hFont);
int nOldBkMode = SetBkMode(hMemDC, TRANSPARENT);
COLORREF cOldColor = SetTextColor(hMemDC, RGB(124, 150, 101));
DrawText(hMemDC, L"88:88:88", -1, &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
SetTextColor(hMemDC, RGB(38, 41, 52));
DrawText(hMemDC, szTime, -1, &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
SetTextColor(hMemDC, cOldColor);
SetBkMode(hMemDC, nOldBkMode);
SelectObject(hMemDC, hOldFont);
}
InvalidateRect(hWnd, 0, 0);
}
}
break;
case WM_APP:
if (hMemDC) {
if (hOldBitmap) {
SelectObject(hMemDC, hOldBitmap);
}
if (hBitmap) {
DeleteObject(hBitmap);
hBitmap = 0;
}
DeleteDC(hMemDC);
hMemDC = 0;
}
break;
case WM_SIZE:
{
SendMessage(hWnd, WM_APP, 0, 0);
HDC hdc = GetDC(hWnd);
if (hdc) {
hMemDC = CreateCompatibleDC(hdc);
if (hMemDC) {
RECT rect;
GetClientRect(hWnd, &rect);
hBitmap = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
}
ReleaseDC(hWnd, hdc);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rect = {};
GetClientRect(hWnd, &rect);
BitBlt(hdc, 0, 0, rect.right, rect.bottom, hMemDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
}
break;
case WM_NCHITTEST:
wParam = DefWindowProc(hWnd, msg, wParam, lParam);
if (wParam == HTCLIENT)
return HTCAPTION;
else
return wParam;
case WM_DESTROY:
KillTimer(hWnd, 0x1234);
SendMessage(hWnd, WM_APP, 0, 0);
if (hFont)
DeleteObject(hFont);
if (hFontResource)
RemoveFontMemResourceEx(hFontResource);
if (hGlobal)
FreeResource(hGlobal);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd)
{
MSG msg;
HBRUSH hBrush = CreateSolidBrush(BKCOLOR);
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1)),
LoadCursor(0,IDC_ARROW),
hBrush,
0,
szClassName
};
RegisterClass(&wndclass);
DWORD dwWindowStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
RECT rect = {};
SetRect(&rect, 0, 0, CLIENT_WIDTH, CLIENT_HEIGHT);
AdjustWindowRect(&rect, dwWindowStyle, 0);
HWND hWnd = CreateWindowEx(
WS_EX_TOPMOST,
szClassName,
TEXT("clock"),
dwWindowStyle,
CW_USEDEFAULT,
0,
rect.right - rect.left,
rect.bottom - rect.top,
0,
0,
hInstance,
0
);
// change caption color for windows 11
COLORREF bkColor = BKCOLOR;
BOOL SET_CAPTION_COLOR = SUCCEEDED(DwmSetWindowAttribute(
hWnd, 35/*DWMWINDOWATTRIBUTE::DWMWA_CAPTION_COLOR*/,
&bkColor, sizeof(COLORREF)));
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DeleteObject(hBrush);
return (int)msg.wParam;
}