我正在尝试用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);
}
Run Code Online (Sandbox Code Playgroud)
一切都工作得很好,但只有当我打开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; …Run Code Online (Sandbox Code Playgroud)