怎么实现 c#中让Windows窗体只运行一次,并在第二次启动窗体时激活该窗体
就像迅雷一样迅雷在启动的时候点击关闭按钮就最小化到电脑的右下角了,然后在启动迅雷,却没有从新启动,而是把已经启动的迅雷激活了...请问用C#怎么实现。...
就像迅雷一样 迅雷在启动的时候点击关闭按钮就最小化到电脑的右下角了,然后在启动迅雷,却没有从新启动,而是把已经启动的迅雷激活了...请问用C#怎么实现。
展开
2个回答
展开全部
把这段代码覆盖你原来的Program类
功能:程序在已经运行的情况下,如果程序的另一个实例企图运行,结束它,并弹出已运行的程序实例。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Process instance = AIIS();
if (instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
//处理发现的例程
BIIS(instance);
//MessageBox.Show("当前程序已经运行了!");
}
}
public static Process AIIS()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void BIIS(Process instance)
{
//确保窗口没有被最小化或最大化
//1 center,2 minimize 3maximize
ShowWindowAsync(instance.MainWindowHandle, 1);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
功能:程序在已经运行的情况下,如果程序的另一个实例企图运行,结束它,并弹出已运行的程序实例。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Process instance = AIIS();
if (instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
//处理发现的例程
BIIS(instance);
//MessageBox.Show("当前程序已经运行了!");
}
}
public static Process AIIS()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void BIIS(Process instance)
{
//确保窗口没有被最小化或最大化
//1 center,2 minimize 3maximize
ShowWindowAsync(instance.MainWindowHandle, 1);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
展开全部
把这段代码覆盖你原来的Program类
功能:程序在已经运行的情况下,如果程序的另一个实例企图运行,结束它,并弹出已运行的程序实例。
static
class
Program
{
///
<summary>
///
The
main
entry
point
for
the
application.
///
</summary>
[STAThread]
static
void
Main()
{
Process
instance
=
AIIS();
if
(instance
==
null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new
Form1());
}
else
{
//处理发现的例程
BIIS(instance);
//MessageBox.Show("当前程序已经运行了!");
}
}
public
static
Process
AIIS()
{
Process
current
=
Process.GetCurrentProcess();
Process[]
processes
=
Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
foreach
(Process
process
in
processes)
{
//忽略现有的例程
if
(process.Id
!=
current.Id)
{
//确保例程从EXE文件运行
if
(System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/",
"\\")
==
current.MainModule.FileName)
{
//返回另一个例程实例
return
process;
}
}
}
//没有其它的例程,返回Null
return
null;
}
public
static
void
BIIS(Process
instance)
{
//确保窗口没有被最小化或最大化
//1
center,2
minimize
3maximize
ShowWindowAsync(instance.MainWindowHandle,
1);
//设置真实例程为foreground
window
SetForegroundWindow(instance.MainWindowHandle);
}
[System.Runtime.InteropServices.DllImport("User32.dll")]
private
static
extern
bool
ShowWindowAsync(IntPtr
hWnd,
int
cmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private
static
extern
bool
SetForegroundWindow(IntPtr
hWnd);
}
功能:程序在已经运行的情况下,如果程序的另一个实例企图运行,结束它,并弹出已运行的程序实例。
static
class
Program
{
///
<summary>
///
The
main
entry
point
for
the
application.
///
</summary>
[STAThread]
static
void
Main()
{
Process
instance
=
AIIS();
if
(instance
==
null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new
Form1());
}
else
{
//处理发现的例程
BIIS(instance);
//MessageBox.Show("当前程序已经运行了!");
}
}
public
static
Process
AIIS()
{
Process
current
=
Process.GetCurrentProcess();
Process[]
processes
=
Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
foreach
(Process
process
in
processes)
{
//忽略现有的例程
if
(process.Id
!=
current.Id)
{
//确保例程从EXE文件运行
if
(System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/",
"\\")
==
current.MainModule.FileName)
{
//返回另一个例程实例
return
process;
}
}
}
//没有其它的例程,返回Null
return
null;
}
public
static
void
BIIS(Process
instance)
{
//确保窗口没有被最小化或最大化
//1
center,2
minimize
3maximize
ShowWindowAsync(instance.MainWindowHandle,
1);
//设置真实例程为foreground
window
SetForegroundWindow(instance.MainWindowHandle);
}
[System.Runtime.InteropServices.DllImport("User32.dll")]
private
static
extern
bool
ShowWindowAsync(IntPtr
hWnd,
int
cmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private
static
extern
bool
SetForegroundWindow(IntPtr
hWnd);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询