-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_cand_ui.cpp
177 lines (156 loc) · 5.77 KB
/
single_cand_ui.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
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef UNICODE
#define UNICODE
#endif // !UNICODE
#include "./single_cand_ui.h"
#include <comdef.h>
#include <ctime>
#include <d2d1.h>
#include <dwmapi.h>
#include <dwrite.h>
#include <fstream>
#include <iostream>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "dwrite.lib")
ID2D1Factory *factory;
ID2D1HwndRenderTarget *target;
ID2D1SolidColorBrush *solid_brush;
IDWriteFactory *w_factory;
IDWriteTextFormat *w_format;
IDWriteTextLayout *w_layout;
HWND cand_ui_hwnd = NULL;
// std::wstring fontname = L"Courier";
std::wstring fontname = L"微软雅黑";
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void CreateSource();
void DrawString(std::wstring str, float fontSize, float x, float y, float r,
float g, float b, float a);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.lpszClassName = L"single_cand_ui";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClassEx(&wc);
cand_ui_hwnd = CreateWindowEx(0, wc.lpszClassName, L"cand_ui", WS_POPUP,
100, // x
100, // y
640, // nWidth
480, // nHeight
NULL, NULL, hInstance, NULL);
if (cand_ui_hwnd == NULL) {
MessageBox(NULL, TEXT("NULL!"), TEXT("caption"),
MB_OKCANCEL | MB_ICONINFORMATION);
}
CreateSource();
ShowWindow(cand_ui_hwnd, nCmdShow);
UpdateWindow(cand_ui_hwnd);
// DrawString("1. hello\n2. what\n3. fany\n4. full\n5. world\n6 .window\n7. "
// "time\n8. hey",
// 8, 0, 0, 0, 1, 1, 1);
// UpdateWindow(cand_ui_hwnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void CreateSource() {
MARGINS mar = {-1};
DwmExtendFrameIntoClientArea(cand_ui_hwnd, &mar);
D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &factory);
factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN,
D2D1_ALPHA_MODE_PREMULTIPLIED)),
D2D1::HwndRenderTargetProperties(cand_ui_hwnd, D2D1::SizeU(640, 480),
D2D1_PRESENT_OPTIONS_IMMEDIATELY),
&target);
target->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.0f, 0.0f), &solid_brush);
target->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
reinterpret_cast<IUnknown **>(&w_factory));
w_factory->CreateTextFormat(fontname.c_str(), NULL, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, 10.0f, L"en-us",
&w_format);
}
void DrawString(std::wstring str, float fontSize, float x, float y, float r,
float g, float b, float a) {
target->BeginDraw();
// 这可以把窗口的背景颜色给清除成透明色,主要在于最后一个指定 a 的参数
target->Clear(D2D1::ColorF(0, 0, 0, 0));
// 清除成白色的背景
// target-> Clear(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f));
// 清除成黑色的背景
// target-> Clear(D2D1::ColorF(0.0f, 0.0f, 0.0f, 1.0f));
// target->Clear(D2D1::ColorF(r, g, b, a));
RECT re;
GetClientRect(cand_ui_hwnd, &re);
FLOAT dpix, dpiy;
dpix = static_cast<float>(re.right - re.left);
dpiy = static_cast<float>(re.bottom - re.top);
HRESULT res = w_factory->CreateTextLayout(
std::wstring(str.begin(), str.end()).c_str(), str.length() + 1, w_format,
dpix, dpiy, &w_layout);
if (SUCCEEDED(res)) {
DWRITE_TEXT_RANGE range;
range.startPosition = 0;
range.length = str.length();
w_layout->SetFontSize(fontSize, range);
solid_brush->SetColor(D2D1::ColorF(r, g, b, a));
target->DrawTextLayout(D2D1::Point2F(x, y), w_layout, solid_brush);
w_layout->Release();
w_layout = NULL;
}
target->EndDraw();
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uiMessage, WPARAM wParam,
LPARAM lParam) {
switch (uiMessage) {
case WM_CREATE:
break;
case WM_PAINT:
DrawString(L"ll'zi\n1. 量子 ov\n2. 两字 ab\n3. 梁子 mv\n4. 良子 dv\n5. 凉子 dv\n6. 亮子 wv\n7. 梁紫 ms\n8. 两 ad", 18, 0, 0, 0, 1, 1, 1);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
// case WM_LBUTTONDOWN:
// 获取鼠标坐标
// int xPos = GET_X_LPARAM(lParam);
// int yPos = GET_Y_LPARAM(lParam);
// 将鼠标事件传递给下层窗口
// HWND hUnderlyingWindow = WindowFromPoint({xPos, yPos});
// SendMessage(hUnderlyingWindow, uiMessage, wParam, lParam);
// break;
// case WM_LBUTTONUP:
// break;
// case WM_MOUSEMOVE:
// // 获取鼠标坐标
// POINT pt;
// GetCursorPos(&pt);
// ScreenToClient(hWnd, &pt);
//
// // TODO: 检查鼠标位置是否在透明区域
// // 如果在透明区域,将消息传递给下层窗口
// // return DefWindowProc(hWnd, uiMessage, wParam, lParam);
//
// break;
default:
return DefWindowProc(hWnd, uiMessage, wParam, lParam);
}
return 0;
}