Menu

[r121]: / trunk / DisplayPowerOff / Platform.cpp  Maximize  Restore  History

Download this file

109 lines (80 with data), 2.9 kB

  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
#include "stdafx.h"
#include "Platform.h"
void Platform::InitCommonControls()
{
EXECUTE_ONCE()
constexpr auto initCommonControlsEx = INITCOMMONCONTROLSEX
{
sizeof INITCOMMONCONTROLSEX,
ICC_STANDARD_CLASSES | ICC_BAR_CLASSES | ICC_LINK_CLASS
};
VERIFY(InitCommonControlsEx(&initCommonControlsEx));
}
HICON Platform::LoadIconBest(HINSTANCE hInstance, int nIconId, bool smallIcon)
{
HICON hIcon = nullptr;
VERIFY_SUCCEEDED(LoadIconMetric(hInstance, MAKEINTRESOURCE(nIconId), smallIcon ? LIM_SMALL : LIM_LARGE, &hIcon));
return hIcon;
}
HICON Platform::LoadIconSize(HINSTANCE hInstance, int nIconId, bool smallIcon)
{
const auto hIcon = LoadImage(hInstance, MAKEINTRESOURCE(nIconId),
IMAGE_ICON,
GetSystemMetrics(smallIcon ? SM_CXSMICON : SM_CXICON),
GetSystemMetrics(smallIcon ? SM_CYSMICON : SM_CYICON),
LR_SHARED);
ASSERT(hIcon && "LoadImage");
return static_cast<HICON>(hIcon);
}
void Platform::CenterWindow(HWND hwnd)
{
ASSERT(hwnd);
auto hwndOwner = GetParent(hwnd);
if (!hwndOwner || !IsWindowVisible(hwndOwner) || IsIconic(hwndOwner))
{
hwndOwner = GetDesktopWindow();
}
ASSERT(hwndOwner);
RECT ownerRect, hwndRect, rect;
VERIFY(GetWindowRect(hwndOwner, &ownerRect));
VERIFY(GetWindowRect(hwnd, &hwndRect));
VERIFY(CopyRect(&rect, &ownerRect));
VERIFY(OffsetRect(&hwndRect, -hwndRect.left, -hwndRect.top));
VERIFY(OffsetRect(&rect, -rect.left, -rect.top));
VERIFY(OffsetRect(&rect, -hwndRect.right, -hwndRect.bottom));
VERIFY(SetWindowPos(hwnd, nullptr, ownerRect.left + rect.right / 2, ownerRect.top + rect.bottom / 2, 0, 0, SWP_NOSIZE | SWP_NOZORDER));
}
void Platform::CloseWindow(HWND hwnd)
{
VERIFY(PostMessage(hwnd, WM_CLOSE, 0, 0));
}
void Platform::SetIcon(HWND hwnd, HINSTANCE hInstance, int nIconId)
{
#pragma warning(push)
#pragma warning(disable: 26490)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(LoadIconSize(hInstance, nIconId, true)));
SendMessage(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadIconSize(hInstance, nIconId, false)));
#pragma warning(pop)
}
BOOL Platform::MoveMenuItem(HMENU hMenu, UINT uPosFrom, UINT uPosInsertAfter)
{
CharBuffer<256> szMenuText;
if (GetMenuString(hMenu, uPosFrom, szMenuText, szMenuText, MF_BYPOSITION) &&
InsertMenu(hMenu, uPosInsertAfter, MF_BYPOSITION, GetMenuItemID(hMenu, uPosFrom), szMenuText))
{
if (uPosFrom > uPosInsertAfter)
{
++uPosFrom;
}
return DeleteMenu(hMenu, uPosFrom, MF_BYPOSITION);
}
return FALSE;
}
BOOL Platform::SetMenuItemText(HMENU hMenu, UINT uId, LPTSTR szText)
{
#pragma warning(suppress: 26486)
DECLARE_CBSTRUCT(MENUITEMINFO, menuItemInfo);
menuItemInfo.dwTypeData = szText;
menuItemInfo.fMask = MIIM_STRING;
return SetMenuItemInfo(hMenu, uId, FALSE, &menuItemInfo);
}