Skip to content

Instantly share code, notes, and snippets.

@egtra
Created May 10, 2016 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egtra/30aee7f7dc51bf85e75af17928c419e8 to your computer and use it in GitHub Desktop.
Save egtra/30aee7f7dc51bf85e75af17928c419e8 to your computer and use it in GitHub Desktop.
/*
CThemeFontForControl.h
zlib License:
Copyright (c) 2016 Egtra
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#pragma once
#include <windows.h>
#include <uxtheme.h>
#include <vssym32.h>
#include <atlbase.h>
#include <atlwin.h>
#include <atlapp.h>
#include <atlgdi.h>
class CThemeFontForControlBase
{
public:
LRESULT OnGetFont(UINT, WPARAM, LPARAM, _Out_ BOOL& bHandled)
{
if (m_font)
{
bHandled = TRUE;
return reinterpret_cast<LRESULT>(m_font.m_hFont);
}
else
{
bHandled = FALSE;
return 0;
}
}
protected:
void UpdateThemeFontObject()
{
m_font = nullptr;
HTHEME hTheme = ::OpenThemeData(nullptr, VSCLASS_TEXTSTYLE);
if (hTheme == nullptr)
{
return;
}
LOGFONTW lf = {};
HRESULT hr = ::GetThemeFont(hTheme, nullptr, TEXT_LABEL, 0, TMT_FONT, &lf);
CloseThemeData(hTheme);
if (FAILED(hr))
{
return;
}
m_font = CreateFontIndirectW(&lf);
}
void UpdateControlFontImpl(_In_ HWND hwnd)
{
UpdateThemeFontObject();
LPARAM lFont = static_cast<LPARAM>(::SendMessage(hwnd, WM_GETFONT, 0, 0));
if (lFont == 0)
{
lFont = reinterpret_cast<LPARAM>(GetStockObject(DEFAULT_GUI_FONT));
}
::EnumChildWindows(hwnd, UpdateControlFontCallback, lFont);
}
private:
static BOOL CALLBACK UpdateControlFontCallback(_In_ HWND hwnd, LPARAM lFont)
{
::SendMessage(hwnd, WM_SETFONT, static_cast<WPARAM>(lFont), TRUE);
return TRUE;
}
WTL::CFont m_font;
protected:
#if _MSC_VER >= 1900
CThemeFontForControlBase() = default;
CThemeFontForControlBase(CThemeFontForControlBase&&) = delete;
CThemeFontForControlBase(const CThemeFontForControlBase&) = delete;
CThemeFontForControlBase& operator=(CThemeFontForControlBase&&) = delete;
CThemeFontForControlBase& operator=(const CThemeFontForControlBase&) = delete;
~CThemeFontForControlBase() = default;
#endif
};
template<typename T>
class CThemeFontForControl
: public CThemeFontForControlBase
{
BEGIN_MSG_MAP(CThemeFontForControl<T>)
MESSAGE_HANDLER(WM_INITDIALOG, OnUpdateControlFont)
MESSAGE_HANDLER(WM_THEMECHANGED, OnUpdateControlFont)
MESSAGE_HANDLER(WM_GETFONT, OnGetFont)
END_MSG_MAP()
public:
LRESULT OnUpdateControlFont(UINT, WPARAM, LPARAM, _Out_ BOOL& bHandled)
{
UpdateControlFont();
bHandled = FALSE;
return 0;
}
void UpdateControlFont()
{
UpdateControlFontImpl(((T*)this)->m_hWnd);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment