Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
CDialogWithThemeFontImpl.h
zlib License:
Copyright (c) 2014-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 <string.h>
#include <cstddef>
#include <vector>
#include <windows.h>
#include <uxtheme.h>
#include <vssym32.h>
#include <atlbase.h>
#include <atlwin.h>
#include <atlconv.h>
#include <atlapp.h>
#include <atldlgs.h>
#include <atlgdi.h>
#pragma comment(lib, "uxtheme.lib")
#if _MSC_VER < 1600 && !defined __cpluspluc_cli
# pragma push_macro("nullptr")
# define nullptr 0
#endif
class DialogTemplateThemeFont
{
public:
bool Init(ATL::_U_STRINGorID id)
{
HMODULE hmod = WTL::ModuleHelper::GetResourceInstance();
HRSRC hrsrc = ::FindResource(hmod, id.m_lpstr, RT_DIALOG);
DWORD size = ::SizeofResource(hmod, hrsrc);
HGLOBAL hg = ::LoadResource(hmod, hrsrc);
const void* p = ::LockResource(hg);
if (p == nullptr)
{
return false;
}
WTL::CLogFont font;
if (!GetThemeFont(font))
{
Fallback(p, size);
return false;
}
m_buffer.clear();
const DLGTEMPLATE* dlgTemplate = static_cast<const DLGTEMPLATE*>(p);
if (!ATL::_DialogSplitHelper::IsDialogEx(dlgTemplate))
{
Fallback(p, size);
return false;
}
const BYTE* pbFont = ATL::_DialogSizeHelper::GetFontSizeField(dlgTemplate);
m_buffer.insert(m_buffer.end(), reinterpret_cast<const BYTE*>(p), pbFont);
reinterpret_cast<WTL::DLGTEMPLATEEX*>(&m_buffer[0])->style |= DS_SETFONT;
WTL::CDC dc;
dc.CreateCompatibleDC();
WORD fontSize = static_cast<WORD>(font.GetDeciPointHeight(dc) / 10);
PushBack(reinterpret_cast<const BYTE*>(&fontSize), sizeof (WORD));
WORD fontWeight = static_cast<WORD>(font.lfWeight);
PushBack(reinterpret_cast<const BYTE*>(&fontWeight), sizeof (WORD));
m_buffer.push_back(font.lfItalic);
m_buffer.push_back(font.lfCharSet);
ATL::CT2CW fontFaceName(font.lfFaceName);
PushBack(reinterpret_cast<const BYTE*>(fontFaceName.m_psz), (std::wcslen(fontFaceName.m_psz) + 1) * sizeof (WCHAR));
m_buffer.resize(m_buffer.size() + 3 & ~3);
const BYTE* pItemFirst = reinterpret_cast<const BYTE*>(ATL::_DialogSplitHelper::FindFirstDlgItem(dlgTemplate));
const BYTE* pEnd = reinterpret_cast<const BYTE*>(p) + size;
m_buffer.insert(m_buffer.end(), pItemFirst, pEnd);
return true;
}
bool IsValid() const { return !m_buffer.empty(); }
const DLGTEMPLATE* GetTemplatePtr() const
{
return reinterpret_cast<const DLGTEMPLATE*>(&m_buffer[0]);
}
private:
bool GetThemeFont(LOGFONT& lf)
{
HTHEME hTheme = ::OpenThemeData(nullptr, VSCLASS_TEXTSTYLE);
if (hTheme == nullptr)
{
return false;
}
LOGFONTW tmp = {};
HRESULT hr = ::GetThemeFont(hTheme, nullptr, TEXT_CONTROLLABEL, 0, TMT_FONT, &tmp);
CloseThemeData(hTheme);
if (FAILED(hr))
{
return false;
}
#ifdef UNICODE
lf = tmp;
#else
lf.lfHeight = tmp.lfHeight;
lf.lfWidth = tmp.lfWidth;
lf.lfEscapement = tmp.lfEscapement;
lf.lfOrientation = tmp.lfOrientation;
lf.lfWeight = tmp.lfWeight;
lf.lfItalic = tmp.lfItalic;
lf.lfUnderline = tmp.lfUnderline;
lf.lfStrikeOut = tmp.lfStrikeOut;
lf.lfCharSet = tmp.lfCharSet;
lf.lfOutPrecision = tmp.lfOutPrecision;
lf.lfClipPrecision = tmp.lfClipPrecision;
lf.lfQuality = tmp.lfQuality;
lf.lfPitchAndFamily = tmp.lfPitchAndFamily;
strncpy_s(lf.lfFaceName, ATL::CW2A(tmp.lfFaceName), _TRUNCATE);
#endif
return true;
}
void Fallback(const void* originalResource, DWORD size)
{
const BYTE* p = reinterpret_cast<const BYTE*>(originalResource);
m_buffer.assign(p, p + size);
}
void PushBack(const BYTE* p, std::size_t n)
{
m_buffer.insert(m_buffer.end(), p, p + n);
}
std::vector<BYTE> m_buffer;
};
#if _WTL_VER >= 0x0900
template<class T, class TBase = ATL::CWindow>
#else
template<class T, class TBase = ATL::CDialogImpl<T, ATL::CWindow>>
#endif
class ATL_NO_VTABLE CDialogWithThemeFontImpl
: public WTL::CIndirectDialogImpl<T, DialogTemplateThemeFont, TBase>
{
public:
void DoInitTemplate()
{
m_Template.Init(static_cast<T*>(this)->IDD);
}
void DoInitControls() {}
private:
// must define IDD in derived class T
using WTL::CIndirectDialogImpl<T, DialogTemplateThemeFont, TBase>::IDD;
};
template <class T, class TBase = WTL::CPropertyPageWindow>
class ATL_NO_VTABLE CPropertyPageWithThemeFontImpl
: public WTL::CPropertyPageImpl<T, TBase>
{
public:
CPropertyPageWithThemeFontImpl(ATL::_U_STRINGorID title = (LPCTSTR)nullptr)
: CPropertyPageImpl(title)
{
m_Template.Init(static_cast<T*>(this)->IDD);
if (m_Template.IsValid())
{
m_psp.dwFlags |= PSP_DLGINDIRECT;
m_psp.pResource = m_Template.GetTemplatePtr();
}
}
DialogTemplateThemeFont m_Template;
};
#if _MSC_VER < 1600
# pragma pop_macro("nullptr")
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.