当前位置:编程学习 > C#/ASP.NET >>

让子窗口和父窗口同时处于激活状态

一般情况下,激活父窗口的时候,子窗口会失去焦点,同理,激活子窗口的时候,父窗口也会失去焦点,这在某些时候不太好看,比如子窗口作为ToolWindow漂浮在父窗口上面时。Visual Studio好像也有这个问题,当激活其中某个处于浮动状态的DockingPanel时,Visual Studio主窗口会失去焦点。
上两幅图,更明白点,
一般效果如下:
\

我们现在追求的效果如下:
\

 

恩,这里有个简单的包装,呵呵,复制下代码就可以直接使用了(Framework版本需要3.5):
 

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace YourNamespace
{
    public static class WindowActiveService
    {
        internal class WindowActiveHelper
        {
            [DllImport("user32", CharSet = CharSet.Auto)]
            private extern static int SendMessage(
               IntPtr handle, int msg, int wParam, IntPtr lParam);

            [DllImport("user32.dll")]
            private static extern IntPtr GetForegroundWindow();

// ReSharper disable InconsistentNaming
            private const int WM_NCACTIVATE = 0x086;
// ReSharper restore InconsistentNaming
            private IntPtr ownerHwnd = IntPtr.Zero;
            private IntPtr childHwnd = IntPtr.Zero;
            private HwndSource ownerHwndSource;
            private HwndSource childHwndSource;
            private HwndSourceHook ownerHook;
            private HwndSourceHook childHook;
            private bool childActive;
            private bool ownerActive;

            private static IntPtr GetWindowHwnd(Window window)
            {
                var helper = new WindowInteropHelper(window);
                return helper.Handle;
            }

            private IntPtr FilterChildMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr 
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,