Thursday, May 14, 2015

Fixing the High DPI Alignment Issue

Yesterday, I gave up on aligning Windows Forms controls at high DPI. Today I found a (fairly simple) solution. My problem was that I had been too insistent on a design-time solution rather than the addition of some code to fix it up at runtime.

All I wanted was to make the right edges of the Label and LinkLabel line up. That is, I wanted to assign Rlabel to Rlink. However, the Right property of controls is read-only; I can only modify Left and Top. A little bit of algebra got me the simple formula:

Llink = Llabel + Wlabel - Wlink

Or simply:

Llink = Rlabel - Wlink

And in code:

LinkLabel.Left = Label.Right - LinkLabel.Width

The link is now aligned properly at all DPI levels accessible in Windows 7, with only one line of code added to the form's Load handler.

No comments:

Post a Comment