激活WPF窗口而不会失去以前的应用程序/窗口的焦点 [英] Activate WPF Window without losing focus on previous application/window

查看:651
本文介绍了激活WPF窗口而不会失去以前的应用程序/窗口的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序中有一个WPF窗口,通过调用 MainView.Activate(); MainView.BringIntoView() ; 方法。它也把重点放在这个MainView窗口上。

但是我的要求是这个窗口不应该得到Focus。即我的光标应该仍然保留在以前的应用程序(记事本,单词等..)

我尝试使用 MainView.ShowActivated =False但它没有工作。
是否需要使用HwndSource,如这里还是什么?



只有当Window最小化时):

  IntPtr HWND_TOPMOST = new IntPtr(-1); 
const short SWP_NOMOVE = 0X2;
const short SWP_NOSIZE = 1;
const short SWP_NOZORDER = 0X4;
const int SWP_SHOWWINDOW = 0x0040;

Process [] processes = Process.GetProcesses(。);

foreach(进程中的进程)
{
IntPtr handle = process.MainWindowHandle;
如果(处理!= IntPtr.Zero)
{
SetWindowPos(句柄,HWND_TOPMOST,0,0,0,SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);


$ / code $ / pre

解决方案

我的最近的博客文章,我使用 SetWindowPos 将窗口置于z顺序的前面而不给焦点。我不相信WPF有一个内置的方法实现没有p / invoke相同。

I have a WPF window in application which I Activate on some specific scenarios by calling MainView.Activate(); and MainView.BringIntoView(); method. it also sets focus on this 'MainView' window.

But my requirement is this window should not get Focus. i.e. my cursor should still remain on previous application(notepad,word etc..)

I tried using MainView.ShowActivated="False" but it didn't work. Do I need to use HwndSource as mentioned here or what?

Code I have used after Kent's help (Its working only if Window is minimized):

IntPtr HWND_TOPMOST = new IntPtr(-1);
            const short SWP_NOMOVE = 0X2;
            const short SWP_NOSIZE = 1;
            const short SWP_NOZORDER = 0X4;
            const int SWP_SHOWWINDOW = 0x0040;

            Process[] processes = Process.GetProcesses(".");

            foreach (var process in processes)
            {
                IntPtr handle = process.MainWindowHandle;
                if (handle != IntPtr.Zero)
                {
                    SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
                }
            }

解决方案

In my recent blog post, I use SetWindowPos to bring a window to the front of the z-order without giving it focus. I don't believe WPF has an in-built means of achieving the same without p/invoke.

这篇关于激活WPF窗口而不会失去以前的应用程序/窗口的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆