当前位置:编程学习 > C#/ASP.NET >>

VB.NET 控件移动

 Public Class Form1
Dim x As Double
    Dim y As Double
    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        x = e.X
        y = e.Y
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Button1.Left = x - (Button1.Width \ 2)
        Button1.Top = y - (Button1.Height \ 2)
        Timer1.Enabled = False
    End Sub
现在控件可以移动到鼠标点击的位置,但是我想实现动画的那种效果,控件慢慢的移动到鼠标点击的位置,不要一下就移动过去,我要看移动的过程.
       --------------------编程问答--------------------
Public Class Form1
    '鼠标点击时的坐标
    Dim x1 As Integer
    Dim y1 As Integer

    ''鼠标点击时button的坐标
    Dim x2 As Integer
    Dim y2 As Integer
    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        x1 = e.X
        y1 = e.Y

        x2 = Button1.Location.X
        y2 = Button1.Location.Y
        Timer1.Enabled = True
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If x1 < x2 Then
            x2 -= 1
        ElseIf x1 > x2 Then
            x2 += 1
        End If

        If y1 < y2 Then
            y2 -= 1
        ElseIf y1 > y2 Then
            y2 += 1
        End If

        Button1.Left = x2
        Button1.Top = y2

        If y2 = y1 AndAlso x2 = x1 Then
            ''到达目标地方,停止工作
            Timer1.Enabled = False
        End If
    End Sub
End Class



改变x2,y2的变化规律可以得到不同的移动路线. --------------------编程问答-------------------- 先谢谢xingyuebuyu的回复 但是如果改变移动速度BUTTON 上下跳动 以前我写出过这个的 在timer计时器里用了两行代码 但是忘了算法  很模糊了 我写下 
button1.left=button1.left+(x-(me.Width -button1.Width ))\2
button1.top=button1.top+(x-(me.Height -button1.Height ))\2
虽然写的不对 但是 就用了两行这样的代码 最后加上了
Timer1.Enabled = False
我会加分数 直到结贴 有多少分 送多少 送给回答正确的  --------------------编程问答--------------------
    ''' <summary>
    ''' 将指定控件移动到指定位置
    ''' </summary>
    ''' <param name="c">控件</param>
    ''' <param name="p">目标位置(父容器坐标系)</param>
    ''' <param name="movestep">移动次数</param>
    ''' <param name="time">移动耗时</param>
    ''' <remarks></remarks>
    Sub MoveTo(ByVal c As Control, ByVal p As Point, ByVal movestep As Integer, ByVal time As Integer)
        Dim l As Point = c.Location
        Dim xspeed As Single = (p.X - l.X) / movestep
        Dim yspeed As Single = (p.Y - l.Y) / movestep
        Dim steptime As Single = 1000 * time / movestep
        For i As Integer = 1 To movestep
            c.Left = l.X + i * xspeed
            c.Top = l.Y + i * yspeed
            Threading.Thread.Sleep(steptime)
            My.Application.DoEvents()
        Next
    End Sub

测试代码


    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        MoveTo(Button1, e.Location, 100, 2)
    End Sub
--------------------编程问答--------------------             c.Left = l.X + i * xspeed 
            c.Top = l.Y + i * yspeed 
不要用c.Left+=……去代替,因为Left是个整数,会导致移动速度的浮点被舍入,终点与目标点不吻合。 --------------------编程问答--------------------
引用 3 楼 zcsor 的回复:
    ''' <summary>
    ''' 将指定控件移动到指定位置
    ''' </summary>
    ''' <param name="c">控件 </param>
    ''' <param name="p">目标位置(父容器坐标系) </param>
    ''' <param name="movestep">移动次数 </param>
    ''' <param name="time">移动耗时 </param>
    ''' <remarks> </remarks>
    Sub MoveTo(ByVal c As Control, ByVal p As Point, ByVal movestep As Integer, ByVal time As Integer)
        Dim l As Point = c.Location
        Dim xspeed As Single = (p.X - l.X) / movestep
        Dim yspeed As Single = (p.Y - l.Y) / movestep
        Dim steptime As Single = 1000 * time / movestep
        For i As Integer = 1 To movestep
            c.Left = l.X + i * xspeed
            c.Top = l.Y + i * yspeed
            Threading.Thread.Sleep(steptime)
            My.Application.DoEvents()
        Next
    End Sub

测试代码


    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        MoveTo(Button1, e.Location, 100, 2)
    End Sub


就是这个了。 --------------------编程问答-------------------- Public Class Form1
    Dim x, y As Double
    Dim ay, ax As Double
    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        x = e.X
        y = e.Y
        ax = e.X - btnMove.Location.X
        ay = e.Y - btnMove.Location.Y
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim m As Double
        m = Math.Sqrt(ax * ax + ay * ay) * (0.001 * Timer1.Interval)

        If Math.Abs(x - btnMove.Left) < m Or Math.Abs(y - btnMove.Top) < m Then
            btnMove.Left = x
            btnMove.Top = y
            Timer1.Enabled = False
            Exit Sub
        End If
        If btnMove.Left = x And btnMove.Top = y Then
            Timer1.Enabled = False
            Exit Sub
        End If

        btnMove.Top += (m * ay) / Math.Sqrt(ax * ax + ay * ay)
        btnMove.Left += (m * ax) / Math.Sqrt(ax * ax + ay * ay)
    End Sub
End Class --------------------编程问答-------------------- 代码号多哦 眼花了 如果还不知道 我在贴 --------------------编程问答--------------------
Public Class Form1
    '鼠标点击时的坐标
    Dim x1 As Integer
    Dim y1 As Integer

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        x1 = e.X
        y1 = e.Y

        Timer1.Enabled = True
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Button1.Left += IIf(x1 = Button1.Left, 0, IIf(x1 > Button1.Left, 1, -1))
        Button1.Top += IIf(y1 = Button1.Top, 0, IIf(y1 > Button1.Top, 1, -1))

    End Sub
End Class
--------------------编程问答--------------------
引用 2 楼 wangjiankai1109 的回复:
先谢谢xingyuebuyu的回复 但是如果改变移动速度BUTTON 上下跳动 以前我写出过这个的 在timer计时器里用了两行代码 但是忘了算法  很模糊了 我写下
button1.left=button1.left+(x-(me.Width -button1.Width ))\2
button1.top=button1.top+(x-(me.Height -button1.Height ))\2
虽然写的不对 但是 就用了两行这样的代码 最后加上了
Timer1.Enabled = False
我会加分数 直到结贴 有多少分 送多少 送给回答正确的
Public Class Form1
    '鼠标点击时的坐标
    Dim x1 As Integer
    Dim y1 As Integer

    ''鼠标点击时button的坐标
    Dim x2 As Integer
    Dim y2 As Integer
    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        x1 = e.X
        y1 = e.Y

        x2 = Button1.Location.X
        y2 = Button1.Location.Y
        Timer1.Enabled = True
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Button1.Left = IIf(x1 = Button1.Left, x2, x1)
        Button1.Top = IIf(y1 = Button1.Top, y2, y1)

    End Sub
End Class
--------------------编程问答-------------------- 学习了!回复内容太短了! --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- Mark..........

学习了 --------------------编程问答-------------------- 举一个无边框窗体移动的例子
        private Point mouseOffset;   //坐标
        private bool isMouseDown = false;  //是否鼠标落下

        public BaseForm()
        {
            InitializeComponent();
        }
        //窗体移动事件
        private void BaseForm_MouseMove(object sender, MouseEventArgs e)
        {

            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                Location = mousePos;
            }
        }
        //鼠标放下事件
        private void BaseForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }
        //鼠标按下的事件
        private void BaseForm_MouseDown(object sender, MouseEventArgs e)
        {
            int xOffset;
            int yOffset;

            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                yOffset = -e.Y - SystemInformation.CaptionHeight -
                    SystemInformation.FrameBorderSize.Height;
                mouseOffset = new Point(xOffset, yOffset);
                isMouseDown = true;
            }
        } --------------------编程问答--------------------

Public Class Form1

    Dim x As Integer
    Dim y As Integer

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

        x = e.X - (Button1.Width \ 2)
        y = e.Y - (Button1.Height \ 2)

        Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Button1.Left = Button1.Left + (x - Button1.Left) \ 5
        Button1.Top = Button1.Top + (y - Button1.Top) \ 5

    End Sub

End Class
--------------------编程问答-------------------- --------------------编程问答--------------------

  Dim x1 As Double  '記錄點1的座標
    Dim y1 As Double

    Dim x2 As Double   '記錄點2的座標
    Dim y2 As Double
    Dim Counts As Double  '循環的次數
    Dim i As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        x2 = e.X
        y2 = e.Y
        If x1 <> x2 And y1 <> y2 Then
            Counts = CInt(Math.Sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2))
            For i = 1 To Counts
                If x1 < x2 Then
                    If x1 + i <= x2 Then
                        Button1.Left = x1 + i
                    End If
                Else
                    If x1 - i >= x2 Then
                        Button1.Left = x1 - i
                    End If
                End If
                If y1 < y2 Then
                    If y1 + i <= y2 Then
                        Button1.Top = y1 + i
                    End If
                Else
                    If y1 - i >= y2 Then
                        Button1.Top = y1 - i
                    End If
                End If
            Next
            x1 = x2
            y1 = y2
        End If
    End Sub
--------------------编程问答--------------------
 
    Dim x1 As Double  '記錄點1的座標
    Dim y1 As Double

    Dim x2 As Double   '記錄點2的座標
    Dim y2 As Double

    Dim Counts As Double  '循環的次數
    Dim i As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        x2 = e.X
        y2 = e.Y
        If x1 <> x2 Or y1 <> y2 Then
            Counts = CInt(Math.Sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2))
            For i = 1 To Counts Step 1
                If x1 < x2 Then
                    If x1 + i <= x2 Then
                        Button1.Left = x1 + i
                    End If
                Else
                    If x1 - i >= x2 Then
                        Button1.Left = x1 - i
                    End If
                End If
                If y1 < y2 Then
                    If y1 + i <= y2 Then
                        Button1.Top = y1 + i
                    End If
                Else
                    If y1 - i >= y2 Then
                        Button1.Top = y1 - i
                    End If
                End If
            Next
            x1 = x2
            y1 = y2
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        x1 = Button1.Left
        y1 = Button1.Top
    End Sub
--------------------编程问答-------------------- --------------------编程问答-------------------- 这个是正解,直接重写控件的这3个事件,MouseMove,MouseUp,MouseDown

引用 14 楼 marcellen 的回复:
举一个无边框窗体移动的例子
        private Point mouseOffset;   //坐标
        private bool isMouseDown = false;  //是否鼠标落下

        public BaseForm()
        {
            InitializeComponent();
        }
 ……
补充:.NET技术 ,  VB.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,