刚才找到了只打开一个winform窗口的代码,
不知道如何实现点击这个最小化窗口的.exe执行文件时激活这个窗口。 if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
//这里怎么写才能能激活这个窗口呢???????????
}
else
{
Application.Run(new Form1());
}
不知如何写,请教前辈们,谢谢!
不知道如何实现点击这个最小化窗口的.exe执行文件时激活这个窗口。 if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
//这里怎么写才能能激活这个窗口呢???????????
}
else
{
Application.Run(new Form1());
}
不知如何写,请教前辈们,谢谢!
解决方案 »
- 菜鸟请教未将对象引用到实例问题
- C# 如何删除txt文档的第一行
- 數據庫操作問題!急!急!級!急!
- c# 调用非托管DLL代码问题?
- 在vs2005中datagridview想在里面添加一列
- 读取内部局域网中所有用户,计算机,以及组?有哪位做过?
- C#的一个大bug?
- C#在登录界面限制用户登陆次数,下面的程序哪里错了??
- monodevelop 新建gtk+界面窗体时自带代码出现找不到引用问题!求解决!
- 请教个开发问题
- 急急急!请问各位大哥大姐们 谁有有 《C#开发实战1200例》该书的全部代码啊!能不能发到[email protected]邮箱
- richtextbox 中怎么加入图片(如表情图片)求高手
具体的用方法忘记了
http://www.lordong.cn/blog/post/17.html
C#单实例运行实现
在某些情况我们要求应用程序只能运行一次,后运行的实例要把之前运行的程序激活并自己退出。
在网上搜索了一些实现并根据自己的理解重新整理得出下面的实现代码:1. API函数的声明
// Uses to active the exist window
[DllImport("User32.dll")]
public static extern void SetForegroundWindow(IntPtr hwnd);[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);// 0-Hidden, 1-Centered, 2-Minimized, 3-Maximized
private const int WS_SHOWNORMAL = 3;2. 查找并激活已经存在的程序(仅应用于进程名相同的程序)
///
/// Finds the running instance.
///
/// true if exist a running instace, otherwise false
private static bool ExistRunningInstance()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] procList = Process.GetProcessesByName(currentProcess.ProcessName);foreach (Process proc in procList)
{
// Found a running instance
if (proc.Id != currentProcess.Id)
{
// Active the running instance
ShowWindowAsync(proc.MainWindowHandle, WS_SHOWNORMAL);
SetForegroundWindow(proc.MainWindowHandle);return true;
}
}return false;
}3. 在Main函数入口判断
[STAThread]
static void Main()
{
// Control only one instance with the same process name can run
if (ExistRunningInstance())
{
Environment.Exit(1); // App is running, exit
}// 程序真正运行的代码
}
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
[DllImport ( "user32.dll", EntryPoint = "FindWindowEx", SetLastError = true )]
private static extern IntPtr FindWindowEx( IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow );[DllImport ( "user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true )]
private static extern void SetForegroundWindow( IntPtr hwnd );
IntPtr hwndC = FindWindow ( null, "" );
if ( hwndCalc != IntPtr.Zero )
{
SetForegroundWindow ( hwndC );
System.Threading.Thread.Sleep ( 2000 );
}[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain());
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
ShowWindowAsync(process.MainWindowHandle, SW_RESTORE);
break;
}
}
}
}
}