aboutsummaryrefslogtreecommitdiff
path: root/WinUtils.h
blob: 2ab9fb899cdef3d51ccce52d7dae499601365f37 (plain)
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
#pragma once
#include <windows.h>
#include <string>

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif

/* void Cls_OnSizing(HWND hwnd, UINT edge, LPRECT prRect) */
#define HANDLE_WM_SIZING(hwnd, wParam, lParam, fn) \
    ((fn)((hwnd), (UINT)(wParam), (LPRECT)lParam), TRUE)
#define FORWARD_WM_SIZING(hwnd, edge, prRect, fn) \
    (void)(fn)((hwnd), WM_SIZING, (WPARAM)edge, (LPARAM)prRect)

inline BOOL UnadjustWindowRect(
    LPRECT prc,
    DWORD dwStyle,
    BOOL fMenu)
{
    RECT rc;
    SetRectEmpty(&rc);
    BOOL fRc = AdjustWindowRect(&rc, dwStyle, fMenu);
    if (fRc) {
        prc->left -= rc.left;
        prc->top -= rc.top;
        prc->right -= rc.right;
        prc->bottom -= rc.bottom;
    }
    return fRc;
}

inline BOOL UnadjustWindowRectEx(
    LPRECT prc,
    DWORD dwStyle,
    BOOL fMenu,
    DWORD dwExStyle)
{
    RECT rc;
    SetRectEmpty(&rc);
    BOOL fRc = AdjustWindowRectEx(&rc, dwStyle, fMenu, dwExStyle);
    if (fRc) {
        prc->left -= rc.left;
        prc->top -= rc.top;
        prc->right -= rc.right;
        prc->bottom -= rc.bottom;
    }
    return fRc;
}

inline BOOL UnadjustWindowRectExForDpi(
    LPRECT prc,
    DWORD dwStyle,
    BOOL fMenu,
    DWORD dwExStyle,
    UINT dpi)
{
    RECT rc;
    SetRectEmpty(&rc);
    BOOL fRc = AdjustWindowRectExForDpi(&rc, dwStyle, fMenu, dwExStyle, dpi);
    if (fRc) {
        prc->left -= rc.left;
        prc->top -= rc.top;
        prc->right -= rc.right;
        prc->bottom -= rc.bottom;
    }
    return fRc;
}

inline RECT Rect(POINT p1, POINT p2)
{
    return { p1.x, p1.y, p2.x, p2.y };
}

inline RECT Rect(POINT p1, SIZE s2)
{
    return { p1.x, p1.y, p1.x + s2.cx, p1.y + s2.cy };
}

inline HFONT CreateFont(LPCTSTR pFontFace, int iFontHeight, int cWeight, BOOL bItalic, BOOL bUnderline)
{
    return CreateFont(iFontHeight, 0, 0, 0, cWeight, bItalic, bUnderline, FALSE, ANSI_CHARSET,
        OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
        FIXED_PITCH | FF_DONTCARE, pFontFace);
}

inline std::string RegGetString(HKEY hKey, LPCSTR sValue, const std::string& strDef)
{
    CHAR buf[1024];
    DWORD len = (ARRAYSIZE(buf) - 1) * sizeof(CHAR);
    if (RegGetValueA(hKey, nullptr, sValue, RRF_RT_REG_SZ, nullptr, buf, &len) == ERROR_SUCCESS)
    {
        buf[len / sizeof(CHAR)] = TEXT('\0');
        return buf;
    }
    else
        return strDef;
}

inline std::string RegGetString(HKEY hKey, LPCSTR sSubKey, LPCSTR sValue, const std::string& strDef)
{
    CHAR buf[1024];
    DWORD len = (ARRAYSIZE(buf) - 1) * sizeof(CHAR);
    if (RegGetValueA(hKey, sSubKey, sValue, RRF_RT_REG_SZ, nullptr, buf, &len) == ERROR_SUCCESS)
    {
        buf[len / sizeof(CHAR)] = TEXT('\0');
        return buf;
    }
    else
        return strDef;
}

inline std::tstring RegGetString(HKEY hKey, LPCWSTR sValue, const std::tstring& strDef)
{
    WCHAR buf[1024];
    DWORD len = (ARRAYSIZE(buf) - 1) * sizeof(WCHAR);
    if (RegGetValueW(hKey, nullptr, sValue, RRF_RT_REG_SZ, nullptr, buf, &len) == ERROR_SUCCESS)
    {
        buf[len / sizeof(WCHAR)] = TEXT('\0');
        return buf;
    }
    else
        return strDef;
}

inline std::tstring RegGetString(HKEY hKey, LPCWSTR sSubKey, LPCWSTR sValue, const std::tstring& strDef)
{
    WCHAR buf[1024];
    DWORD len = (ARRAYSIZE(buf) - 1) * sizeof(WCHAR);
    if (RegGetValueW(hKey, sSubKey, sValue, RRF_RT_REG_SZ, nullptr, buf, &len) == ERROR_SUCCESS)
    {
        buf[len / sizeof(WCHAR)] = TEXT('\0');
        return buf;
    }
    else
        return strDef;
}

inline DWORD RegGetDWORD(HKEY hKey, LPCTSTR sValue, DWORD dwDef)
{
    DWORD data = 0;
    DWORD len = sizeof(data);
    if (RegGetValue(hKey, nullptr, sValue, RRF_RT_REG_DWORD, nullptr, &data, &len) == ERROR_SUCCESS)
        return data;
    else
        return dwDef;
}

inline DWORD RegGetDWORD(HKEY hKey, LPCTSTR sSubKey, LPCTSTR sValue, DWORD dwDef)
{
    DWORD data = 0;
    DWORD len = sizeof(data);
    if (RegGetValue(hKey, sSubKey, sValue, RRF_RT_REG_DWORD, nullptr, &data, &len) == ERROR_SUCCESS)
        return data;
    else
        return dwDef;
}

inline bool RegEnumKeyEx(_In_ HKEY hKey, _In_ DWORD dwIndex, std::tstring& strName)
{
    TCHAR name[256];
    DWORD len = ARRAYSIZE(name);
    if (RegEnumKeyEx(hKey, dwIndex, name, &len, nullptr, nullptr, nullptr, nullptr) == ERROR_SUCCESS)
    {
        strName = name;
        return true;
    }
    else
        return false;
}

inline HWND GetMDIClient(HWND hWnd)
{
    return FindWindowEx(hWnd, NULL, TEXT("MDICLIENT"), nullptr);
}

inline HWND GetMDIActive(HWND hWndMDIClient, BOOL* pbMaximized = nullptr)
{
    return (HWND) SendMessage(hWndMDIClient, WM_MDIGETACTIVE, 0, (LPARAM) pbMaximized);
}

inline bool IsMDIChild(HWND hWnd)
{
    return (GetWindowExStyle(hWnd) & WS_EX_MDICHILD) != 0;
}

inline HWND MyGetActiveWnd(HWND hWnd)
{
    HWND hActive = GetActiveWindow();
    if (hActive != NULL && IsMDIChild(hWnd))
        hActive = GetMDIActive(GetParent(hWnd));
    return hActive;
}

inline bool FindMenuPos(HMENU hBaseMenu, UINT myID, HMENU* pMenu, int* mpos)
{
    if (hBaseMenu == NULL)
    {
        *pMenu = NULL;
        *mpos = -1;
        return true;
    }

    for (int myPos = 0; myPos < GetMenuItemCount(hBaseMenu); ++myPos)
    {
        if (GetMenuItemID(hBaseMenu, myPos) == myID)
        {
            *pMenu = hBaseMenu;
            *mpos = myPos;
            return true;
        }

        HMENU hNewMenu = GetSubMenu(hBaseMenu, myPos);
        if (hNewMenu != NULL)
        {
            if (FindMenuPos(hNewMenu, myID, pMenu, mpos))
                return true;
        }
    }

    return false;
}

inline BOOL CALLBACK CountChildWindowsProc(HWND, LPARAM lParam)
{
    int* pCount = (int*) lParam;
    ++(*pCount);
    return TRUE;
}

inline int CountChildWindows(HWND hWnd)
{
    int count = 0;
    EnumChildWindows(hWnd, CountChildWindowsProc, (LPARAM) &count);
    return count;
}