From comments, the test I added in your WPF sample in MainWindow.xaml.cs
I added a Subclassing of the Static control to test events (Beep on Double-click)
=>
private IntPtr hWndContainer = IntPtr.Zero;
private SUBCLASSPROC SubClassDelegate;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource? hWndSource = PresentationSource.FromVisual(this) as HwndSource;
var nWidth = ((Panel)Application.Current.MainWindow.Content).ActualWidth;
var nHeight = ((Panel)Application.Current.MainWindow.Content).ActualHeight;
hWndContainer = CreateWindowEx(0, "Static", "", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_NOTIFY, 120, 10, (int)nWidth - 120 - 10, (int)nHeight - 10 * 2, hWndSource.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
// comment "handle = childWindowInterOp.Handle;" in MainWindow_Loaded
// "this.SizeChanged += MainWindow_SizeChanged;" could also be commented or MoveWindow added in MainWindow_SizeChanged
if (hWndContainer != IntPtr.Zero)
{
handle = hWndContainer;
SubClassDelegate = new SUBCLASSPROC(WindowSubClass);
bool bRet = SetWindowSubclass(hWndContainer, SubClassDelegate, 0, 0);
}
hWndSource.AddHook(WndProc);
}
private int nX = 110, nY = 0;
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SIZE)
{
if (hWndContainer != IntPtr.Zero)
MoveWindow(hWndContainer, nX, nY, LOWORD((int)lParam) - nX, HIWORD((int)lParam), true);
}
return IntPtr.Zero;
}
private int WindowSubClass(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, uint dwRefData)
{
switch (uMsg)
{
case WM_LBUTTONDBLCLK:
{
Console.Beep(6000, 10);
return 0;
}
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
Declarations :
public const int WS_OVERLAPPED = 0x0;
public const int WS_BORDER = 0x00800000;
public const int WS_POPUP = unchecked((int)0x80000000L);
public const int WS_CHILD = 0x40000000;
public const int WS_MINIMIZE = 0x20000000;
public const int WS_VISIBLE = 0x10000000;
public const int WS_DISABLED = 0x8000000;
public const int SS_NOTIFY = 0x00000100;
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr CreateWindowEx(int dwExStyle, string lpClassName, string lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport("User32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int cx, int cy, bool repaint);
public static int HIWORD(int n)
{
return (n >> 16) & 0xffff;
}
public static int LOWORD(int n)
{
return n & 0xffff;
}
public delegate int SUBCLASSPROC(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, uint dwRefData);
[DllImport("Comctl32.dll", SetLastError = true)]
public static extern bool SetWindowSubclass(IntPtr hWnd, SUBCLASSPROC pfnSubclass, uint uIdSubclass, uint dwRefData);
[DllImport("Comctl32.dll", SetLastError = true)]
public static extern int DefSubclassProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
public const int WM_SIZE = 0x0005;
public const int WM_LBUTTONDBLCLK = 0x0203;
public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;