Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF disable window moving

Tags:

In wpf how can i prevent user from moving the windows by dragging the title bar?

like image 870
naeron84 Avatar asked Mar 08 '10 11:03

naeron84


1 Answers

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;
}
like image 119
Thomas Levesque Avatar answered Sep 18 '22 15:09

Thomas Levesque