(Win32 refresh) DialogUtil
Fixes a 20 year old font leak issue which can be avoided by storing the font handle in a unique_ptr.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include "archutils/Win32/ErrorStrings.h"
|
#include "archutils/Win32/ErrorStrings.h"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
// Create*Font copied from MFC's CFont
|
// Create*Font copied from MFC's CFont
|
||||||
|
|
||||||
@@ -43,17 +44,27 @@ static HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName)
|
|||||||
return ::CreatePointFontIndirect(&logFont);
|
return ::CreatePointFontIndirect(&logFont);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DialogUtil::SetHeaderFont( HWND hdlg, int nID )
|
struct FontDeleter
|
||||||
{
|
{
|
||||||
ASSERT( hdlg != nullptr );
|
void operator()(HFONT font) const
|
||||||
|
{
|
||||||
|
if (font)
|
||||||
|
{
|
||||||
|
::DeleteObject(font);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
HWND hControl = ::GetDlgItem( hdlg, nID );
|
void DialogUtil::SetHeaderFont(HWND hdlg, int nID)
|
||||||
ASSERT( hControl != nullptr );
|
{
|
||||||
|
ASSERT(hdlg != nullptr);
|
||||||
|
|
||||||
// TODO: Fix font leak
|
HWND hControl = ::GetDlgItem(hdlg, nID);
|
||||||
const int FONT_POINTS = 16;
|
ASSERT(hControl != nullptr);
|
||||||
HFONT hfont = CreatePointFont( FONT_POINTS*10, "Arial Black" );
|
|
||||||
::SendMessage( hControl, WM_SETFONT, (WPARAM)hfont, TRUE );
|
static std::unique_ptr<std::remove_pointer<HFONT>::type, FontDeleter> hfont(CreatePointFont(16 * 10, "Arial Black"));
|
||||||
|
|
||||||
|
::SendMessage(hControl, WM_SETFONT, (WPARAM)hfont.get(), TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DialogUtil::LocalizeDialogAndContents( HWND hdlg )
|
void DialogUtil::LocalizeDialogAndContents( HWND hdlg )
|
||||||
|
|||||||
Reference in New Issue
Block a user