0

I customized the checkbox to be rendered as a toggle button which doesn't render properly if I use two monitors with different resolutions, otherwise it renders properly. How can I lock it to be displayed as same in any monitor

enter image description here

enter image description here

This is how the custom checkbox looks like,

      protected override void OnPaint(PaintEventArgs e)
    {
        this.OnPaintBackground(e);
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
        using (var path = new GraphicsPath())
        {
            var d = Padding.All;
            var r = this.Height - 2 * d;
            path.AddArc(d, d, r, r, 90, 180);
            path.AddArc(this.Width - r - d, d, r, r, -90, 180);
            path.CloseFigure();
            e.Graphics.FillPath(Checked ? Brushes.DarkGray : Brushes.LightGray, path);
            r = Height - 1;
            var rect = Checked ? new Rectangle(Width - r - 1, 0, r, r)
                               : new Rectangle(0, 0, r, r);
            SolidBrush brush = new SolidBrush(Color.FromArgb(0, 122, 204));

            e.Graphics.FillEllipse(Checked ? brush : (TeamsForm.CurrentBackGroundTheme.Name == DarkBackGround) ? Brushes.WhiteSmoke : Brushes.LightGray, rect);
        }
Diksha Mundhra
  • 121
  • 2
  • 12
  • Maybe [this](https://stackoverflow.com/a/53026765/10216583) help.. –  May 06 '20 at 00:48
  • 1
    @JQSOFT Only the DpiAwareness part described there would somewhat help. Here the problem is that using a fixed measure, as the Padding value, doesn't scale well. Or, doesn't scale at all. All measures need to be relative, here. The OP needs to define a section of the Custom/User Control where the Graphics is rendered and recalculate/verify the section ratio (the relation between the parts - current width and height, mainly, but also the relative position of the Graphics inside the section) each time the control is invalidated. – Jimi May 06 '20 at 04:20

1 Answers1

1

You'll need to handle Form.DpiChanged event and rescale your control.

However in .NET Framework PMA is rudimentary, to-date it hasn't seen much improvement in .NET Core either.

RussKie
  • 1,630
  • 11
  • 20
  • How to rescale the control? Which properties do I need to change? – Diksha Mundhra May 07 '20 at 18:12
  • Your code above contains a number of constants that need to be multiplied by the scale factor. Depending on how involved what you're trying to accomplish you may need to do something like this: https://github.com/dotnet/winforms/blob/62c17fe35b2f00f8156828148a5fbbb910cc52a1/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs – RussKie May 11 '20 at 10:27
  • Without PMA, you could probably do something like https://github.com/gitextensions/gitextensions/blob/b210331fb67b2362cc1c666ad49911d4666d85e0/GitExtUtils/GitUI/DpiUtil.cs – RussKie May 11 '20 at 10:28