首页  编辑  

客户区拖动窗体

Tags: /C#/界面处理/Form/   Date Created:

using System . Runtime . InteropServices;

[ DllImport ( "User32.DLL" )]

public static extern int SendMessage( IntPtr hWnd, uint Msg, int wParam, int lParam);

[ DllImport ( "User32.DLL" )]

public static extern bool ReleaseCapture();

public const uint WM_SYSCOMMAND = 0x0112 ;

public const int SC_MOVE = 61456 ;

public const int HTCAPTION = 2 ;

private void Form1_MouseDown( object sender , MouseEventArgs e)

{

   ReleaseCapture();

   SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0 );

}

// or

protected override void WndProc( ref Message m)

{

    const int WM_NCHITTEST = 0x0084 ;

    const int WM_NCLBUTTONDBLCLK   = 0x00A3 ;

    const int WM_SYSCOMMAND = 0x0112 ;

    const int HTCLIENT = 1 ;

    const int HTCAPTION = 2 ;

    switch (m . Msg)

   {

        case WM_NCHITTEST:

            base . WndProc( ref m);

            if (( int )m . Result == HTCLIENT)

               m . Result = ( IntPtr )HTCAPTION;

            return ;

        case WM_NCLBUTTONDBLCLK:

            return ;

   }

    base . WndProc( ref m);

}