Search code examples
c#winformstopmostform-control

Form not in right position


I'd like to show an instance of a form class for a specific time. The form needs to be topmost and not steal focus. Here is my code:

public class mSplashForm : Form
{
    public mSplashForm()
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.BackColor = Color.LightBlue;
        this.Opacity = 0.92D;
        this.ShowInTaskbar = false;
        this.MinimumSize = new System.Drawing.Size(5, 5);
    }
}


public static void mSplash(int time = 500)
{
    mSplashForm SF = new mSplashForm();
    Application.EnableVisualStyles();
    SF.Width = 500;
    SF.Height = 100;
    SF.Left = 500;
    SF.Top = 500;
    SetWindowPos(SF.Handle, HWND_TOPMOST, SF.Left, SF.Top, SF.Width, SF.Height, SWP_NOACTIVATE);
    ShowWindow(SF.Handle, mEnumShowWindowCommands.ShowNoActivate);
    Application.DoEvents();
    Thread.Sleep(time);
    SF.Close();
}

It works, but the form is not shown in the right position defined using Top and Left parameters. What is wrong please?


Solution

  • You've got your form set to start in FormStartPosition.WindowsDefaultLocation. Add this into your mSplash function:

    SF.StartPosition = FormStartPosition.Manual;
    

    This is why it's trying to position successively down the page (as per your comment) on each opening.