この投稿の助けを借りて問題を解決することができました。
これがコードです
public partial class MainWindow : Window
{
private double right;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
right = Left + Width;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == (int)WindowMessage.WindowPositionChanging)
{
var windowPosition = (WindowPosition)Marshal.PtrToStructure(lParam, typeof(WindowPosition));
bool isMove = !windowPosition.Flags.HasFlag(WindowPositionFlags.NoMove); //0x0002
bool isSize = !windowPosition.Flags.HasFlag(WindowPositionFlags.NoSize); //0x0001
if (isMove)
{
right = windowPosition.Left + windowPosition.Width;
}
else if (isSize)
{
windowPosition.Left = (int)(right - windowPosition.Width);
windowPosition.Top = (int)Top;
windowPosition.Flags = (WindowPositionFlags)((int)windowPosition.Flags & 0xfffd); //remove the NoMove flag
Marshal.StructureToPtr(windowPosition, lParam, true);
}
}
return IntPtr.Zero;
}
それが誰かを助けることを願っています