WPF禁用窗口移动 [英] WPF disable window moving

查看:241
本文介绍了WPF禁用窗口移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF如何从通过拖动标题栏移动窗口prevent用户?

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

推荐答案

既然你不能定义一个的WndProc 直接在WPF中,你需要获得 HwndSource ,并添加一个挂钩,以便它:

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;
}

这篇关于WPF禁用窗口移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆