[轉]C#無邊框窗體移動的三種方法

 1. 重寫WndProc

protected override void WndProc(ref Message m)

{       
        
const int WM_NCHITTEST = 0x84;
        
const int HTCLIENT = 0x01;
        
const int HTCAPTION = 0x02;
         
if (m.Msg == WM_NCHITTEST)
        {
                
this.DefWndProc(ref m);
                
if (m.Result.ToInt32() == HTCLIENT)
                    m.Result 
= new IntPtr(HTCAPTION);
                
else
                    
base.WndProc(ref m);
        }
        
else
        {
                
base.WndProc(ref m);
        }
}

2.創建消息

private const int WM_NCLBUTTONDOWN = 0x00A1;
private const int WM_NCHITTEST = 0x84;
private const int HT_CAPTION = 0x2;
private const int HT_CLIENT = 0x1;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    
//this.Capture = false;
    pictureBox1.Capture = false;
    Message msg 
= Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
    WndProc(
ref   msg);   

3.調用API代碼

 

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


   
//在窗體的_MouseDown中加入如下代碼:
如:
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(
this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章