移动无标题窗体

来源:互联网 发布:matlab 矩阵中最小值 编辑:程序博客网 时间:2024/05/04 11:39
1.       const int WM_NCHITTEST = 0x0084;
        const int HTCLIENT = 0x0001;
        const int HTCAPTION = 0x0002;
        protected override void WndProc(ref System.Windows.Forms.Message m)
       {
            switch(m.Msg)
           {
                    case WM_NCHITTEST:
                             base.WndProc(ref m);
                             if (m.Result==(IntPtr)HTCLIENT)
                                      m.Result=(IntPtr)HTCAPTION;
                             break;
                    default:
                             base.WndProc(ref m);
                             break;
           }

      }

2.

通过API来处理,需要引入System.Runtime.InteropServices;

  [DllImport("user32.dll")]
  public static extern bool ReleaseCapture();
  [DllImport("user32.dll")]
  public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam);
                                      
  public const int WM_SYSCOMMAND=0x0112;
  public const int SC_MOVE=0xF010;
  public const int HTCAPTION=0x0002;
  
  private void Form2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   ReleaseCapture();
   SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0);
  }

0 0
原创粉丝点击