Results 1 to 8 of 8

Thread: [RESOLVED] Move/Drag skinned form

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Resolved [RESOLVED] Move/Drag skinned form

    I've got a skinned for with no standard title bar, so I want to be able to drag the label which is the custom titlebar and it will move the form just like the standard title bar does, here it is in vb6

    VB Code:
    1. Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, _
    2.  ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3.  
    4. Private Declare Sub ReleaseCapture Lib "User32" ()
    5.  
    6. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    7.     Const WM_NCLBUTTONDOWN = &HA1
    8.     Const HTCAPTION = 2
    9.     If Button = 1 Then
    10.         ReleaseCapture
    11.         SendMessage Me.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
    12.     End If
    13. End Sub
    Chris

  2. #2
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Move/Drag skinned form

    Theres no need to use pinvoke, but it translates as:

    VB Code:
    1. 'Imports System.Runtime.InteropServices !!
    2.     <DllImport("user32")> _
    3.         Private Shared Function SendMessage( _
    4.         ByVal hWnd As IntPtr, ByVal wMsg As Integer, _
    5.         ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    6.     End Function
    7.  
    8.     <DllImport("user32")> _
    9.     Private Shared Sub ReleaseCapture()
    10.     End Sub
    11.  
    12.     Private Const WM_NCLButtonDown As Integer = &HA1
    13.     Private Const HTCaption As Integer = 2
    14.  
    15.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    16.         If e.Button = Windows.Forms.MouseButtons.Left Then
    17.             ReleaseCapture()
    18.             SendMessage(Me.Handle, WM_NCLButtonDown, HTCaption, 0)
    19.         End If
    20.     End Sub

    Using .Net instead...
    VB Code:
    1. Public Class Form2
    2.     Inherits Form
    3.  
    4.     Private lastPosition As Point
    5.  
    6.     ' dismiss with doubleclick
    7.     Private Sub Form2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
    8.         Me.Close()
    9.         Me.Dispose()
    10.     End Sub
    11.  
    12.     Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    13.         If e.Button = Windows.Forms.MouseButtons.Left Then
    14.             lastPosition = Me.PointToScreen(e.Location)
    15.         End If
    16.     End Sub
    17.  
    18.     ' drag form around screen
    19.     Private Sub Form2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    20.         If e.Button = Windows.Forms.MouseButtons.Left Then
    21.             Dim newlocation As Point = Me.PointToScreen(e.Location)
    22.             Dim offsetX As Integer = newlocation.X - lastPosition.X
    23.             Dim offsety As Integer = newlocation.Y - lastPosition.Y
    24.             Me.Location = New Point(Me.Location.X + offsetX, Me.Location.Y + offsety)
    25.             lastPosition = newlocation
    26.         End If
    27.     End Sub
    28.  
    29. End Class

  3. #3

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Move/Drag skinned form

    it errors with 'Location is not a member of system.windows.forms.mouseeventargs'
    Chris

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Move/Drag skinned form

    Ah, thats vb2005. For 2003, handle the correctly named events and replace e.location with:

    New Point(e.X, e.Y)

  5. #5

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Move/Drag skinned form

    okay i changed and now it says 'keyword is not an identifier' with this line

    VB Code:
    1. Private Sub Form2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles [B]Me[/B].MouseMove
    it highlights the Me word
    Chris

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Move/Drag skinned form

    VB Code:
    1. '2003 version
    2.     Private lastPosition As Point
    3.  
    4.     Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
    5.         Me.Close()
    6.         Me.Dispose()
    7.     End Sub
    8.  
    9.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    10.         If e.Button = Windows.Forms.MouseButtons.Left Then
    11.             lastPosition = Me.PointToScreen(New Point(e.X, e.Y))
    12.         End If
    13.     End Sub
    14.  
    15.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    16.         If e.Button = Windows.Forms.MouseButtons.Left Then
    17.             Dim newlocation As Point = Me.PointToScreen(New Point(e.X, e.Y))
    18.             Dim offsetX As Integer = newlocation.X - lastPosition.X
    19.             Dim offsety As Integer = newlocation.Y - lastPosition.Y
    20.             Me.Location = New Point(Me.Location.X + offsetX, Me.Location.Y + offsety)
    21.             lastPosition = newlocation
    22.         End If
    23.     End Sub

    And the example using the apis, use mybase instead of me for the event.

  7. #7

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Move/Drag skinned form

    thx works well
    Chris

  8. #8
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: [RESOLVED] Move/Drag skinned form

    that is a very good piece of code

    thank u

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width