Kawe Mazidjatari 8e81d99585 CppKore: fix potential font resource leak
As of commit 168ad9aabd624510045d56aabcbfeff6f4aaad6f, we can assign uniform fonts to all child controls, but only 1 control can own the font resource. This is by default false since we do not want to free the resource when destroying the child controls, but we do want to free it once the parent window gets destroyed.
2024-06-01 11:43:10 +02:00

50 lines
972 B
C++

#include "stdafx.h"
#include "Font.h"
namespace Drawing
{
Font::Font(HWND Handle, const HFONT hFont, const bool OwnsFont)
: _Handle(Handle), _NativeFont(hFont), _OwnsFont(OwnsFont)
{
}
Font::Font(HWND Handle, const Gdiplus::Font& FontObject)
: _Handle(Handle), _NativeFont(nullptr), _OwnsFont(true)
{
LOGFONTA FontParams{};
auto hDC = GetDC(Handle);
auto Gfx = Graphics::FromHDC(hDC);
FontObject.GetLogFontA(Gfx, &FontParams);
_NativeFont = CreateFontIndirectA(&FontParams);
delete Gfx;
ReleaseDC(Handle, hDC);
}
Font::~Font()
{
if (_OwnsFont && _NativeFont != nullptr)
DeleteObject(_NativeFont);
_NativeFont = nullptr;
_Handle = nullptr;
}
HFONT Font::GetFontHandle()
{
return _NativeFont;
}
std::unique_ptr<Gdiplus::Font> Font::GetFont()
{
auto hDC = GetDC(this->_Handle);
auto Result = std::make_unique<Gdiplus::Font>(hDC, this->_NativeFont);
ReleaseDC(this->_Handle, hDC);
return std::move(Result);
}
}