14
在wpf我如何防止用户通过拖动标题栏来移动窗口?WPF禁用窗口移动
在wpf我如何防止用户通过拖动标题栏来移动窗口?WPF禁用窗口移动
既然你不能在WPF直接定义WndProc
,你需要获得HwndSource
,并添加一个钩到它:
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;
}
没关系,+1你;经验教训:) – 2010-03-08 11:33:48
WndProc应该返回什么? IntPtr.Zero? – naeron84 2010-03-08 12:09:50
它的工作原理,返回值无关紧要。所以IntPrt.Zero就好了。 – naeron84 2010-03-08 12:30:17