Controls forecolor and text size glitch/issue

leorob88

Well-known member
Joined
Dec 31, 2020
Messages
56
Programming Experience
1-3
I'm working on a setting for my app to change color for the controls (basically, i want to create a dark theme). I'm almost done with it but I noticed this issue.
I'm changing the comboboxes draw mode from Normal to OwnerDrawFixed so I can change the backcolor and forecolor. Changing Draw Mode, however, makes the letters in the combobox appear different: not only the text gets slightly moved up-right (1 pixel, no big deal), some letters get larger or take more space between one another.
In the pic i uploaded you can check the comboboxes are exactly the same, the content though changes. Is there a way to prevent this? I could get around this handling differently the form area, but before that i'd rather avoid the issue in the first place.

No big deal with the code, as I said I'm changing the comboboxes draw mode from Normal to OwnerDrawFixed and I'm setting backcolor and forecolor, nothing else.
There's also a handler for the dropdown list, but it should involve only that, i suppose. If you think the problem could be there, just tell me, this is it (it's used for every combobox):
C#:
private void darktheme_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                var combo = sender as ComboBox;
                int index = e.Index;
                var brush = new SolidBrush(Color.FromArgb(220, 220, 220));
                e.DrawBackground();
                e.Graphics.DrawString(combo.Items[index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }
        }
 

Attachments

  • Immagine.png
    Immagine.png
    1.8 KB · Views: 13
Chances are that the Win32 user implementation has some kerning settings, but those settings are not set on the DC that is wrapped by the e.Graphics object that you get.
 
Use StringFormat.GenericTypographic. You may also try setting e.Graphics.TextRenderingHint to one of the AntiAlias options.
 
Use StringFormat.GenericTypographic. You may also try setting e.Graphics.TextRenderingHint to one of the AntiAlias options.
Nice enough. Doesn't keep the text identical, since it tends to need still more space (blank space hides the second part of the text, probably because it overflows), but since it's a matter of pixels I can get around that enlarging the boxes as much as needed. (consider many controls and such have their proper position and size to avoid the form being too large, it doesn't matter for me but I'm trying to keep a form size to fit even medium display resolution)
 

Latest posts

Back
Top Bottom