C#讓窗體永遠在窗體最前面顯示的實例
這篇文章主要介紹了C#實現讓窗體永遠在窗體最前面顯示,功能非常實用,需要的朋友可以參考下:
本文以實例描述了C#實現讓窗體永遠在窗體最前面顯示的方法,具體步驟如下:
1、新建一個窗體程序,添加一個Timer以及設置它可用並綁定事件。
2、設置窗體的TopMost屬性為True
3、然后設置代碼如下即可實現.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace jiyi { public partial class Form1 : Form {public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { this.TopMost = false; this.BringToFront(); this.TopMost = true; } } }
出處:https://www.jb51.net/article/52401.htm
==========================================================================================
| 防止程序運行多個實例的方法有多種,如:通過使用互斥量和進程名等.而我想要實現的是:在程序運行多個實例時激活的是第一個實例,使其獲得焦點,並在前端顯示. 主要用到兩個API 函數:
代碼如下: |
using System.Runtime.InteropServices; using System.Diagnostics; using System.Reflection; //***************************************************** static class Program { /// <summary> /// 該函數設置由不同線程產生的窗口的顯示狀態。 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="cmdShow">指定窗口如何顯示。查看允許值列表,請查閱ShowWlndow函數的說明部分。</param> /// <returns>如果函數原來可見,返回值為非零;如果函數原來被隱藏,返回值為零。</returns> [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// <summary> /// 該函數將創建指定窗口的線程設置到前台,並且激活該窗口。鍵盤輸入轉向該窗口,並為用戶改各種可視的記號。系統給創建前台窗口的線程分配的權限稍高於其他線程。 /// </summary> /// <param name="hWnd">將被激活並被調入前台的窗口句柄。</param> /// <returns>如果窗口設入了前台,返回值為非零;如果窗口未被設入前台,返回值為零。</returns> [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1; /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Process instance = RunningInstance(); if (instance == null) { Form1 frm = new Form1(); Application.Run(new Form1()); } else { HandleRunningInstance(instance); } } /// <summary> /// 獲取正在運行的實例,沒有運行的實例返回null; /// </summary> public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { return process; } } } return null; } /// <summary> /// 顯示已運行的程序。 /// </summary> public static void HandleRunningInstance(Process instance) { ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //顯示,可以注釋掉 SetForegroundWindow(instance.MainWindowHandle); //放到前端 } }
實現讓程序只能打開一個實例(其他方法)
//=====創建互斥體法:===== bool blnIsRunning; Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out blnIsRunning); if (!blnIsRunning) { MessageBox.Show("程序已經運行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } //保證同時只有一個客戶端在運行 System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex(false, "OnePorcess.exe"); if (!mutexMyapplication.WaitOne(100, false)) { MessageBox.Show("程序" + Application.ProductName + "已經運行!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //=====判斷進程法:(修改程序名字后依然能執行)===== Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (process.MainModule.FileName == current.MainModule.FileName) { MessageBox.Show("程序已經運行!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } }
出處:http://www.soaspx.com/dotnet/csharp/csharp_20130319_10168.html
