为什么浏览器处理窗口试图把它带到屏幕前面什么都不做?
问题描述:
它与其他手柄窗口一起工作。为什么浏览器处理窗口试图把它带到屏幕前面什么都不做?
Process[] processes = Process.GetProcessesByName(processName);
SetProcessWindow.BringToFront(processes[0].Id);
SetProcessWindow.CenterProcessWindow(processes[0].Id);
如果processName
例如是Taskmgr
它会带来屏幕的前面,将居中任务管理器。
但如果例如processName
是explorer
(不是Internet Explorer,但探险家,在这种情况下是在硬盘上的目录),它不会把它带到前面也不会做任何事情。
这是SetProcessWindow
类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
namespace Automation
{
class SetProcessWindow
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNORMAL = 1;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
public static void BringToFront(int processId)
{
Process process = Process.GetProcessById(processId);
IntPtr handle = process.MainWindowHandle;
if (handle == IntPtr.Zero)
return;
ShowWindow(handle, SW_SHOWNORMAL);
SetForegroundWindow(handle);
}
public static void CenterProcessWindow(int processId)
{
Process process = Process.GetProcessById(processId);
while (process.MainWindowHandle == IntPtr.Zero)
{
process.Refresh();
break;
}
IntPtr handle = process.MainWindowHandle;
RECT rct;
GetWindowRect(handle, out rct);
Rectangle screen = Screen.FromHandle(handle).Bounds;
Point pt = new Point(screen.Left + screen.Width/2 - (rct.Right - rct.Left)/2, screen.Top + screen.Height/2 - (rct.Bottom - rct.Top)/2);
SetWindowPos(handle, IntPtr.Zero, pt.X, pt.Y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
}
}
不知道,也许它不会带来一些窗口前,因为我在未经检查的项目属性改变偏爱的32位?
无论如何,它带来了一些窗口,前面有些没有。
答
也许你在处理CenterProcessWindow
方法时遇到了同样的问题。尝试将这段代码添加到BringToFront
方法中。
while (process.MainWindowHandle == IntPtr.Zero)
process.Refresh();
此行前:IntPtr handle = process.MainWindowHandle;
。此外,你不需要break;
在while循环内。如果您在那里放置中断,即使MainWindoWHandle
仍然是IntPtr.Zero
,它也会从您的while循环中消失。你写它的方式是一样的:
if (process.MainWindowHandle == IntPtr.Zero)
process.Refresh();
如果是IntPtr.Zero
,刷新继续,你愿意等待它得到一些价值,因此while循环。
http://*.com/questions/20224673/focus-windows-explorer-from-c-sharp-app-in-task-bar http://*.com/questions/1132422/open-a-文件夹使用进程启动 –
Explorer创建*许多*窗口。当你使用MainWindowHandle时,你通常会得到任务栏。将它带入前台可能很有用,不太可能这就是你真正想要的。始终使用Spy ++实用程序来获得洞察。 ShellWindows COM对象允许迭代你正在寻找的那种窗口。 –
我的想法是真正让用户把应用带到最前面。像Skype一样任务栏,这是工作。其余的我将在稍后处理。我认为我一般想要的是工作。主要想法是用Elgato hd游戏捕获软件来完成它,并且它正在工作。 –