+ -
当前位置:首页 → 问答吧 → Butten问题

Butten问题

时间:2011-12-06

来源:互联网

运行时,窗体上可以自由拉动移动butten。
请问要怎么写代码?

作者: lxr20101112   发布时间: 2011-12-06

处理button的拖动事件

作者: niaoked   发布时间: 2011-12-06

处理鼠标事件

作者: bdmh   发布时间: 2011-12-06

C# code

    Point _oldPosition = new Point();  
      
    private void button1_MouseMove(object sender, MouseEventArgs e)  
    {  
        if (e.Button == MouseButtons.Left)  
        {  
            panel1.Left += Cursor.Position.X - _oldPosition.X;  
            panel1.Top += Cursor.Position.Y - _oldPosition.Y;  
            _oldPosition = Cursor.Position;  
         }  
    }  
      
    private void button1_MouseDown(object sender, MouseEventArgs e)  
    {  
        _oldPosition = Cursor.Position;  
    }  


获取鼠标拖动坐标: 

    int x, y;  
      
    private void Form1_MouseDown(object sender, MouseEventArgs e)  
    {  
        x = e.X;  
        y = e.Y;  
    }  
      
    private void Form1_MouseUp(object sender, MouseEventArgs e)  
    {  
        MessageBox.Show("鼠标按下坐标:" + x + "," + y + "\n鼠标释放坐标:" + e.X + "," + e.Y);  
    }  

作者: taomanman   发布时间: 2011-12-06

在窗体上创建可自由移动的控件的方法

This sample shows how to move controls over a form.
Be aware that there is no check build in to see if it goes outside the borders. 

To try it, just open a class paste it in and set in the project properties the right start point to Form1. 

Public Class Form1
  Inherits System.Windows.Forms.Form
  Public Sub New()
  MyBase.New()
  End Sub

  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  If disposing Then
  If Not (components Is Nothing) Then
  components.Dispose()
  End If
  End If
  MyBase.Dispose(disposing)
  End Sub

  Private components As System.ComponentModel.IContainer
  Private WithEvents Label1 As New System.Windows.Forms.Label
  Private WithEvents Label2 As New System.Windows.Forms.Label
  Private mouseX, mouseY As Integer
  Private arLabels() As Label
  Dim myMousedown As String

  Private Sub Form1_Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
  Me.ClientSize = New System.Drawing.Size(400, 400)
  Label1.Name = "Label1"
  Label2.Name = "Label2" '更多.net源码和实例,来自[乐 博 网 www.lob.cn]
  arLabels = New Label() {Label1, Label2}
  Dim lblY As Integer = 100
  For Each Lbl As Label In arLabels
  Lbl.Location = New System.Drawing.Point(100, lblY)
  Lbl.ForeColor = Color.Red
  Lbl.BackColor = Color.Transparent
  Lbl.TextAlign = ContentAlignment.MiddleCenter
  Lbl.Text = Lbl.Location.X.ToString & "." & Lbl.Location.Y.ToString
  AddHandler Lbl.MouseDown, AddressOf Label_MouseDown
  AddHandler Lbl.MouseUp, AddressOf Label_MouseUp
  AddHandler Lbl.MouseMove, AddressOf Label_MouseMove
  lblY += 30
  Me.Controls.Add(Lbl)
  Next
  End Sub

  Private Sub Label_MouseDown(ByVal sender As Object, ByVal _
  e As System.Windows.Forms.MouseEventArgs)
  Dim lbl As Label = DirectCast(sender, Label)
  myMousedown = lbl.Name
  lbl.BringToFront()
  mouseX = Cursor.Position.X - lbl.Location.X
  mouseY = Cursor.Position.Y - lbl.Location.Y
  lbl.Cursor = Cursors.Hand
  End Sub

  Private Sub Label_MouseUp(ByVal sender As Object, ByVal e As _
  System.Windows.Forms.MouseEventArgs)
  Dim lbl As Label = DirectCast(sender, Label)
  myMousedown = ""
  lbl.Cursor = Cursors.Default
  End Sub

  Private Sub Label_MouseMove(ByVal sender As Object, ByVal e _
  As System.Windows.Forms.MouseEventArgs)
  Dim lbl As Label = DirectCast(sender, Label)
  Static LastCursor As Point
  Dim NowCursor As Point = New Point(Cursor.Position.X, Cursor.Position.Y)
  If Point.op_Inequality(NowCursor, LastCursor) Then
  If myMousedown = lbl.Name Then
  lbl.Location = New System.Drawing.Point(Cursor.Position.X _
  - mouseX, Cursor.Position.Y - mouseY)
  End If
  LastCursor = Cursor.Position
  lbl.Text = lbl.Location.X.ToString & "." & lbl.Location.Y.ToString
  End If
  End Sub
End Class 

作者: Try__hard   发布时间: 2011-12-06

C# code

        const uint WM_SYSCOMMAND = 0x0112;
        const uint SC_MOVE = 0xF010;
        const uint HTCAPTION = 0x0002;

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);
        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();


        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
             if (e.Button == MouseButtons.Left)
              {
                  this.Cursor = Cursors.SizeAll;
                  ReleaseCapture();
                  SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
                  this.Cursor = Cursors.Default;
              }
        }


根据网络知识改写(非原创)
测试通过

作者: MKing0412   发布时间: 2011-12-07

热门下载

更多