求教,求指点....怎么把进程窗口显示并前置?

li735937915 2011-12-09 03:24:02
项目需要判断程序只能运行一个!在打开另外一个程序的时候,显示并前置已打开的程序! 目前可以使程序只能运行一个,但不知道怎么设置前置!本人新手,求高手指点一二....
...全文
205 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
li735937915 2011-12-09
  • 打赏
  • 举报
回复
知道了,谢谢了yfqvip(满衣兄), 祝你新婚快乐! 幸福永远!!!
li735937915 2011-12-09
  • 打赏
  • 举报
回复
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
有错误,系统找不到!
这些需要引用什么名称空间吗?DLL文件时系统的吗?
满衣兄 2011-12-09
  • 打赏
  • 举报
回复
代码都给你了,其实自己组合一下就行了。。

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;

public void RaiseOtherProcess()
{
Process proc = Process.GetCurrentProcess();
Process.GetProcesses();
foreach (Process otherProc in
Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
{
//ignore "this" process
if (proc.Id != otherProc.Id)
{
// Found a "same named process".
// Assume it is the one we want brought to the foreground.
// Use the Win32 API to bring it to the foreground.
IntPtr hWnd = otherProc.MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, 9);
}
SetForegroundWindow(hWnd);
break;
}
}
}


用mutex实现互斥。
当互斥发生时调用以上代码,实现:
2.如果程序已经存在,且最小化,则还原那个程序。
3.如果程序已经存在,且不是最小化(最大化或正常状态),则显示(注意:不是还原!)那个程序。
li735937915 2011-12-09
  • 打赏
  • 举报
回复
有什么办法通过进程来前置窗口?
满衣兄 2011-12-09
  • 打赏
  • 举报
回复
单实例:
http://topic.csdn.net/t/20050516/14/4010565.html

窗口前置:用SetForegroundWindow函数

[DllImport("user32.dll ")]
public static extern IntPtr FindWindow(string 类名, string 程序标题);

[DllImport( "user32.dll ")]
private static extern bool SetForegroundWindow(IntPtr hWnd);


private void button3_Click(object sender, EventArgs e)
{
IntPtr hwnd = FindWindow(null, "新建 文本文档 (6).txt - 记事本"); //放回句柄
int WM_PASTE = // 具体的消息号你可以网上查下
if (hwnd.ToInt32 () != 0)
{
SetForegroundWindow(hwnd);
SendMessage(hwnd,WM_PASTE,0,0); // 具体的消息原型你可以网上查下
}
else { MessageBox.Show("记事本没有运行 "); }

}
li735937915 2011-12-09
  • 打赏
  • 举报
回复
求高手,多多指教!!!

110,537

社区成员

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

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

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