Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to draw a GDI + text independent of DPI?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 0
Jean-Milost Rey...

Posts: 20
Registered: 11/4/10
How to draw a GDI + text independent of DPI?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2014 3:07 PM
Hello,

I'm drawing a text using GDI+. I recently noticed that this text is automatically scaled when the DPI is changed. Is there a way to make the GDI+ text drawing independent of the DPI? E.g. I want to draw a text up to 20 pixels, regardless of the DPI. Is it possible? How to do this?

Below is a sample code. I want to draw the first text with a constant size, regardless of the DPI, and the second text normally:
        case WM_PAINT:
        {
            inherited::WndProc(message);
 
            Canvas->Brush->Style = bsSolid;
            Canvas->Brush->Color = clWhite;
            Canvas->FillRect(ClientRect);
 
            // get GDI+ graphics from canvas
            Gdiplus::Graphics graphics(Canvas->Handle);
 
            // set text rendering hint
            graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintSystemDefault);
 
            std::auto_ptr<Gdiplus::Font>         pFont(new Gdiplus::Font(Canvas->Handle, Font->Handle));
            std::auto_ptr<Gdiplus::SolidBrush>   pBrush(new Gdiplus::SolidBrush(Gdiplus::Color(255, 0, 0, 0)));
            std::auto_ptr<Gdiplus::StringFormat> pFormat(new Gdiplus::StringFormat());
 
            Gdiplus::FontFamily fontFamily;
            pFont->GetFamily(&fontFamily);
 
            std::auto_ptr<Gdiplus::Font> pFont2(new Gdiplus::Font(&fontFamily, pFont->GetSize(),
                    pFont->GetStyle(), Gdiplus::UnitPixel));
            Gdiplus::Unit test = pFont->GetUnit();
            Gdiplus::Unit test2 = pFont2->GetUnit();
 
            pFormat->SetAlignment(Gdiplus::StringAlignmentNear);
            pFormat->SetLineAlignment(Gdiplus::StringAlignmentNear);
 
            Gdiplus::StringFormatFlags flags = Gdiplus::StringFormatFlagsBypassGDI;
            //flags = (Gdiplus::StringFormatFlags)(flags | Gdiplus::StringFormatFlagsDirectionRightToLeft);
            //flags = (Gdiplus::StringFormatFlags)(flags | Gdiplus::StringFormatFlagsDirectionVertical);
            //flags = (Gdiplus::StringFormatFlags)(flags | Gdiplus::StringFormatFlagsNoWrap);
            //flags = (Gdiplus::StringFormatFlags)(flags | Gdiplus::StringFormatFlagsNoClip);
            pFormat->SetFormatFlags(flags);
 
            pFormat->SetTrimming(Gdiplus::StringTrimmingEllipsisCharacter);
            pFormat->SetHotkeyPrefix(Gdiplus::HotkeyPrefixNone);
 
            std::wstring text = L"This is a sample code";
 
            Gdiplus::Unit prevPageUnit = graphics.GetPageUnit();
 
            try
            {
                graphics.SetPageUnit(Gdiplus::UnitPixel);
 
                // draw text
                graphics.DrawString(text.c_str(), text.length(), pFont2.get(), Gdiplus::RectF(ClientRect.Left,
                        ClientRect.Top, ClientWidth, ClientHeight), pFormat.get(), pBrush.get());
            }
            __finally
            {
                graphics.SetPageUnit(prevPageUnit);
            }
 
            // draw text 2
            graphics.DrawString(text.c_str(), text.length(), pFont.get(), Gdiplus::RectF(ClientRect.Left,
                    ClientRect.Top + 25, ClientWidth, ClientHeight), pFormat.get(), pBrush.get());
 
            return;
        }


Regards
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02