无标题栏窗体移动的实现

 

方法一:

Private HTCAPTION As IntPtr = 2
    Private HTCLIENT As IntPtr = 1
    Private WM_NCHITTEST As Integer = 132

    Protected Overrides Sub WndProc(ByRef m As Message)
        If (m.Msg = WM_NCHITTEST) Then
            MyBase.WndProc(m)
            If (m.Result = HTCLIENT) Then
                m.Result = HTCAPTION
            End If
        ElseIf (m.Msg = 163) Then
        Else
            MyBase.WndProc(m)
        End If
    End Sub


 方法二(手柄控件 ):

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Size_MoveCtr
{
 /// <summary>
 /// Mover 的摘要说明。
 /// </summary>
 public class SizeGrip: System.Windows.Forms.Control
 {
  private const int WM_NCHITTEST = 0x84;
  private const int WM_NCLBUTTONDOWN = 0xa1;

  private const int HTCLIENT =  1;
  private const int HTBOTTOMRIGHT = 17;
 
  private System.Drawing.Drawing2D.GraphicsPath m_Path;

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SendMessage( IntPtr hWnd,
   int Msg,
   IntPtr wParam,
   IntPtr lParam
   );

  public SizeGrip()
  {
   this.m_Path = new System.Drawing.Drawing2D.GraphicsPath();

   this.m_Path.StartFigure();
   this.m_Path.AddLines(new Point[]{new Point(this.Width, 0),new Point(this.Width-this.Height, this.Height), new Point(this.Width, this.Height)});
   this.m_Path.CloseFigure();
  }

  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint (e);
   ControlPaint.DrawSizeGrip(this.CreateGraphics(), System.Drawing.SystemColors.Control, this.Width-this.Height, 0, this.Height, this.Height);

  }

  protected override void OnSizeChanged(EventArgs e)
  {
   System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
   path.StartFigure();
   path.AddLines(new Point[]{new Point(this.Width, 0),new Point(this.Width-this.Height, this.Height), new Point(this.Width, this.Height)});
   path.CloseFigure();

   Region reReg = new Region(path);
   reReg.Complement(m_Path);

   this.Invalidate(reReg);

   base.OnSizeChanged(e);
   this.m_Path = path;

   ControlPaint.DrawSizeGrip(this.CreateGraphics(), System.Drawing.SystemColors.Control, this.Width-this.Height, 0, this.Height, this.Height);
  }

  protected override void WndProc(ref Message m)
  {
   if (this.DesignMode)
   {
    base.WndProc(ref m);
    return;
   }

   switch (m.Msg)
   {
    case WM_NCHITTEST:
     SendMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
     base.WndProc(ref m);
     if ((int)m.Result == HTCLIENT)
     {
      m.Result = (IntPtr)HTBOTTOMRIGHT;//右下
     }
     break;
    case WM_NCLBUTTONDOWN:
     SendMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
     break;
    default:
     base.WndProc(ref m);
     break;
   }
  }
 }
}

方法三:

先将 FormBorderStyle 设置为 FormBorderStyle.None,然后:

 Dim theScreen As Rectangle
    
Dim MousX As Short
    
Dim MousY As Short
    
Dim CurrX As Short
    
Dim CurrY As Short
    
Dim MoveScreen As Boolean
    
Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        theScreen 
= Screen.PrimaryScreen.Bounds()
        Me.Location 
= New Point((theScreen.Width - Me.Width) / 2, (theScreen.Height - Me.Height) / 2)     
    
End Sub


    
Private Sub Form1_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        CurrX 
= Me.Left - MousX + e.X
        CurrY 
= Me.Top - MousY + e.Y
        
If MoveScreen = True Then
            Me.Location 
= New Point(CurrX, CurrY)
        
End If
    
End Sub


    
Private Sub Form1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        MousX 
= e.X
        MousY 
= e.Y
        MoveScreen 
= True
    
End Sub


    
Private Sub Form1_MouseUp(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        MoveScreen 
= False
    
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值