forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
102 lines (89 loc) · 2.41 KB
/
Util.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
/* Copyright (C) 2002 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "Util.h"
#include "Rainmeter.h"
#include "DialogAbout.h"
#include "System.h"
UINT GetUniqueID()
{
static UINT id = 0;
return id++;
}
WCHAR* GetString(UINT id)
{
LPWSTR pData = nullptr;
int len = LoadString(GetRainmeter().GetResourceInstance(), id, (LPWSTR)&pData, 0);
return len > 0 ? pData : L"";
}
std::wstring GetFormattedString(UINT id, ...)
{
LPWSTR pBuffer = nullptr;
va_list args = nullptr;
va_start(args, id);
DWORD len = FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetString(id),
0,
0,
(LPWSTR)&pBuffer,
0,
&args);
va_end(args);
std::wstring tmpSz(len ? pBuffer : L"", len);
if (pBuffer) LocalFree(pBuffer);
return tmpSz;
}
HICON GetIcon(UINT id, bool large)
{
HINSTANCE hExe = GetModuleHandle(nullptr);
HINSTANCE hComctl = GetModuleHandle(L"Comctl32");
if (hComctl)
{
// Try LoadIconMetric for better quality with high DPI
auto loadIconMetric = (decltype(LoadIconMetric)*)GetProcAddress(hComctl, "LoadIconMetric");
if (loadIconMetric)
{
HICON icon = { 0 };
HRESULT hr = loadIconMetric(hExe, MAKEINTRESOURCE(id), large ? LIM_LARGE : LIM_SMALL, &icon);
if (SUCCEEDED(hr))
{
return icon;
}
}
}
return (HICON)LoadImage(
hExe,
MAKEINTRESOURCE(id),
IMAGE_ICON,
GetSystemMetrics(large ? SM_CXICON : SM_CXSMICON),
GetSystemMetrics(large ? SM_CYICON : SM_CYSMICON),
LR_SHARED);
}
HICON GetIconBySize(UINT id, int size)
{
return (HICON)LoadImage(
GetModuleHandle(nullptr),
MAKEINTRESOURCE(id),
IMAGE_ICON,
size,
size,
LR_SHARED);
}
void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
{
#ifdef _DEBUG
if (function && file)
{
LogErrorF(L"Invalid parameter detected. Function: \"%s\" File: \"%s:%d\" Expression: \"%s\"", function, file, line, expression);
return;
}
#endif
if (GetRainmeter().GetDebug())
{
LogError(L"Invalid parameter detected");
}
}