如何在进程列表中找到一个程序并置顶 c#,winform中,如何在进程列表中,通过程序名找到一个正在运行的程序,并将他放置最前端。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/t/20061109/13/5145176.html [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool SetForegroundWindow(IntPtr hWnd); private void button1_Click(object sender, EventArgs e) { System.Diagnostics.Process[] _Process = System.Diagnostics.Process.GetProcessesByName("notepad"); //找到记事本的进程 if (_Process.Length != 0) { IntPtr _FormIntPtr = _Process[0].MainWindowHandle; //获取第1个记事本的窗体 SetForegroundWindow(_FormIntPtr); //激活窗体 } } static class Program { [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess(); System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcessesByName(current.ProcessName); //找到名称相同的进程 foreach (System.Diagnostics.Process p in processList) { if (p.Id != current.Id)//获取第1个同名但非本进程的 { IntPtr _FormIntPtr = p.MainWindowHandle; SetForegroundWindow(_FormIntPtr); //激活窗体 return; } } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }谢谢各位上面的方法能够实现,但有一个问题:如果被激活窗体,原来是最小化的,不能实现将其置于最前面。如何解决,谢谢。 [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); private void button1_Click(object sender, EventArgs e) { System.Diagnostics.Process[] _Process = System.Diagnostics.Process.GetProcessesByName("notepad"); //找到记事本的进程 if (_Process.Length != 0) { IntPtr _FormIntPtr = _Process[0].MainWindowHandle; //获取第1个记事本的窗体 ShowWindowAsync(_FormIntPtr, /*WS_SHOWNORMAL*/ 1); SetForegroundWindow(_FormIntPtr); } } 怎么都不仔细看的,等着要源代码啊4 楼wdy9927()回复于 2006-11-09 15:22:40 得分 20//是说这2个么? [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); //以上的方法声明为私有,对其进一步包装, /// <summary> /// 功能:HandleRunningInstance静态方法为获取应用程序句柄,设置应用程序为前台运行,并返回bool值。 /// 实现:确保窗口没有被最小化或最大化。 /// 设置为前台显示。 /// </summary> /// <param name="instance">准备设置成前台运行的程序</param> /// <returns></returns> private bool HandleRunningInstance(Process instance) { //确保窗口没有被最小化或最大化 ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //设置为foreground window return SetForegroundWindow(instance.MainWindowHandle); } 如何让正则表达式最小匹配?? 求解devexpress控件中的spinEdit控件用法 如何在ashx文件中定义全局变量?在线等~ C#字符串全组合问题 C#中如何定义结构体中的结构体? 在c# 2003中使用ProgressBar,怎么把他编成没有格子的样式? 请教一个 ADOX 创建数据表的问题 实现查询XML文档,创建索引.急需大家的帮助1 快速导出数据到excel并设定背景色[每个单元个不一定相同] 祝贺论坛补了我10天的登陆分100分!散10分祝贺!哈哈!开心啊! 使用IComparer<T>自定义Sort排序的问题 用C
public static extern bool SetForegroundWindow(IntPtr hWnd); private void button1_Click(object sender, EventArgs e)
{ System.Diagnostics.Process[] _Process = System.Diagnostics.Process.GetProcessesByName("notepad"); //找到记事本的进程 if (_Process.Length != 0)
{
IntPtr _FormIntPtr = _Process[0].MainWindowHandle; //获取第1个记事本的窗体
SetForegroundWindow(_FormIntPtr); //激活窗体
} }
{
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcessesByName(current.ProcessName); //找到名称相同的进程
foreach (System.Diagnostics.Process p in processList)
{
if (p.Id != current.Id)//获取第1个同名但非本进程的
{
IntPtr _FormIntPtr = p.MainWindowHandle;
SetForegroundWindow(_FormIntPtr); //激活窗体
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
谢谢各位
上面的方法能够实现,但有一个问题:
如果被激活窗体,原来是最小化的,不能实现将其置于最前面。如何解决,谢谢。
public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
private void button1_Click(object sender, EventArgs e)
{ System.Diagnostics.Process[] _Process = System.Diagnostics.Process.GetProcessesByName("notepad"); //找到记事本的进程 if (_Process.Length != 0)
{
IntPtr _FormIntPtr = _Process[0].MainWindowHandle; //获取第1个记事本的窗体
ShowWindowAsync(_FormIntPtr, /*WS_SHOWNORMAL*/ 1);
SetForegroundWindow(_FormIntPtr); } }
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
//以上的方法声明为私有,对其进一步包装,
/// <summary>
/// 功能:HandleRunningInstance静态方法为获取应用程序句柄,设置应用程序为前台运行,并返回bool值。
/// 实现:确保窗口没有被最小化或最大化。
/// 设置为前台显示。
/// </summary>
/// <param name="instance">准备设置成前台运行的程序</param>
/// <returns></returns>
private bool HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置为foreground window
return SetForegroundWindow(instance.MainWindowHandle);
}