如何拖动并使用鼠标移动的winform [英] How to drag and move winform using mouse

查看:340
本文介绍了如何拖动并使用鼠标移动的winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何'拖移动一个winform中加入以下code

 受保护的覆盖子的WndProc(为ByRef米作为消息)
    如果(((m.Msg = 163)和ClientRectangle.Contains(PointToClient(新点(m.LParam.ToInt32))))和(m.WParam.ToInt32 = 2))然后
        m.WParam = CTYPE(1,IntPtr的)
    结束如果
    MyBase.WndProc(米)
    如果((m.Msg = 132)和(m.Result.ToInt32 = 1))。然后
        m.Result = CTYPE(2,IntPtr的)
    结束如果
结束小组
 

但经过小组被添加到WinForm的,我不能拖动的面板区域内的WinForm的。在面板内如何拖放移动的任何想法?我的意思是把鼠标指向,点击,按住那板内移动,并在WinForm会跟随鼠标移动,直到我松开鼠标按钮。

更新:在解决我的问题

 添加到您的窗体类
私人MouseIsDown由于布尔= FALSE
私人MouseIsDownLoc作为点=无

这是你的面板MouseMove事件
私人小组panel_MouseMove(BYVAL发件人为对象,BYVAL E上MouseEventArgs)处理Panel3.MouseMove
    如果e.Button = MouseButtons.Left然后
        如果MouseIsDown = false,那么
            MouseIsDown = TRUE
            MouseIsDownLoc =新的点(e.X,e.Y)
        结束如果

        Me.Location =新的点(Me.Location.X +的eX  -  MouseIsDownLoc.X,Me.Location.Y + EY  -  MouseIsDownLoc.Y)
    结束如果
结束小组

和你的面板MouseUp事件
私人小组panel_MouseUp(BYVAL发件人为对象,BYVAL E上MouseEventArgs)处理Panel3.MouseUp
    MouseIsDown = FALSE
结束小组
 

解决方案

修改:改到VB.NET - 我真的需要开始阅读标签...

 添加到您的窗体类
私人MouseIsDown由于布尔= FALSE
私人MouseIsDownLoc作为点=无

这是你的面板MouseMove事件
私人小组panel_MouseMove(发送者为对象,E为MouseEventArgs)
    如果e.Button = MouseButtons.Left然后
        如果MouseIsDown = false,那么
            MouseIsDown = TRUE
            MouseIsDownLoc =新的点(e.X,e.Y)
        结束如果

        Me.Location =新的点(Me.Location.X +的eX  -  MouseIsDownLoc.X,Me.Location.Y + EY  -  MouseIsDownLoc.Y)
    结束如果
结束小组

和你的面板MouseUp事件
私人小组panel_MouseUp(发送者为对象,E为MouseEventArgs)
    MouseIsDown = FALSE
结束小组
 

I know how to 'drag and move' a winform by adding following code

Protected Overrides Sub WndProc(ByRef m As Message)
    If (((m.Msg = 163) And ClientRectangle.Contains(PointToClient(New Point(m.LParam.ToInt32)))) And (m.WParam.ToInt32 = 2)) Then
        m.WParam = CType(1, IntPtr)
    End If
    MyBase.WndProc(m)
    If ((m.Msg = 132) And (m.Result.ToInt32 = 1)) Then
        m.Result = CType(2, IntPtr)
    End If
End Sub

But after a panel being added to winform, I can not 'drag and move' the winform within that panel area. Any idea of how to 'drag and move' within a panel? I mean the mouse point, click, hold and move within that panel and the winform will follow the mouse movement until I release the mouse button.

Update: The solution to my problem.

'Add these to your form class
Private MouseIsDown As Boolean = False
Private MouseIsDownLoc As Point = Nothing

'This is the MouseMove event of your panel
Private Sub panel_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Panel3.MouseMove
    If e.Button = MouseButtons.Left Then
        If MouseIsDown = False Then
            MouseIsDown = True
            MouseIsDownLoc = New Point(e.X, e.Y)
        End If

        Me.Location = New Point(Me.Location.X + e.X - MouseIsDownLoc.X, Me.Location.Y + e.Y - MouseIsDownLoc.Y)
    End If
End Sub

'And the MouseUp event of your panel
Private Sub panel_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Panel3.MouseUp
    MouseIsDown = False
End Sub

解决方案

EDIT: Changed to VB.NET - I really need to start reading the tags...

'Add these to your form class
Private MouseIsDown As Boolean = False
Private MouseIsDownLoc As Point = Nothing

'This is the MouseMove event of your panel
Private Sub panel_MouseMove(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Left Then
        If MouseIsDown = False Then
            MouseIsDown = True
            MouseIsDownLoc = New Point(e.X, e.Y)
        End If

        Me.Location = New Point(Me.Location.X + e.X - MouseIsDownLoc.X, Me.Location.Y + e.Y - MouseIsDownLoc.Y)
    End If
End Sub

'And the MouseUp event of your panel
Private Sub panel_MouseUp(sender As Object, e As MouseEventArgs)
    MouseIsDown = False
End Sub

这篇关于如何拖动并使用鼠标移动的winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆