Reputation: 3025
In wpf how can i prevent user from moving the windows by dragging the title bar?
Upvotes: 21
Views: 15375
Reputation: 46
alt + space would still bring up the window context menu, giving you access to all commands. You would need to implement logic to override the default menu item behaviour to make this example work properly.
Upvotes: 0
Reputation: 41
Really old thread and other techniques like UWP and WinUI3 are out there but maybe my suggestions are helpful to others.
I achieve the goal by setting
WindowStyle="None"
Add a button to the window and set
IsCancel="True"
That's it. No need for interop code. Minimized code.
Upvotes: 4
Reputation: 292455
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;
}
Upvotes: 43