Sign in to follow this  
Roxypinky

[GDI+] How to properly load the system font into GDI+

Recommended Posts

Hello everyone! I'd like to know how to properly load the default system font into GDI+. Currently I'm loading the font into an HDC using SelectObect and GetStockObject(DEFAULT_GUI_FONT) and then pass that to the constructor of a Gdiplus::Font object but I'm sure there a better way to do this. My probrem is that I'm making a property list (like those you see in Visual Studio) but when I spawn a EDIT control over the value field, the text rendered by the EDIT control isn't exactly the same, characters are more close together etc... If I used GDI instead of GDI+ to render the font I get the exact same result... but it feels kind of a kludge to switch to GDI to render that part while everything else use GDI+. Anyway, I'm not sure if I'm doing something wrong while loading the font into GDI+ (can't seem find a way to load the system into directly into GDI+ without first getting form GDI), or maybe the answer is simply as both system don't render the font in the same way. I don't think the font rendering quality is the problem though, both are using ClearType. Anyone have any hindsight into this that could help me? Thanks a lot -Roxy

Share this post


Link to post
Share on other sites
Emulating GDI Font behaviour with GDI+ is a Pain. I strongly advise you not to go that Route.
GDI+ uses different rendering Algorithms, with different spacing, and a Floating Point coordinate system.
You may get lucky if you use StringFormat::GenericTypographic() and then pass that Format to Graphics::DrawString(). GenericTypographic() allows you to behave like GDI in terms of Spacing. But I am not sure if this applies to your Case. There is other GDI+ Flag you need to tweak: SetTextRenderingHint().
I don't see a Problem using GDI in your code. GDI+ is not a replacement for GDI. Is more like an Extension. And besides GDI is much faster.
For example you cannot possibly do efficient Double Buffering with GDI+.
I still rely on GDI for that.

Share this post


Link to post
Share on other sites
There's no such thing as "the" system default font. Different things have different fonts. SystemParemetersInfo can you get your finer resolution.

That being said, it's not strictly wrong to use DEFAULT_GUI_FONT. However, you're not getting what you think you're getting. Especially if your goal is to mimic some other control. Sending the control a WM_GETFONT is the best way to accomplish that.

Share this post


Link to post
Share on other sites
Thanks for the replies :)

Unfortunately StringFormat::GenericTypographic() didn't do the trick, for some reason it didn't change the spacing (GDI use a much shorter spacing). I was already calling SetTextRenderingHint(TextRenderingHintClearTypeGridFit). But anyway, I was more looking into a way to change how GDI render because I think GDI+ gives a much better result. Do you think that's possible? Maybe subclassing the edit control to handle the rendering (that looks like a bad idea already lol). I know GDI+ is lot slower than GDI but I decided to use it anyway because I needed to load PNG images, but I hid all the actual calls to GDI+ into a class and I plan to change that use AGG in the future which gives an impressive quality and is quite fast.

Thanks for the info Mike, I thought I was getting the font from the user settings with DEFAULT_GUI_FONT. However, looking at SystemParemetersInfo I couldn't find anything to get the font the user chooses (other than the use used to render icon titles)

Thanks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this