首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么实现:如果一个程序已经运行,再次运行的时候,激活并显示程序的窗口

2012-01-15 
如何实现:如果一个程序已经运行,再次运行的时候,激活并显示程序的窗口staticvoidMain(){Application.Enabl

如何实现:如果一个程序已经运行,再次运行的时候,激活并显示程序的窗口
static   void   Main()
                {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);


                        Form1   myform   =   new   Form1();

                        bool   not_created;
                        Mutex   m   =   new   Mutex(true,   "last ",   out   not_created);
                        if   (not_created)
                        {

                                Application.Run(myform);
                                m.ReleaseMutex();
                        }
                        else
                        {

                                try
                                {
                                       
                                        //用了n多种方法就是不成功
                                        //myform.Show();
                                        //myform.Activate();
                                        //myform.Focus();
                                        //myform.BringToFront();
                                }
                                catch   (Exception   ex)
                                {
                                        MessageBox.Show(ex.ToString());
                                }

                        }



                }

另外,为了实现程序启动时隐藏(只在托盘显示图标),我加了
                private   void   Form1_Shown(object   sender,   EventArgs   e)
                {
                        this.Hide();
                }
跟这个有冲突吗?



[解决办法]
using System.Diagnostics;//用于使程序只运行一次
using System.Reflection;//用于使程序只运行一次
using System.Runtime.InteropServices; //用于使程序只运行一次
using System.IO;//用于使程序只运行一次

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//用于使程序只运行一次
Process instance=RunningInstance();
if( instance!= null )
{
//MessageBox.Show( "程序已在运行! " );
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle , 1);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
return;
}
}

/// <summary>
/// 确保窗口没有被最小化或最大化 (用于使程序只运行一次)
/// </summary>
/// <param name= "hWnd "> 进程中主窗口的窗口句柄 </param>
/// <param name= "cmdShow "> 1还原窗口,2 窗口最小化,3 窗口最大化 </param>
/// <returns> </returns>
[DllImport( "User32.dll ")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);

/// <summary>
/// 设置真实例程为foreground window (用于使程序只运行一次)
/// </summary>
/// <param name= "hWnd "> 进程中主窗口的窗口句柄 </param>
/// <returns> </returns>
[DllImport( "User32.dll ")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[解决办法]
/*
*How to prevent multiple application instances?
*See cSingletonApp class
*
*cSingletonApp.PreviousInstanceDetected detects previous instance of the application and returns
*its MainWindowHandle (if any)
*
*cSingletonApp.ShowPreviousInstance is more tricky. If the MainWindowHandle of the previous application
*instance is available, it activates the main window.
*What makes it tricky is that
*it works correctly even if some modal form is active or some dialog is running.
*
*Copyright ?2003 by Sergey A. Kryukov
*http://www.SAKryukov.org
*
*/
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace SingletonApp {
/// <summary>
/// Singleton application class.
/// </summary>
public class cSingletonApp {

/// <summary>
/// Verifies if previous instance is detected.
/// </summary>
/// <param name= "handle "> </param>
/// <returns> </returns>
public static bool PreviousInstanceDetected(out IntPtr handle) {
handle = IntPtr.Zero;
Process currentProcess=Process.GetCurrentProcess();
string currentName = currentProcess.MainModule.FileName;
string shortCurrentName = Path.GetFileNameWithoutExtension(currentName);
Process[] processes = Process.GetProcessesByName(shortCurrentName);
foreach (Process aproc in processes) {
if (aproc.Id!=currentProcess.Id) {


if (aproc.MainModule.FileName==currentName) {
handle = aproc.MainWindowHandle;
return true;
} // if same full name
} // if not the same process
}
return false;
} // PreviousInstanceDetected()
/// <summary>
/// Shows previous instance.
/// </summary>
/// <param name= "handle "> </param>
public static void ShowPreviousInstance(IntPtr handle) {
if (handle==IntPtr.Zero) return;
IntPtr topWindow = GetLastActivePopup(handle);
if (topWindow==IntPtr.Zero) return;
if ( (IsWindowVisible(topWindow)) && (IsWindowEnabled(topWindow)) )
SetForegroundWindow(topWindow);
} // ShowPreviousInstance()
#region private: user32.dll imports
[DllImport( "user32.dll ")]
privatestatic extern IntPtr GetLastActivePopup(IntPtr handle);
[DllImport( "user32.dll ")]
privatestatic extern bool IsWindowVisible(IntPtr handle);
[DllImport( "user32.dll ")]
privatestatic extern bool IsWindowEnabled(IntPtr handle);
[DllImport( "user32.dll ")]
privatestatic extern void SetForegroundWindow(IntPtr handle);
#endregion // private
} // class cSingletonApp
} // namespace SingletonApp

热点排行