你可以在.exe运行时,判断是否已有该程序已运行,如果运行的话,把改程序显示出来就好,没写过相应代码,网上找了一段,你可以尝试一下。
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection; 其他代码省略。仅仅从一个form启动讲。每个form都有个main入口。
static void Main()
{ Process instance = RunningInstance(); //获取运行中的实例
if (instance==null)
Application.Run(new Form1());
else
{
HandleRunningInstance(instance);//处理得到的进程。
}
}
private static Process RunningInstance()
{Process current = Process.GetCurrentProcess(); //得到当前form1的进程
Process[] processes = Process.GetProcessesByName(current.ProcessName);// 得到所有同名进程。
foreach(Process MyProcess in processes)
{
if(MyProcess.Id!= current.Id) //不同的进程id,也就是肯定不是同一进程
{
if(Assembly.GetExecutingAssembly().Location.Replace( "/ ", "\\ ")==current.MainModule.FileName) //进程名不同而id不同 的时候,比较程序启动的路径
return MyProcess; }
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show( "程序已经运行! ");
ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport( "User32.dll ")]
private static externbool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport( "User32.dll ")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection; 其他代码省略。仅仅从一个form启动讲。每个form都有个main入口。
static void Main()
{ Process instance = RunningInstance(); //获取运行中的实例
if (instance==null)
Application.Run(new Form1());
else
{
HandleRunningInstance(instance);//处理得到的进程。
}
}
private static Process RunningInstance()
{Process current = Process.GetCurrentProcess(); //得到当前form1的进程
Process[] processes = Process.GetProcessesByName(current.ProcessName);// 得到所有同名进程。
foreach(Process MyProcess in processes)
{
if(MyProcess.Id!= current.Id) //不同的进程id,也就是肯定不是同一进程
{
if(Assembly.GetExecutingAssembly().Location.Replace( "/ ", "\\ ")==current.MainModule.FileName) //进程名不同而id不同 的时候,比较程序启动的路径
return MyProcess; }
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show( "程序已经运行! ");
ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport( "User32.dll ")]
private static externbool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport( "User32.dll ")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
解决方案 »
- datagridview能加日历控件吗?Winform
- Winform 中tabControl 这个功能如何写?
- 如何从数据表中批量取出数据
- XHEO|Licensing 有人用过吗?它的多语言支持始终也搞不好,大家帮忙看下谢谢,解决立刻结贴
- 在C#中,切换到设计器时出错
- 在WinForm开发下,TreeView控件中Click事件的问题:
- 在c#如何使用excel
- 今天又看到为.NET反编译头痛的贴子?如果你为.NET的反编译头痛,请关注这里
- 写入htm文件时出现乱码的问题,急盼指教!
- SqlDataReader问题,是不是一个conn只能由一个SqlDataReader用呀
- 求助内存表操作!
- 线程调用相关问题
给你个winform的禁止程序多开,替换整个Program文件的代码以及 Application.Run方法的参数
这个方法也是百度搜到的。 static class Program
{
/// <summary>
/// /// The main entry point for the application.
/// /// </summary>
[STAThread]
static void Main()
{ bool createdNew;
//系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
//第二个参数可以设置为产品的名称:Application.ProductName
//每次启动应用程序,都会验证名称为SingletonWinAppMutex的互斥是否存在
Mutex mutex = new Mutex(false, "SingletonWinAppMutex", out createdNew);
//如果已运行,则在前端显示 //createdNew == false,说明程序已运行
if (!createdNew)
{
Process instance = GetExistProcess();
if (instance != null)
{
//让程序前段显示并且关闭新启动的程序
SetForegroud(instance);
Application.Exit();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//把Application.Run中的参数改为你的主窗体名
Application.Run(new UpdateVersion());
} /// <summary>
/// /// 查看程序是否已经运行
/// /// </summary>
/// /// <returns></returns>
private static Process GetExistProcess()
{
Process currentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if ((process.Id != currentProcess.Id) &&
(Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
{
return process;
}
}
return null;
}
/// <summary>
/// /// 使程序前端显示
/// /// </summary>
/// /// <param name="instance"></param>
private static void SetForegroud(Process instance)
{
IntPtr mainFormHandle = instance.MainWindowHandle;
if (mainFormHandle != IntPtr.Zero)
{
ShowWindowAsync(mainFormHandle, 1);
SetForegroundWindow(mainFormHandle);
}
} [DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
}
using System.Collections.Generic;
using System.Windows.Forms;namespace Directory_File
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Boolean createdNew; //返回是否赋予了使用线程的互斥体初始所属权
System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName",out createdNew); //同步基元变量
if (createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
instance.ReleaseMutex();
}
else
{
MessageBox.Show("已经启动了一个程序!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
}
}
}
Main()
{
if(CheckAppIsRun())
{
SendMessageToActive(); //发送Window消息,重写主窗体的WndProc函数,处理自定义的Window消息
}
else
{
RunApp(); //启动新程序
}
}
这个方法我试过了,我用的就是和你们一样的代码。
可以判断出进程是否已开启,但是使用user32.dll
这样只能调出最小化后的窗体,窗体hide隐藏后,调用不出来。
hide后如何调出来呢?
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void Form1_Load(object sender, EventArgs e)
{
System.Diagnostics.Process oProcess = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] aoProcesses = System.Diagnostics.Process.GetProcessesByName(oProcess.ProcessName);
if (aoProcesses.Length > 1)
{
MessageBox.Show(
"程序 \""
+ oProcess.ProcessName
+ "\" 已经运行.", "Ultrasimple");
SetForegroundWindow(aoProcesses[aoProcesses[0].Id ==
oProcess.Id ? 1 : 0].MainWindowHandle);
return;
}
c#一样很简单,有封装好的“托盘图标控制对象”winform控件,而且可以捕获各种事件,可以自定义各种事件(例如通知程序的主窗口执行show()方法)。问题是,你有没有注意到他们想“即使在任务栏托盘图标隐藏时也要点击出来”这种描述?你可以看到,在你的回复的上面的那几位,都是考虑如何“偷别人”的进程,从外部去控制进程去“模拟用户点击”。而不是正规地让一个进程“内建”响应用户的点击托盘图标的操作。所以用一个做正规软件的思路去理解某些的说明,如果不小心,会看错意思的。
软件搞一个启动界面,然后通过这个界面进入主界面,这是关键的一步,我暂时设主界面为MainForm
那么在Programe.CS中加入 Public static MainForm mainfrm;
在启动界面中这样进行主界面
Programe.mainfrm=new MainForm();
Programe.mainfrm.Show();那么在Programe.cs的Main方法中这样判断
//if软件已经运行
//mainfrm.Show();这个是我的一个解决方法没有用代码去验证,你可以试下!