How to make a WPF window be on top of all other windows of my app (not system wide)?

后端 未结 19 2975
南笙
南笙 2020-12-14 05:42

I want my window to be on top of all other windows in my application only. If I set the TopMost property of a window, it becomes on top of all windows of al

19条回答
  •  失恋的感觉
    2020-12-14 05:54

    Just learning C# and ran across similar situation. but found a solution that I think may help. You may have figured this a long time ago. this will be from starting a new project but you can use it in any.

    1) Start new project.

    2) go to Project, then New Windows form, then select Windows Form and name Splash.

    3) set size, background, text, etc as desired.

    4) Under Properties of the Splash.cs form set Start Position: CenterScreen and TopMost: true

    5) form1 add "using System.Threading;"

    6) form1 under class add "Splash splashscreen = new Splash();"

    7) form1 add "splashscreen.Show();" and "Application.DoEvents();"

    8) form1 Under Events>>Focus>>Activated add "Thread.Sleep(4000); splashscreen.Close();"

    9) Splash.cs add under "Public Splash" add "this.BackColor = Color.Aqua;" /can use any color

    10) This is the code for Form1.cs

    public partial class Form1 : Form
    {
        Splash splashscreen = new Splash();
        public Form1()
        {
            InitializeComponent();
            splashscreen.Show();
            Application.DoEvents();
    
        }
    
        private void Form1_Activated(object sender, EventArgs e)
        {
            Thread.Sleep(4000);
            splashscreen.Close();
        }
    }
    

    11) this is the code on Splash.cs

    public partial class Splash : Form
    {
        public Splash()
        {
            InitializeComponent();
            this.BackColor = Color.Aqua;
        }
    }
    

    12) I found that if you do NOT do something in the splash then the screen will not stay on the top for the time the first form needs to activate. The Thread count will disappear the splash after x seconds, so your program is normal.

提交回复
热议问题