Reputation: 1971
I want to lock its Left coordinate.
This doesn't work as I want because there are glitches - flickering, because the window is moved back to Left=0. I'm more interested in something like LocationChanging to prevent it from ever moving left or right.
private void Window_LocationChanged(object sender, EventArgs e)
{
if (Left != 0) Left = 0;
}
Upvotes: 0
Views: 399
Reputation: 3601
One option is to catch WM_MOVING window message and manipulate its lParam. Since WM_MOVING comes before the Window moves, you can adjust the next position as you wish.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public partial class MainWindow : Window
{
private const int WM_MOVING = 0x0216;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private int _left;
private int _width;
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
_left = (int)this.Left;
_width = (int)this.Width;
var handle = new WindowInteropHelper(this).Handle;
var source = HwndSource.FromHwnd(handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_MOVING)
{
var position = Marshal.PtrToStructure<RECT>(lParam);
position.left = _left;
position.right = position.left + _width;
Marshal.StructureToPtr(position, lParam, true);
}
return IntPtr.Zero;
}
}
Upvotes: 1