#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);
}