Where is WindowsDefaultLocation (CW_USEDEFAULT)?

2

I'm trying to find out how OS defines so called WindowsDefaultLocation in Winforms. The forms have property called StartPosition that takes enumeration as value. One the values is WindowsDefaultLocation (= 2). In framework code I found:

public enum FormStartPosition

The comment to enum:

WindowsDefaultLocation = 2;

is

The form is positioned at the Windows default location and has the bounds determined by Windows default.

In method that sets form's position (for WindowsDefaultLocation) I found:

cp.X = NativeMethods.CW_USEDEFAULT;
cp.Y = NativeMethods.CW_USEDEFAULT;

where

CW_USEDEFAULT = (unchecked((int)0x80000000))

Other enums (eg: CenterScreen) have inside some mathematical calculations - it's understandable.

cp.X = Math.Max(clientRect.X, clientRect.X + (clientRect.Width - cp.Width)/2);
cp.Y = Math.Max(clientRect.Y, clientRect.Y + (clientRect.Height - cp.Height)/2);

So my question again: where is Windows default location for newly created form and how does OS define it each time when application starts or/and new form is initialized and shown?

c#
winforms
asked on Stack Overflow Aug 10, 2019 by yarecky • edited Aug 11, 2019 by yarecky

1 Answer

2

The position is determined by the OS, and the implementation of that algorithm might differ from one OS version to the other.

Raymond Chen explains the rules for "an unspecified version of Windows" at https://devblogs.microsoft.com/oldnewthing/20121126-00/?p=5993

Basically if there is no other reason for a specific monitor (such as a parent window), the window will be placed on the primary monitor, and the location on that monitor will be at an offset of the previous default location.

answered on Stack Overflow Aug 12, 2019 by C.Evenhuis

User contributions licensed under CC BY-SA 3.0