激活 winform 窗口

zenjj 2009-07-14 07:30:34
刚才找到了只打开一个winform窗口的代码,
不知道如何实现点击这个最小化窗口.exe执行文件时激活这个窗口。

if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
//这里怎么写才能能激活这个窗口呢???????????
}
else
{
Application.Run(new Form1());
}


不知如何写,请教前辈们,谢谢!
...全文
604 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ck2022 2012-02-18
  • 打赏
  • 举报
回复
Activate()方法
zenjj 2009-07-14
  • 打赏
  • 举报
回复
被我先分分了!!!
wuyq11 2009-07-14
  • 打赏
  • 举报
回复
DllImport ( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
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;
}
}
}
}
}


十八道胡同 2009-07-14
  • 打赏
  • 举报
回复
4楼的方法
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
}

// 程序真正运行的代码
}
宝_爸 2009-07-14
  • 打赏
  • 举报
回复
参考:
http://www.lordong.cn/blog/post/17.html
十八道胡同 2009-07-14
  • 打赏
  • 举报
回复
Form.Show();试试
weilong_0754 2009-07-14
  • 打赏
  • 举报
回复
好像是用这个Activate()方法
具体的用方法忘记了
zenjj 2009-07-14
  • 打赏
  • 举报
回复
等待

110,502

社区成员

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

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

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