aboutsummaryrefslogtreecommitdiff
path: root/WinUtils.h
blob: d020727a2f14e3ff1144d3ba108465369a4d765f (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
#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 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,
        DEFAULT_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::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 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 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;
}