Normally, when the parent window is activated, the Child Window will lose the focus. Similarly, when the child window is activated, the parent window will also lose the focus, which is not very nice in some cases, for example, when a child window floats above the parent window as a ToolWindow. Visual Studio also seems to have this problem. When a floating DockingPanel is activated, the focus of the Visual Studio main window will be lost.
The last two images are more clear,
The general effect is as follows:
We are pursuing the following results:
Well, there is a simple package here. You can copy the code and use it directly (the Framework version requires 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 get1_whwnd (Window window Window)
{
Var helper = new WindowInteropHelper (window );
Return helper. Handle;
}
Private IntPtr FilterChildMessage (IntPtr hwnd, int msg, IntPtr wParam, IntPtr