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?

A long time ago, this happened to me. I'll share but make no claim this is it. What was it? The Windows machine had no default printer selected or printer driver installed. As such, the fonts were not showing up in our app.

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);
        }
    }
}


///
void Font(CHOOSEFONT chft)
    {
        chFont=chft;
    }

    void DrawText(string strText,int PosX=0, int PosY=0)
    {
        SetLastError(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=CreateFontIndirect(chFont.lpLogFont);
        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);
    }

seems that i can lose the cf.lpLogFont, because it's a pointer :(
but i have the size(cf.lpLogFont->lfHeight), i only - tested - lose the font name :(
i see the the color(with white on back and it's transparent) and the size. the font name isn't used. if i'm not losing the pointer, so the problem must be with DrawText(). please tell me something

now works fine:

CHOOSEFONT ShowSelectFont(HWND hwnd=GetForegroundWindow())
{
    CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
    LOGFONT* lf = new LOGFONT;
    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
    ChooseFont(&cf);

        HFONT hf = CreateFontIndirect(lf);
        //lf->lfQuality=PROOF_QUALITY;
        cf.lpLogFont = lf;
    //delete lf; //i can't delete what i'm using it
            return cf;


}

//heres my image DrawText()
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

        //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=CreateFontIndirect(chFont.lpLogFont);
        HFONT holdfont=(HFONT)SelectObject((HDC)HBitmap,hFont);
        SetTextColor(HBitmap,chFont.rgbColors);

        ::DrawText(HBitmap, text, -1,&r,DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
        r.left+=PosX;
        r.top+=PosY;
        r.bottom+= PosX;
        r.right+= PosY;
        SetBkMode(HBitmap,TRANSPARENT);

        ::DrawText(HBitmap, text, -1,&r,DT_LEFT | DT_EXPANDTABS);

        SelectObject(HBitmap,holdfont);
        DeleteBrush(hFont);

        SelectObject(HBitmap,oldBrush);
        DeleteBrush(hBackBrush);

        SelectObject(HBitmap,oldPen);
        DeleteBrush(hBackPen);
    }

//heres how i draw it:
case WM_PAINT:
            {
                PAINTSTRUCT  ps;
                BeginPaint(inst->hwnd, &ps);
                image imgLabel(inst->intWidth+1,inst->intHeight+1);

                //back color
                /*pen penTransparent;
                imgLabel.Pen(penTransparent);
                imgLabel.Brush(inst->clrBackColor);
                imgLabel.DrawRectangle(0,0,imgLabel.width()+2,imgLabel.height()+2);*/
                SetDCBrushColor(imgLabel,inst->clrBackColor);
                SetDCBrushColor(ps.hdc,inst->clrBackColor);

                //draw image
                DrawHICONtoHDC( imgLabel, inst->imgtest);

                //draw text
                brush transparentbrush;
                imgLabel.Brush(transparentbrush);

                SetBkMode(imgLabel,TRANSPARENT);//text background transparent
                imgLabel.Font(inst->chFont);
                imgLabel.DrawText(inst->strCaption.c_str());

                //NULL the window region
                SetWindowRgn(inst->hwnd,NULL,FALSE);


                if(inst->blnTransparent==true)
                {
                    region lblRegion(imgLabel,GetPixel(imgLabel,0,0));
                    SetWindowRgn(inst->hwnd,lblRegion,FALSE);
                    TransparentBlt(ps.hdc,0,0,imgLabel.width(), imgLabel.height(), imgLabel,0,0,imgLabel.width(), imgLabel.height(),GetPixel(imgLabel,0,0));

                }
                else
                {
                    BitBlt(ps.hdc,0,0,imgLabel.width(), imgLabel.height(), imgLabel,0,0,SRCCOPY);
                }


                EndPaint(inst->hwnd, &ps);
                return 0;
            }
            break;
//i create the memory DC with control size+1. for don't lose any pixel;
//if your draw it's strange, like not perfect design, please change the back color or brush

thanks for all

see the image:
1 - the blue and the image on left couner is the form;
2 - the button is behind the label.
3 - the image and text it's a transparent label(usin regions functions. the big size, can slow). if by some reason, you can't see the text perfectly, use another backcolor or brush.

Undefined behaviour.

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; //ADDRESS OF LOCAL VARIABLE!
    cf.rgbColors = RGB(0,0,0);
    cf.lpLogFont->lfStrikeOut=FALSE;
    cf.lpLogFont->lfUnderline=FALSE;
    cf.lpLogFont->lfHeight=-MulDiv(12, GetDeviceCaps(GetDC(hwnd), LOGPIXELSY), 72);
    _tcscpy(cf.lpLogFont->lfFaceName, "Arial" );
    if(ChooseFont(&cf))
    {
        HFONT hf = CreateFontIndirect(&lf); //HF is unused and dies when the function finishes.
        if(hf)
        {
            return cf; //Memory Leak for HF.
        }
        else
        {
            MessageBox(hwnd, "Font creation failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
            //return  (CHOOSEFONT)(HFONT)GetStockObject(DEFAULT_GUI_FONT);
        }
    }
}

that's why i gived you the update function:

CHOOSEFONT ShowSelectFont(HWND hwnd=GetForegroundWindow())
{
    CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
    LOGFONT* lf = new LOGFONT;
    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
    ChooseFont(&cf);
        HFONT hf = CreateFontIndirect(lf);
        //lf->lfQuality=PROOF_QUALITY;
        cf.lpLogFont = lf;
    //delete lf; //i can't delete what i'm using it
            return cf;
}

Your update function is no different. You're leaking "hf".

maybe you have right, but i can't delete a new pointer, after return it. i belive.

how can i draw a string, transparent, clear?
what i mean is, depending on font name, i can see a white color arround the letters :(
how can avoid that effect?
SetBKMode() is transparent(or i can use a HBRUSH transparent(it's the same))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.