我正在使用PInvoke在.NET中使用SetForegroundWindow API.
当我在Visual Studio中调试时使用API时,它的工作非常完美.但是,当应用程序正常运行时,它始终不起作用.
我在调用SetForegroundWindow之前放了一些日志,并确认API已被调用但有时不生效.我也看过几个关于这个问题的帖子,但我想知道它为什么会失败.
该职位的链接如下:
我有Visual Studio 2010 Ultimate SP1,我的项目基于MFC.
当我调试我的项目的下一个代码时Visual Studio挂起:
CWnd* _window = CWnd::FromHandle(_hwnd_);
if (_window) {
DWORD nForeThread, nAppThread;
nForeThread = ::GetWindowThreadProcessId(::GetForegroundWindow(), 0);
nAppThread = GetCurrentThreadId();
if (nForeThread != nAppThread)
{
AttachThreadInput(nForeThread, nAppThread, TRUE);
_window->BringWindowToTop();
_window->ShowWindow(SW_RESTORE);
AttachThreadInput(nForeThread, nAppThread, FALSE);
}
else
{
_window->BringWindowToTop();
_window->ShowWindow(SW_RESTORE);
}
}
Run Code Online (Sandbox Code Playgroud)
我在下一行有一个断点:
AttachThreadInput(nForeThread, nAppThread, TRUE);
Run Code Online (Sandbox Code Playgroud)
因此,如果我按下F10或F11或F5按钮,则VS会立即挂起.
可能是什么问题呢?
我想这可能是不可能的。请证明我错了。
以下设置:
gui)通过创建一个来打开另一个应用程序(让我们称之为它server)new Process()server由其他人开发)以一个参数启动以隐藏其 GUIgui用户进行一些输入gui命令server执行一些任务server现在,由于整个用户体验需要针对重复操作进行优化,因此打开的 GUI 元素(窗口/表单/对话框)需要预先选择/聚焦/活动。
第一个问题的出现是因为我没有找到这些属性(Focus、Active、Selected、TopMost)之间差异的明确解释。
现在真正的问题是,我如何确保所有 GUI 元素都处于活动状态并被选中,无论它们是由我的gui进程还是由server进程启动?
使用 WINAPI 可以更强大我读到所以我定义了以下内容
// required to use WINAPI for RegainFocus();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SetActiveWindow(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static private void RegainFocus() …Run Code Online (Sandbox Code Playgroud)