Frage

Every time I re-open a form inside an MdiParent Container, the child form moves a small amount (~20 pixels) down and towards the right.

Is there a way to stop the form from doing this on form load, without stopping it from being moved freely within the parent?

Here is an example:

enter image description here

and then reload the child form:

enter image description here

War es hilfreich?

Lösung

Change the StartPosition property on the child form:

StartPosition = FormStartPosition.Manual

The default value is WindowsDefaultLocation, which causes each instance of the child form to drift down and to the right each time.


The following block of code from the Form class is called when the form is an MdiChild form, and the form is initially being displayed. (I trimmed out some unrelated parts.)

For all other StartPosition values beside Manual, there's some calculation going on regarding the location of the form, but it does nothing for StartPosition.Manual.

 // Adjusts the CreateParams to reflect the window bounds and start position.
 private void FillInCreateParamsStartPosition(CreateParams cp) { 
    switch ((FormStartPosition)formState[FormStateStartPos]) {
        case FormStartPosition.WindowsDefaultBounds: 
            cp.Width = NativeMethods.CW_USEDEFAULT;
            cp.Height = NativeMethods.CW_USEDEFAULT; 
            ...
        case FormStartPosition.WindowsDefaultLocation: 
        case FormStartPosition.CenterParent:
            ...
            cp.X = NativeMethods.CW_USEDEFAULT;
            cp.Y = NativeMethods.CW_USEDEFAULT;
            break;
        case FormStartPosition.CenterScreen: 
            if (IsMdiChild) {
                ...
                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);
            }
            else {
                ...
                if (WindowState != FormWindowState.Maximized) { 
                    cp.X = Math.Max(screenRect.X,screenRect.X + (screenRect.Width - cp.Width)/2); 
                    cp.Y = Math.Max(screenRect.Y,screenRect.Y + (screenRect.Height - cp.Height)/2);
                } 
            }
            break;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top