private class WindowLockHook { private readonly Window Window;
public WindowLockHook(Window window) { this.Window = window;
var source = PresentationSource.FromVisual(window) as HwndSource; if (source == null) { // If there is no hWnd, we need to wait until there is window.SourceInitialized += Window_SourceInitialized; } else { source.AddHook(WndProc); } }
public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_WINDOWPOSCHANGING && GetIsLocked(Window)) { var wp = Marshal.PtrToStructure<WINDOWPOS>(lParam); wp.flags |= SWP_NOMOVE | SWP_NOSIZE; Marshal.StructureToPtr(wp, lParam, false); }
return IntPtr.Zero; } } }
internal static class NativeMethods { [StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; }
[StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public int flags; }
public const int SWP_NOMOVE = 0x0002, SWP_NOSIZE = 0x0001;