-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinUtil.h
291 lines (236 loc) · 9.69 KB
/
WinUtil.h
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Copyright 2021 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */
#define NO_COLOR (COLORREF) - 1
#define WIN_COL_WHITE RGB(0xff, 0xff, 0xff)
#define WIN_COL_BLACK RGB(0, 0, 0)
#define DRAGQUERY_NUMFILES 0xFFFFFFFF
int RectDx(const RECT& r);
int RectDy(const RECT& r);
POINT MakePoint(long x, long y);
SIZE MakeSize(long dx, long dy);
RECT MakeRect(long x, long y, long dx, long dy);
RECT GetClientRect(HWND);
Rect ClientRect(HWND hwnd);
Rect WindowRect(HWND hwnd);
Rect MapRectToWindow(Rect rect, HWND hwndFrom, HWND hwndTo);
void Edit_SelectAll(HWND hwnd);
void ListBox_AppendString_NoSort(HWND hwnd, WCHAR* txt);
bool IsValidHandle(HANDLE);
bool SafeCloseHandle(HANDLE*);
void FillWndClassEx(WNDCLASSEX& wcex, const WCHAR* clsName, WNDPROC wndproc);
void MoveWindow(HWND hwnd, Rect rect);
void MoveWindow(HWND hwnd, RECT* r);
bool IsOs64();
bool IsProcess64();
bool IsRunningInWow64();
bool IsProcessAndOsArchSame();
bool GetOsVersion(OSVERSIONINFOEX& ver);
const char* OsNameFromVerTemp(const OSVERSIONINFOEX& ver);
const char* GetWindowsVerTemp();
void LogLastError(DWORD err = 0);
void DbgOutLastError(DWORD err = 0);
bool RegKeyExists(HKEY keySub, const WCHAR* keyName);
WCHAR* ReadRegStr(HKEY keySub, const WCHAR* keyName, const WCHAR* valName);
char* ReadRegStrUtf8(HKEY keySub, const WCHAR* keyName, const WCHAR* valName);
WCHAR* ReadRegStr2(const WCHAR* keyName, const WCHAR* valName);
bool WriteRegStr(HKEY keySub, const WCHAR* keyName, const WCHAR* valName, const WCHAR* value);
bool ReadRegDWORD(HKEY keySub, const WCHAR* keyName, const WCHAR* valName, DWORD& value);
bool WriteRegDWORD(HKEY keySub, const WCHAR* keyName, const WCHAR* valName, DWORD value);
bool CreateRegKey(HKEY keySub, const WCHAR* keyName);
bool DeleteRegKey(HKEY keySub, const WCHAR* keyName, bool resetACLFirst = false);
TempWstr GetSpecialFolderTemp(int csidl, bool createIfMissing = false);
void DisableDataExecution();
bool RedirectIOToConsole();
bool RedirectIOToExistingConsole();
void HandleRedirectedConsoleOnShutdown();
TempWstr GetExePathTemp();
WCHAR* GetComputerName();
WCHAR* GetExeDir();
WCHAR* GetSystem32Dir();
WCHAR* GetCurrentDir();
void ChangeCurrDirToDocuments();
int FileTimeDiffInSecs(const FILETIME& ft1, const FILETIME& ft2);
WCHAR* ResolveLnk(const WCHAR* path);
bool CreateShortcut(const WCHAR* shortcutPath, const WCHAR* exePath, const WCHAR* args = nullptr,
const WCHAR* description = nullptr, int iconIndex = 0);
IDataObject* GetDataObjectForFile(const WCHAR* filePath, HWND hwnd = nullptr);
DWORD GetFileVersion(const WCHAR* path);
bool IsKeyPressed(int key);
bool IsShiftPressed();
bool IsAltPressed();
bool IsCtrlPressed();
HFONT CreateSimpleFont(HDC hdc, const WCHAR* fontName, int fontSize);
Rect ShiftRectToWorkArea(Rect rect, HWND hwnd = nullptr, bool bFully = false);
Rect GetWorkAreaRect(Rect rect, HWND hwnd);
void LimitWindowSizeToScreen(HWND hwnd, SIZE& size);
Rect GetFullscreenRect(HWND);
Rect GetVirtualScreenRect();
bool LaunchFile(const WCHAR* path, const WCHAR* params = nullptr, const WCHAR* verb = nullptr, bool hidden = false);
bool LaunchBrowser(const WCHAR* url);
bool LaunchBrowser(const char* url);
HANDLE LaunchProcess(const WCHAR* cmdLine, const WCHAR* currDir = nullptr, DWORD flags = 0);
bool CreateProcessHelper(const WCHAR* exe, const WCHAR* args);
bool LaunchElevated(const WCHAR* path, const WCHAR* cmdline);
bool IsProcessRunningElevated();
bool CanTalkToProcess(DWORD procId);
void PaintRect(HDC, Rect);
void PaintLine(HDC, Rect);
void DrawCenteredText(HDC hdc, Rect r, const WCHAR* txt, bool isRTL = false);
void DrawCenteredText(HDC, const RECT& r, const WCHAR* txt, bool isRTL = false);
Size TextSizeInHwnd(HWND, const WCHAR*, HFONT = nullptr);
SIZE TextSizeInHwnd2(HWND, const WCHAR*, HFONT);
Size TextSizeInDC(HDC, const WCHAR*);
bool IsFocused(HWND);
bool IsCursorOverWindow(HWND);
bool GetCursorPosInHwnd(HWND, Point&);
POINT GetCursorPosInHwnd(HWND);
void CenterDialog(HWND hDlg, HWND hParent = nullptr);
WCHAR* GetDefaultPrinterName();
bool CopyTextToClipboard(const WCHAR* text, bool appendOnly = false);
bool CopyImageToClipboard(HBITMAP hbmp, bool appendOnly);
bool IsWindowStyleSet(HWND hwnd, DWORD flags);
bool IsWindowStyleExSet(HWND hwnd, DWORD flags);
void SetWindowStyle(HWND hwnd, DWORD flags, bool enable);
void SetWindowExStyle(HWND hwnd, DWORD flags, bool enable);
bool IsRtl(HWND hwnd);
void SetRtl(HWND hwnd, bool isRtl);
Rect ChildPosWithinParent(HWND);
int GetSizeOfDefaultGuiFont();
HFONT GetDefaultGuiFont();
HFONT GetDefaultGuiFont(bool bold, bool italic);
HFONT GetDefaultGuiFontOfSize(int size);
IStream* CreateStreamFromData(ByteSlice);
ByteSlice GetDataFromStream(IStream* stream, HRESULT* resOpt);
ByteSlice GetStreamOrFileData(IStream* stream, const WCHAR* filePath);
bool ReadDataFromStream(IStream* stream, void* buffer, size_t len, size_t offset = 0);
uint GuessTextCodepage(const char* data, size_t len, uint defVal = CP_ACP);
WCHAR* NormalizeString(const WCHAR* str, int /* NORM_FORM */ form);
void ResizeHwndToClientArea(HWND hwnd, int dx, int dy, bool hasMenu);
void ResizeWindow(HWND, int dx, int dy);
void MessageBoxWarningSimple(HWND hwnd, const WCHAR* msg, const WCHAR* title = nullptr);
void MessageBoxNYI(HWND hwnd);
// schedule WM_PAINT at window's leasure
void ScheduleRepaint(HWND hwnd);
// do WM_PAINT immediately
void RepaintNow(HWND hwnd);
bool RegisterServerDLL(const WCHAR* dllPath, const WCHAR* args = nullptr);
bool UnRegisterServerDLL(const WCHAR* dllPath, const WCHAR* args = nullptr);
bool RegisterOrUnregisterServerDLL(const WCHAR* dllPath, bool install, const WCHAR* args = nullptr);
inline BOOL toBOOL(bool b) {
return b ? TRUE : FALSE;
}
inline bool fromBOOL(BOOL b) {
return b != 0;
}
inline bool tobool(BOOL b) {
return b != 0;
}
namespace win {
void ToForeground(HWND hwnd);
size_t GetTextLen(HWND hwnd);
TempWstr GetTextTemp(HWND hwnd);
void SetText(HWND hwnd, const WCHAR* txt);
void SetVisibility(HWND hwnd, bool visible);
bool HasFrameThickness(HWND hwnd);
bool HasCaption(HWND hwnd);
namespace menu {
void SetChecked(HMENU m, int id, bool isChecked);
bool SetEnabled(HMENU m, int id, bool isEnabled);
void Remove(HMENU m, int id);
// TODO: this doesn't recognize enum Cmd, why?
// void Remove(HMENU m, enum Cmd id);
void Empty(HMENU m);
void SetText(HMENU m, int id, const WCHAR* s);
const WCHAR* ToSafeString(AutoFreeWstr& s);
} // namespace menu
} // namespace win
struct DoubleBuffer {
HWND hTarget = nullptr;
HDC hdcCanvas = nullptr;
HDC hdcBuffer = nullptr;
HBITMAP doubleBuffer = nullptr;
Rect rect{};
DoubleBuffer(HWND hwnd, Rect rect);
DoubleBuffer(const DoubleBuffer&) = delete;
DoubleBuffer& operator=(const DoubleBuffer&) = delete;
~DoubleBuffer();
[[nodiscard]] HDC GetDC() const;
void Flush(HDC hdc) const;
};
class DeferWinPosHelper {
HDWP hdwp;
public:
DeferWinPosHelper();
~DeferWinPosHelper();
void End();
void SetWindowPos(HWND hwnd, Rect rc);
void SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
void MoveWindow(HWND hWnd, int x, int y, int cx, int cy, BOOL bRepaint = TRUE);
void MoveWindow(HWND hWnd, Rect r);
};
struct BitmapPixels {
u8* pixels;
Size size;
int nBytes;
int nBytesPerPixel;
int nBytesPerRow;
HBITMAP hbmp;
BITMAPINFO bmi;
HDC hdc;
};
struct RenderedBitmap {
HBITMAP hbmp{nullptr};
Size size = {};
HANDLE hMap = {};
RenderedBitmap(HBITMAP hbmp, Size size, HANDLE hMap = nullptr) : hbmp(hbmp), size(size), hMap(hMap) {
}
~RenderedBitmap();
[[nodiscard]] RenderedBitmap* Clone() const;
[[nodiscard]] HBITMAP GetBitmap() const;
[[nodiscard]] Size Size() const;
bool StretchDIBits(HDC hdc, Rect target) const;
};
void InitAllCommonControls();
Size GetBitmapSize(HBITMAP hbmp);
BitmapPixels* GetBitmapPixels(HBITMAP hbmp);
void FinalizeBitmapPixels(BitmapPixels* bitmapPixels);
COLORREF GetPixel(BitmapPixels* bitmap, int x, int y);
void UpdateBitmapColors(HBITMAP hbmp, COLORREF textColor, COLORREF bgColor);
ByteSlice SerializeBitmap(HBITMAP hbmp);
HBITMAP CreateMemoryBitmap(Size size, HANDLE* hDataMapping = nullptr);
bool BlitHBITMAP(HBITMAP hbmp, HDC hdc, Rect target);
double GetProcessRunningTime();
void RunNonElevated(const WCHAR* exePath);
void VariantInitBstr(VARIANT& urlVar, const WCHAR* s);
ByteSlice LoadDataResource(int resId);
bool DDEExecute(const WCHAR* server, const WCHAR* topic, const WCHAR* command);
void RectInflateTB(RECT& r, int top, int bottom);
void DivideRectH(const RECT& r, int y, int dy, RECT& r1, RECT& r2, RECT& r3);
void DivideRectV(const RECT& r, int x, int dx, RECT& r1, RECT& r2, RECT& r3);
HCURSOR GetCachedCursor(LPWSTR id);
void SetCursorCached(LPWSTR id);
void DeleteCachedCursors();
int GetMeasurementSystem();
bool TrackMouseLeave(HWND);
void TriggerRepaint(HWND);
HINSTANCE GetInstance();
Size ButtonGetIdealSize(HWND hwnd);
std::tuple<const u8*, DWORD, HGLOBAL> LockDataResource(int id);
bool IsValidDelayType(int type);
void HwndDpiAdjust(HWND, float* x, float* y);
void HwndSetText(HWND, std::string_view s);
HICON HwndSetIcon(HWND, HICON);
HICON HwndGetIcon(HWND);
void HwndInvalidate(HWND);
void HwndSetFont(HWND, HFONT);
HFONT HwndGetFont(HWND);
Size HwndMeasureText(HWND hwnd, const WCHAR* txt, HFONT font);
void HwndPositionToTheRightOf(HWND hwnd, HWND hwndRelative);
void HwndPositionInCenterOf(HWND hwnd, HWND hwndRelative);
void HwndSendCommand(HWND hwnd, int cmdId);
void TbSetButtonInfo(HWND hwnd, int buttonId, TBBUTTONINFO* info);
void TbGetPadding(HWND, int* padX, int* padY);
void TbSetPadding(HWND, int padX, int padY);
void TbGetMetrics(HWND hwnd, TBMETRICS* metrics);
void TbSetMetrics(HWND hwnd, TBMETRICS* metrics);
void TbGetRect(HWND hwnd, int buttonId, RECT* rc);