【发布时间】:2012-05-31 04:56:12
【问题描述】:
我正在尝试使用 c# 将进程窗口设置为前台/焦点(从当时没有焦点的应用程序),因此我使用 user32.dll static extern bool SetForegroundWindow(IntPtr hWnd) 方法:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public void setFocus()
{
SetForegroundWindow(process.MainWindowHandle);
}
一切正常,但只要我打开了visual studio 2008,我什至不需要从VS08启动应用程序,用它打开项目就足够了。在我关闭项目的那一刻,我的应用程序无法将另一个窗口设置为前台。唯一的结果是,在任务栏中,另一个窗口以蓝色突出显示。当我要再次使用 VS08 打开我的项目时,它工作正常。
我一点也不知道为什么...我认为问题可能是他无法导入 dll 但它不会被突出显示,并且其他 win32 函数(如static extern bool ShowWindow(IntPtr hWnd, IntPtr status);)即使在项目时也能正常工作已关闭。
这个问题的任何解决方案或提示?
编辑: 我看了这个函数的注释,我的想法是我的应用程序没有焦点,所以我尝试了这个:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
static extern bool AllowSetForegroundWindow(int procID);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public void setAUTFocus()
{
IntPtr hWnd = GetForegroundWindow();
uint processID = 0;
uint threadID = GetWindowThreadProcessId(hWnd, out processID);
int intID = (int)processID;
AllowSetForegroundWindow(intID);
SetForegroundWindow(process.MainWindowHandle);
}
现在我正在搜索当前具有焦点的窗口进程并为此窗口设置AllowSetForegroundWindow 并尝试现在将焦点设置在我的窗口上。但是同样的问题,当我在 VS 中打开项目时它正在工作,如果不是,我只会在任务栏中得到蓝色突出显示。
在我的应用程序运行期间,我可以打开/关闭 vs 项目,它打开的那一刻一切正常,它关闭的那一刻它不起作用,我在运行我的应用程序时与 VS 项目没有交互。说真的,我不明白。
【问题讨论】:
-
见SetForeground窗口文档msdn.microsoft.com/en-us/library/windows/desktop/…中的备注
-
所以当我试图将另一个窗口放在前面时,我必须先调用函数 AllowSetForegroundWindow,并使用焦点窗口的进程 id ?
-
这不是为什么它在 VS 项目打开时工作的原因,当前前台窗口永远不是 VS
-
这不是答案,我只是发表评论以引起您对此事的注意
-
这不适用于 UWP,因为此平台不支持检索有关本地进程的信息。
标签: c# visual-studio window focus