drag and drop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dave

    drag and drop

    Hi

    How do i drag and drop a command button using the mouse in runtime?

    Its driving me mad

    I am using Visual Basic 6

    Thanx Dave


  • Rick Rothstein

    #2
    Re: drag and drop

    > How do i drag and drop a command button using the mouse in runtime?

    Give the following a try. To see it work, start a new project, place a
    CommandButton (use the default Name) on the form, paste the following code
    into the Form's code window and then Run the project. Clicking on the
    CommandButton will work as before. However, if you press the Control Key
    first (you can change that to the Shift or Alt key if you want) and then
    click on the CommandButton, you will be able to drag it around. I put some
    code in the MouseDown event which is currently commented out. If you
    uncomment that code, the user will not be able to place the CommandButton
    off of the Form.

    Rick - MVP

    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessag eA" _
    (ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) _
    As Long

    Private Declare Function ReleaseCapture Lib "user32" () As Long

    Private Const WM_NCLBUTTONDOW N = &HA1
    Private Const HTCAPTION = 2

    Private Sub Command1_Click( )
    Debug.Print "I still work!"
    End Sub

    Private Sub Command1_MouseD own(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)
    If Shift = vbCtrlMask Then
    If Button = vbLeftButton Then
    Call ReleaseCapture
    With Command1
    Call SendMessage(.hW nd, WM_NCLBUTTONDOW N, _
    HTCAPTION, ByVal 0&)
    ' If .Left < 0 Then .Left = 0
    ' If .Top < 0 Then .Top = 0
    ' If .Left + .Width > Me.ScaleWidth Then
    ' .Left = Me.ScaleWidth - .Width
    ' End If
    ' If .Top + .Height > Me.ScaleHeight Then
    ' .Top = Me.ScaleHeight - .Height
    ' End If
    End With
    End If
    End If
    End Sub


    Comment

    • Steve Gerrard

      #3
      Re: drag and drop


      "dave" <da009a7228@blu eyonder.co.uk> wrote in message
      news:rGPQb.77$v [email protected] er.co.uk...[color=blue]
      > Hi
      >
      > How do i drag and drop a command button using the mouse in runtime?
      >
      > Its driving me mad
      >
      > I am using Visual Basic 6
      >
      > Thanx Dave
      >
      >[/color]

      You could also try manually coded dragging. You can restrict the motion
      to left-right by changing only the .Left property, or to up-down by
      changing only the .Top.

      Option Explicit

      Dim mMoving As Boolean
      Dim mStartX As Single
      Dim mStartY As Single

      Private Sub command1_MouseD own(Button As Integer, Shift As Integer, X As
      Single, Y As Single)
      'control left click to drag
      If Button = vbLeftButton And Shift = vbCtrlMask Then
      mMoving = True
      mStartX = X
      mStartY = Y
      End If
      End Sub

      Private Sub command1_MouseM ove(Button As Integer, Shift As Integer, X As
      Single, Y As Single)
      Dim xNew As Single
      Dim yNew As Single

      If mMoving Then

      With Command1

      xNew = .Left + X - mStartX
      yNew = .Top + Y - mStartY

      If xNew >= 0 And xNew + .Width <= Me.ScaleWidth Then
      .Left = xNew
      End If

      If yNew >= 0 And yNew + .Height <= Me.ScaleHeight Then
      .Top = yNew
      End If

      End With

      End If

      End Sub

      Private Sub command1_MouseU p(Button As Integer, Shift As Integer, X As
      Single, Y As Single)
      mMoving = False
      End Sub



      Comment

      • Larry Serflaten

        #4
        Re: drag and drop

        "Steve Gerrard" <notstevegerrar [email protected]> wrote
        [color=blue]
        > You could also try manually coded dragging.[/color]

        Whoa, I just got an episode of Deja Vu!


        <g>
        LFS





        -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
        http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
        -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

        Comment

        • Steve Gerrard

          #5
          Re: drag and drop


          "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message
          news:40149c2f_5 @corp.newsgroup s.com...[color=blue]
          > "Steve Gerrard" <notstevegerrar [email protected]> wrote
          >[color=green]
          > > You could also try manually coded dragging.[/color]
          >
          > Whoa, I just got an episode of Deja Vu!
          >
          >
          > <g>
          > LFS
          >
          >[/color]

          Cool, I like it when that happens to me <g>.

          Are you referring to my code looking like Rick's code, or that I posted
          in microsoft.publi c.vb.general.di scussion as well, or that my phrasing
          is weird?

          Actually the effect of moving the target in the mouse move event,
          instead of calling ReleaseCapture in a mouse down event, is somewhat
          different. Rick's code doesn't apply conditions to the position of the
          target until the mouse is released, while the mouse move approach
          doesn't move the target at all unless the conditions are met.


          Comment

          Working...