heres my function for select the font:
CHOOSEFONT ShowSelectFont(HWND hwnd=GetForegroundWindow())
{
CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
LOGFONT lf;
cf.Flags = CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
cf.hwndOwner = hwnd;
cf.lpLogFont = &lf;
cf.rgbColors = RGB(0,0,0);
cf.lpLogFont->lfStrikeOut=FALSE;
cf.lpLogFont->lfUnderline=FALSE;
cf.lpLogFont->lfHeight=-MulDiv(12, GetDeviceCaps(GetDC(hwnd), LOGPIXELSY), 72);//change font size
_tcscpy(cf.lpLogFont->lfFaceName, "Arial" ); //we must include tchar.h
if(ChooseFont(&cf))
{
HFONT hf = CreateFontIndirect(&lf);
if(hf)
{
return cf;
}
else
{
MessageBox(hwnd, "Font creation failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
//return (CHOOSEFONT)(HFONT)GetStockObject(DEFAULT_GUI_FONT);
}
}
}
heres how i use it:
CHOOSEFONT testfont=ShowSelectFont();
lbltest.setFont(testfont);
//on label class:
private:
CHOOSEFONT chFont;
public:
void setFont(const CHOOSEFONT font)
{
chFont=font;
}
//on WM_PAINT:
imglabel.TextColor(inst->chFont.rgbColors);
imglabel.Font(inst->chFont);
//inst is the label pointer, that gives me the chFont.
//imglabel is object image class
void Font(CHOOSEFONT chft)
{
chFont=chft;
}
void DrawText(string strText,int PosX=0, int PosY=0)
{
// geting the text rectangle
RECT r = { 0, 0, 0, 0 };
char *text=(char*)strText.c_str();
//change the position of the text
r.left=PosX;
r.top=PosY;
r.bottom=HBitmap.Width() + PosY;
r.right=HBitmap.Height() + PosX;
SetBkMode(HBitmap,TRANSPARENT);
//draw the text
HBRUSH hBackBrush=imgBrush;
HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
HPEN hBackPen=(HPEN)imgPen;
HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
HFONT *hFont=(HFONT*)chFont.lpLogFont; //lpLogFont it's a pointer
HFONT holdfont=(HFONT)SelectObject((HDC)HBitmap,*hFont);
SetTextColor(HBitmap,chFont.rgbColors);
::DrawText(HBitmap, text, -1,&r,DT_LEFT);
SelectObject(HBitmap,holdfont);
DeleteBrush(*hFont);
SelectObject(HBitmap,oldBrush);
DeleteBrush(hBackBrush);
SelectObject(HBitmap,oldPen);
DeleteBrush(hBackPen);
}
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646832%28v=vs.85%29.aspx
by some reason, the font name and size isn't selected, but the color is.
what i'm doing wrong?