C# Winform 防止程序重复启动时的窗口置前

GooKit 2012-04-01 06:46:59
想要屏蔽应用程序重复启动,但是在第N(>1)次启动时想直接把已经打开的程序窗口调到桌面上来,
在网站找到这马一段代码,


[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
...
{
ShowWindowAsync(temp.MainWindowHandle, SW_SHOWNOACTIVATE);
SetForegroundWindow(temp.MainWindowHandle);
}


无效,请各位大侠赐教,谢谢先!
...全文
547 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
orochiheart 2012-04-01
  • 打赏
  • 举报
回复
我也想要这个功能 学习!
bcc222 2012-04-01
  • 打赏
  • 举报
回复
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 防止多次执行
Process[] processes = Process.GetProcessesByName("ReadServer");
if (processes.Length >= 2)
{
MessageBox.Show("程序已经执行!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
Application.Run(new MainForm());
}
}
防止程序重复运行的
bcc222 2012-04-01
  • 打赏
  • 举报
回复
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
具体的还没有测试出来,但是我想你应该先找到原来程序的一个句柄!

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧