In wpf how can i prevent user from moving the windows by dragging the title bar?
A:
Hi,
in a WinForms-Application, you can disable moving the window by overriding the WndProc-method.
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch(message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
{
return;
}
break;
}
base.WndProc(ref message);
}
If this is not possible in WPF, then i doubt that it's possable anyhow.
henchman
2010-03-08 11:15:02
There is no WndProc method in wpf Window class. But this doesn't mean I can't use this example.
naeron84
2010-03-08 11:22:43
+2
A:
Since you can't define a WndProc
directly in WPF, you need to obtain a HwndSource
, and add a hook to it :
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;
}
Thomas Levesque
2010-03-08 11:26:25
It's working, return value doesn't matter. So IntPrt.Zero is just fine.
naeron84
2010-03-08 12:30:17
Yes, I forgot the return value... According to the documentation of WM_SYSCOMMAND : "An application should return zero if it processes this message"
Thomas Levesque
2010-03-08 12:45:55