I have a WinForms application. It has a MainWindowForm
(creatively) named Form1
, and within that, a maximised child Form
named fymmapdraw
. The child form is created with the following VB.NET snippet:
fformmapdraw = New fymmapdraw With {
.MdiParent = Form1,
.WindowState = FormWindowState.Maximized
}
fformmapdraw.Show()
My issue is that with Form1
maximised and fymmapdraw
maximised within it, the fymmapdraw.Width
reports a value greater than the screen width in some cases. This is an issue with the drawing code within fymmapdraw
, where some snapping functionality incorrectly snaps beyond the window border.
It appears this is linked to Aero-style desktops - the width was correct on Windows XP, and correct on Windows 7 if an Aero-style desktop theme was not used, but incorrect on Windows 7 or 10 with such a theme enabled.
My screen resolution is 1920x1080. The mouse X coordinates reported by fymmapdraw
's MouseMove
event e.X
property, which reports relative to the form itself, range from 0 to 1915. These values are reasonable - a couple of pixels are lost at either side to the window frame.
However, the form's Me.Width
property returns 1932 pixels, in excess of the screen width. Me.Left
returns -8 and Me.Right
returns 1924.
The application is fairly old, originally dating back to the Windows XP era (2006-7). It is currently using .Net Framework 4.0. fymmapdraw
has its FormBorderStyle set to Sizeable, and its AutoScaleMode to Font (although it contains no text controls or similar - all drawing is handled by custom code).
Is there a way to have Form.Width return the "correct" value - i.e. the true number of pixels visible - perhaps by changing some property of the Form or Application? If not, there another property of the Form where I can get the true width?
The image below illustrates the output. At top left, the "Raw" coordinates are those reported by the MouseMove
event, with the mouse just inside the right hand boundary. The "Form Dimensions" reported are fymmapdraw.Width
and fymmapdraw.Height
. The small control bar is the one which is snapping incorrectly - it is placed with it's right hand edge equal to fymmapdraw.Width
, but is partially off screen (a full blue border should be visible).