本文以實例描述了C#實現讓窗體永遠在窗體最前面顯示的方法,具體步驟以下:html
一、新建一個窗體程序,添加一個Timer以及設置它可用並綁定事件。前端
二、設置窗體的TopMost屬性爲Trueapp
三、而後設置代碼以下便可實現.函數
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 this
==========================================================================================spa
防止程序運行多個實例的方法有多種,如:經過使用互斥量和進程名等.而我想要實現的是:在程序運行多個實例時激活的是第一個實例,使其得到焦點,並在前端顯示..net 主要用到兩個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.htmlorm