相关疑难解决方法(0)

了解AttachThreadInput - 分离失去焦点

我有一个完全理解AttachThreadInput的问题.

我知道它是"连接"2个线程的消息队列,这(我想做的)允许我例如强制我的窗口(winforms)在前台.

我可以用这种方法做什么:

private void SetForegroundWindowEx(IntPtr hWnd)
{
    uint SW_SHOW = 5;
    uint appThread = GetCurrentThreadId();     
    uint foregroundThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);         

    if (foregroundThread != appThread)
    {
        AttachThreadInput(foregroundThread, appThread, true);

        BringWindowToTop(hWnd);
        ShowWindow(hWnd, SW_SHOW);       
        AttachThreadInput(foregroundThread, appThread, false);

    }
    else
    {
        BringWindowToTop(hWnd);
        ShowWindow(hWnd, SW_SHOW);
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,一旦线程分离,两个窗口都会失去焦点.

如果我等待消息队列清空(Application.DoEvents())并激活我的窗口(现在处于前台但没有聚焦),它将重新获得焦点并保留它.

如果我在消息队列为空之前执行此操作,它将再次失去焦点.

所以我猜想分离的东西会从我的窗口获得焦点,但我不知道它是什么或如何防止它.

这是我不太了解的第一件事.

我没有得到的第二件事是,如果我没有将窗口设置为前景:

AttachThreadInput(foregroundThread, appThread, true);

AttachThreadInput(foregroundThread, appThread, false);
Application.DoEvents();
this.Activate();
Run Code Online (Sandbox Code Playgroud)

等待消息队列清空,并激活我的窗口(这次它不在前台而另一个窗口仍有焦点),它实际上被激活,即使线程不再连接.

或许对AttachThreadInput有更好理解的人可以回答我这2个问题.

信息:
我需要在这种情况下窃取焦点,因为我的应用程序是通过API调用的.调用我的另一个应用程序,等待我的应用程序的反馈,并在大多数时候冻结,直到它获取信息.

如果其他应用程序是全屏,许多用户不会注意到任务栏中的闪烁,并认为其他应用程序崩溃并使用Taskmamanger将其终止.由于我不一定能控制其他应用程序,我无法告诉它将焦点设置到我的窗口.

如果不是绝对必要的话,这个方法就不会被调用,在这种情况下,我认为这种行为是敌对的,我自己和用户一样.

c# winapi multithreading winforms

11
推荐指数
1
解决办法
7379
查看次数

SetForegroundWindow仅在visual studio打开时工作

我正在尝试用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)

c# focus window visual-studio

9
推荐指数
2
解决办法
9099
查看次数

.NET中的SetForegroundWindow问题

我正在使用PInvoke在.NET中使用SetForegroundWindow API.

当我在Visual Studio中调试时使用API​​时,它的工作非常完美.但是,当应用程序正常运行时,它始终不起作用.

我在调用SetForegroundWindow之前放了一些日志,并确认API已被调用但有时不生效.我也看过几个关于这个问题的帖子,但我想知道它为什么会失败.

该职位的链接如下:

c# pinvoke winapi visual-studio-2012

9
推荐指数
3
解决办法
7485
查看次数