gpt4 book ai didi

.net - WPF 禁用窗口移动

转载 作者:行者123 更新时间:2023-12-02 10:55:13 24 4
gpt4 key购买 nike

在wpf中如何阻止用户通过拖动标题栏来移动窗口?

最佳答案

由于您无法直接在 WPF 中定义 WndProc,因此您需要获取 HwndSource,并为其添加一个钩子(Hook):

public Window1()
{
InitializeComponent();

this.SourceInitialized += Window1_SourceInitialized;
}

private void Window1_SourceInitialized(object sender, EventArgs e)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
HwndSource source = HwndSource.FromHwnd(helper.Handle);
source.AddHook(WndProc);
}

const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{

switch(msg)
{
case WM_SYSCOMMAND:
int command = wParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
{
handled = true;
}
break;
default:
break;
}
return IntPtr.Zero;
}

关于.net - WPF 禁用窗口移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2400819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com