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

Vb.Net 怎么让窗口随鼠标动

各位大哥大姐帮忙啊 --------------------编程问答-------------------- 你的意思是不是鼠标拖拽窗体啊!

先要定义一个全局变量记录坐标:
Private m_ptDragStart As New Point(0, 0)

再处理鼠标的按下和移动2个事件。

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) 
            If e.Button = MouseButtons.Left Then //确定鼠标左键执行
                m_ptDragStart.X = e.X 
                m_ptDragStart.Y = e.Y 
            End If 
        End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) 
            If e.Button = MouseButtons.Left Then //确定鼠标左键执行
                Dim ptOrg As Point = Me.Location 
                Dim x As Integer = e.X - m_ptDragStart.X 
                Dim y As Integer = e.Y - m_ptDragStart.Y 
                If Math.Abs(x) > 1 OrElse Math.Abs(y) > 1 Then 
                    Me.Location = New Point(ptOrg.X + x, ptOrg.Y + y) 
                End If 
            End If 
        End Sub

完整代码:

Imports System 
Imports System.Collections.Generic 
Imports System.ComponentModel 
Imports System.Data 
Imports System.Drawing 
Imports System.Text 
Imports System.Windows.Forms 

Namespace WindowsApplication1 
    Public Partial Class Form1 
        Inherits Form 
        Private m_ptDragStart As New Point(0, 0) 
        
        Public Sub New() 
            InitializeComponent() 
        End Sub 
        
        Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) 
            If e.Button = MouseButtons.Left Then 
                m_ptDragStart.X = e.X 
                m_ptDragStart.Y = e.Y 
            End If 
        End Sub 
        
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) 
            If e.Button = MouseButtons.Left Then 
                Dim ptOrg As Point = Me.Location 
                Dim x As Integer = e.X - m_ptDragStart.X 
                Dim y As Integer = e.Y - m_ptDragStart.Y 
                If Math.Abs(x) > 1 OrElse Math.Abs(y) > 1 Then 
                    Me.Location = New Point(ptOrg.X + x, ptOrg.Y + y) 
                End If 
            End If 
        End Sub 
    End Class 
End Namespace
--------------------编程问答--------------------
引用 1 楼 pp_shy 的回复:
你的意思是不是鼠标拖拽窗体啊!

先要定义一个全局变量记录坐标:
Private m_ptDragStart As New Point(0, 0)

再处理鼠标的按下和移动2个事件。

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
            If e.Button = MouseButtons.Left Then //确定鼠标左键执行
                m_ptDragStart.X = e.X
                m_ptDragStart.Y = e.Y
            End If
        End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
            If e.Button = MouseButtons.Left Then //确定鼠标左键执行
                Dim ptOrg As Point = Me.Location
                Dim x As Integer = e.X - m_ptDragStart.X
                Dim y As Integer = e.Y - m_ptDragStart.Y
                If Math.Abs(x) > 1 OrElse Math.Abs(y) > 1 Then
                    Me.Location = New Point(ptOrg.X + x, ptOrg.Y + y)
                End If
            End If
        End Sub

完整代码:
VB.NET codeImports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports System.TextImports System.Windows.FormsNamespace WindowsApplication1PublicPartialClass Form1Inherits FormPrivate m_ptDragStartAsNew Point(0,0)PublicSubNew() 
            InitializeComponent()End SubPrivateSub Form1_MouseDown(ByVal senderAsObject,ByVal eAs MouseEventArgs)If e.Button= MouseButtons.LeftThen 
                m_ptDragStart.X= e.X 
                m_ptDragStart.Y= e.YEndIfEnd SubPrivateSub Form1_MouseMove(ByVal senderAsObject,ByVal eAs MouseEventArgs)If e.Button= MouseButtons.LeftThenDim ptOrgAs Point=Me.LocationDim xAsInteger= e.X- m_ptDragStart.XDim yAsInteger= e.Y- m_ptDragStart.YIf Math.Abs(x)>1OrElse Math.Abs(y)>1ThenMe.Location=New Point(ptOrg.X+ x, ptOrg.Y+ y)EndIfEndIfEnd SubEnd ClassEnd Namespace


lz试试看吧,呵呵
很详细( ⊙ o ⊙ )! --------------------编程问答-------------------- 不是也,我的意思移动鼠标,窗体就自动跟鼠标动 --------------------编程问答-------------------- 你试试把鼠标的当前坐标付值给当前窗口 --------------------编程问答-------------------- 好真的是很好的好啊 啊啊啊啊好的安徽哦啊 --------------------编程问答-------------------- 快来帮帮忙啊。。。。。。。。。。。。。。。。 --------------------编程问答-------------------- 记录鼠标的按下事件,把坐标记录下来,然后赋给窗体.
另外把窗体的最大化最小化状态排除在外. 这个思路是对的. --------------------编程问答-------------------- 应该是类似网页中的广告插件,无论点哪都会弹广告的那种 --------------------编程问答-------------------- 关注~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --------------------编程问答-------------------- 这种效果貌似不会讨人欢喜. --------------------编程问答-------------------- 同感,这种效果令人生厌。 --------------------编程问答-------------------- 我也很讨厌这种效果,不是很道德 --------------------编程问答--------------------

 Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        MyBase.OnMouseDown(e)
        If (e.Button = MouseButtons.Left) Then
            MyBase.Capture = False
            FormShowRegCodeDoubleline.SendMessage(MyBase.Handle, &HA1, 2, 0)
        End If
    End Sub

    <DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wparam As Integer, ByVal lparam As Integer) As IntPtr
    End Function
--------------------编程问答-------------------- FormShowRegCodeDoubleline可以忽略,因为是静态方法,以前带上类名了。 --------------------编程问答-------------------- 新建一窗体FORM2,在原窗体FORM1的在mousemove事件中增加FORM2.left=XX,FORM2.TOP=YY.XX,yy是鼠标的坐标.
--------------------编程问答-------------------- 我想在窗口里放个飞动的蝴蝶,透明化,然后跟着鼠标飞动。我用vb6,不能动啊 --------------------编程问答-------------------- 刚才改了一下窗口的scalemode,把twip改成point就能动了,呵呵。
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,