这个是从别的地方看来的,放我这里
第一种:SetForegroundWindow,这个方法时灵时不灵。有人说,在自己的程序里把自己的窗口之前一般就不灵,而置前其它程序的窗口就灵。我觉得这是有原因的:当一个程序运行这个函数的时候,我们可以想象,如果是用鼠标操作,那么当前窗口实际上正在操作,很可能在瞬间离开后,焦点又回来了,那么看上去更就没有吧其它窗口置前的效果,所以这个函数内部可能做了延迟,略等一下,在这个时间内,当前窗口不会被置前。就是这个时间,函数已经调用完成,所以看上去,自己的窗口并没有置前。
第二种:SwitchToThisWindow,这个方法很好使,还会把最小化的窗口复原。但是Windows说,这个函数以后可能不支持。但是我觉得可以放心使用,Windows当前一般就那几个版本,程序不会计划运行100年不升级吧。这个函数可以直接链接,但是XP以前的版本可能不行,所以,使用动态加载可能保险点。动态加载代码:
- typedef void (WINAPI*PROCSWITCHTOTHISWINDOW)(HWND,BOOL);
- PROCSWITCHTOTHISWINDOW SwitchToThisWindow;
- HMODULE hUser32 = GetModuleHandle(L"user32");
- SwitchToThisWindow = (PROCSWITCHTOTHISWINDOW)GetProcAddress(hUser32,"SwitchToThisWindow");
- SwitchToThisWindow(frame,1);
第三种方法:BringWindowToTop,这个函数比上一个功能少点,实质是调用SetWindowPos函数。
我的具体用法是这样子
//[DllImport("user32.dll")]
//public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll ", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
//[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
//public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
public const int SW_RESTORE = 9;
public static IntPtr formhwnd;
Process[] MyProcesses = Process.GetProcesses();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = @"D:\开发区工作\钢筋软件\开发资料\2017\源代码\MTC部分\廊坊凯博\KBMES\";
startInfo.FileName = "Client.exe";
bool temp = false;
foreach (Process MyProcess in MyProcesses)
{
if (MyProcess.ProcessName == "Client")
{
//IntPtr handle = MyProcess.MainWindowHandle;
//ShowWindow(handle,1);
temp = true;
SwitchToThisWindow(MyProcess.MainWindowHandle,true);
break;
}
}
if (temp == false)
Process.Start(startInfo);
后面还看到
SwitchToThisWindow这个是遗留函数,也就是微软为了和以前版本windows兼容而留下来的,以后的版本可能会被移除。
SetForegroundWindow就是推荐的函数。
SetActiveWindow这三者有什么区别和联系 SetActiveWindow不能激活后台程序,也就是窗口被隐藏了,不能用它激活。
ShowWindow的API函数是显示窗口,
它的第二个参数是决定窗体的状态的。
Value | Meaning |
---|---|
|
Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread. |
|
Hides the window and activates another window. |
|
Maximizes the specified window. |
|
Minimizes the specified window and activates the next top-level window in the Z order. |
|
Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. |
|
Activates the window and displays it in its current size and position. |
|
Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. |
|
Activates the window and displays it as a maximized window. |
|
Activates the window and displays it as a minimized window. |
|
Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated. |
|
Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated. |
|
Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated. |
|
Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. |
好吧,这些都在C#用到了非托管类的API了,不知道VS里面的类有没有这样的功能。