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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM