0

I am trying to make all check boxes in my C# Winform application to ignore user's DPI setting when it's bigger than 100%. i.e I want the size of the checkbox to remain small as if it's on 100% font scale, even when the user has increased the DPI setting.

Using FlatStyle.Flat checkboxes, the size of the checkboxes in various DPI settings:

  • 100% (96 DPI) : 11 pixel x 11 pixel (including border)
  • 125% (120 DPI) : 13 x 13
  • 150% (144 DPI) : 16 x 16

I have done the following but the size of my checkboxes still increases:

  • AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  • set all Font to use GraphicsUnit.Pixel

So, is there a way for my checkbox to remain 11pixel by 11pixel regardless of DPI setting?

If it's not possible, I have also tried overriding OnPaint on my custom checkbox to paint over the checkbox without painting over the Text. But it proves to be complicated since the starting (X,Y) coordinate to render the box is somewhat affected by CheckAlign and RightToLeft properties.

This is the code snippet. I intended for red box to paint over the original checkbox. And green box as the new checkbox I want to display instead.

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // We need to override checkbox drawing for large font scaling since it will be drawn bigger than usual.
            // ex: At 100% scaling, checkbox size is 11x11. At 125% scaling, checkbox size is 13x13.
            if (e.Graphics.DpiX > 96)
            {
                // What is the x & y co-ords of the checkbox?
                int x = 0;
                int y = 0;

                using (SolidBrush brush = new SolidBrush(Enabled ? Color.White : SystemColors.Control))
                {
                    // Red checkbox is to override the original checkbox drawn
                    var scaleFactor = e.Graphics.DpiX / 96;
                    e.Graphics.DrawRectangle(new Pen(Color.Red), x, y, (float)Math.Round(CHECKBOX_WIDTH * scaleFactor) + 1, (float)Math.Round(CHECKBOX_HEIGHT * scaleFactor) + 1);

                    // Green checkbox is to draw the small checkbox that I want 11x11 (including border)
                    e.Graphics.FillRectangle(brush, x, y, CHECKBOX_WIDTH, CHECKBOX_HEIGHT);
                    e.Graphics.DrawRectangle(new Pen(Color.Green), x, y, CHECKBOX_WIDTH + 1, CHECKBOX_HEIGHT + 1); // Draw the outer border
                }
            }
        }

Can anyone help me how to determine the correct X,Y coord for me to draw the red checkbox, please (so I can paint over the original box).

Thank you :)

Maggie
  • 1
  • [CheckBoxRenderer Class](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.checkboxrenderer). Make your application DPIAware: [How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?](https://stackoverflow.com/a/13228495/7444103). – Jimi Jun 11 '19 at 03:58
  • Thanks @Jimi. I tried editing app.manifest and App.config as per the article you recommended, the size of my checkbox is still large in high DPI settings. Also, my issue is not knowing the (X,Y) coordinates to draw the checkbox in the original location. So even using CheckBoxRenderer doesn't help me, plus I read that CheckBoxRenderer cannot draw flat style checkboxes? – Maggie Jun 11 '19 at 04:48
  • Not both! Choose one or the other. As of now, setting these properties in `app.config` is preferred (MS prefers that you prefer it). Start making you app `SystemAware` and test different configurations and `AutoscaleMode`. Another *preference* is to target .Net Framework `4.7.1+`. `CheckBoxRenderer` can help you render *parts* of a CheckBox. – Jimi Jun 11 '19 at 04:53
  • Anyway, a CheckBox, as all controls, has it's Bounds, its ClientArea etc. It should be irrelevant what its Size is. The Size of a Control can always change. You have to read these values at the time you need to use them. Surely not hard-code anything. – Jimi Jun 11 '19 at 05:04
  • See also: [VisualStyleElement.Button.CheckBox Class](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.visualstyles.visualstyleelement.button.checkbox). – Jimi Jun 11 '19 at 18:05

0 Answers0