WPF:窗体置顶

1、设置窗体TopMost属性

private DispatcherTimer timer;

public Window1()
{
    InitializeComponent();
    Loaded += new RoutedEventHandler(Window1_Loaded);
}
 
void Window1_Loaded(object sender, RoutedEventArgs e)
{
    timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(1);
    timer.Tick += timer1_Tick;
    timer.Start();  

  //....

  //timer.Stop();
}
 
private void timer1_Tick(object sender, EventArgs e)
{
    //定时处理

  this.TopMost = true;
}
View Code

2、设置窗体Owner

WindowInteropHelper mianHanel = new WindowInteropHelper(MainWindow.Current);
WindowInteropHelper vedioWin = new WindowInteropHelper(this);
WindowInteropHelper FrameWin = new WindowInteropHelper(FrameWindow);
FrameWin.Owner = IntPtr.Zero;
mianHanel.Owner = vedioWin.Handle;
vedioWin.Owner = FrameWin.Handle; 
View Code

3、通过函数设置

  1)SetWindowPos:该函数将指定的窗口设置到Z序的特定位置。如:SetWindowPos(_process.MainWindowHandle, 0, 0, 0, 0, 0, 1 | 2);//4

    https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms633545(v=vs.85).aspx

  2)BringWindowToTop:该函数将指定的窗口设置到Z序的顶部。如果窗口为顶层窗口,则该窗口被激活;如果窗口为子窗口,则相应的顶级父窗口被激活。调用这个函数类似于调用SetWindowPos函数来改变窗口在Z序中的位置,但是BringWindowToTop函数并不能使一个窗口成为顶层窗口。

  3)SetForegroundWindow:如果应用程序不在前台中而想设置在前台中,可以调用该函数。

注:如果在外软件中以进程的方式启动该软件,还是未置顶则考虑在该软件自身运行时设置SetForegroundWindow。

  实例:

Process process = RuningInstance();
            if (process == null)
            {
                //   process.Kill();
                _logger.Info("打开黑板");
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmBlackbord());
            }
           
            else
            {
                _logger.Info("激活黑板");
                SetForegroundWindow(process.MainWindowHandle);
                try
                {
                    int reuslt = 0;
                    if (IsZhangj != null)
                    {
                        switch (args[1])
                        {
                            case "-n":
                                reuslt = HandleRunningInstance(process, "新建文件&" + BlackDataService.NewFilepath);
                        }

                    }
                    else
                    {
                        // MessageBox.Show("应用程序已经在运行中。。。");
                        reuslt = HandleRunningInstance(process, "激活窗口");
                    }
                    if (reuslt == 0)
                    {
                        //process.Kill();
                        //_logger.Info("重新打开黑板");

                    }
                }
                catch (Exception ex)
                {
                    _logger.Debug(ex.Message + ":" + ex.StackTrace);
                }
                //System.Threading.Thread.Sleep(1000);
                //System.Environment.Exit(1);
            }

        }



private static Process RuningInstance()
        {
            Process currentProcess = Process.GetCurrentProcess();
            Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);
            _logger.Info(currentProcess.ProcessName);

            foreach (Process process in Processes)
            {
                if (process.Id != currentProcess.Id)
                {
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName)
                    {

                        return process;
                    }
                }
            }
            return null;
        }
View Code

 

你可能感兴趣的:(操作系统)