Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi How to set my wpf window as no movable when it is clicked and dragged using mouse?
Posted
Updated 1-Sep-22 2:35am

Try intercepting the calls, like this;

C#
using System;
using System.Windows;
using System.Windows.Interop;

namespace WpfApplication23
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            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);
        }


        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;

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


Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
Red Chocolate 7-Nov-12 22:28pm    
ya thanks but i need the title bar also
Fredrik Bornander 8-Nov-12 4:29am    
Right, check the new suggestion, it handles the scenario while still allowing you to use any window style.
add this to your window Xaml ResizeMode="NoResize" this will disable the user from resizing the window as for the moving im not sure.

Found it add IsDraggingOrResizing="False" this should do the trick!

Wait sorry nevermind. This is only for DevExpress Windows...
 
Share this answer
 
v3
Thanks @Fredrick for the codes, however, just to mention, I noticed that using the ResizeMode = "NoResize" disables the minimize button, and you might not want that
 
Share this answer
 
Comments
CHill60 1-Sep-22 9:17am    
If you want to comment on a solution then use the "Have a Question or Comment?" link next to it. It is highly unlikely that Fredrick will see your post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900