-
Apr 6th, 2006, 12:00 PM
#1
Thread Starter
Frenzied Member
[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:
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub ReleaseCapture Lib "User32" ()
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2
If Button = 1 Then
ReleaseCapture
SendMessage Me.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End If
End Sub
-
Apr 6th, 2006, 12:11 PM
#2
Hyperactive Member
Re: Move/Drag skinned form
Theres no need to use pinvoke, but it translates as:
VB Code:
'Imports System.Runtime.InteropServices !!
<DllImport("user32")> _
Private Shared Function SendMessage( _
ByVal hWnd As IntPtr, ByVal wMsg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
<DllImport("user32")> _
Private Shared Sub ReleaseCapture()
End Sub
Private Const WM_NCLButtonDown As Integer = &HA1
Private Const HTCaption As Integer = 2
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLButtonDown, HTCaption, 0)
End If
End Sub
Using .Net instead...
VB Code:
Public Class Form2
Inherits Form
Private lastPosition As Point
' dismiss with doubleclick
Private Sub Form2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
Me.Close()
Me.Dispose()
End Sub
Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
lastPosition = Me.PointToScreen(e.Location)
End If
End Sub
' drag form around screen
Private Sub Form2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim newlocation As Point = Me.PointToScreen(e.Location)
Dim offsetX As Integer = newlocation.X - lastPosition.X
Dim offsety As Integer = newlocation.Y - lastPosition.Y
Me.Location = New Point(Me.Location.X + offsetX, Me.Location.Y + offsety)
lastPosition = newlocation
End If
End Sub
End Class
-
Apr 6th, 2006, 12:38 PM
#3
Thread Starter
Frenzied Member
Re: Move/Drag skinned form
it errors with 'Location is not a member of system.windows.forms.mouseeventargs'
-
Apr 6th, 2006, 12:47 PM
#4
Hyperactive Member
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)
-
Apr 6th, 2006, 12:59 PM
#5
Thread Starter
Frenzied Member
Re: Move/Drag skinned form
okay i changed and now it says 'keyword is not an identifier' with this line
VB Code:
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
-
Apr 6th, 2006, 01:00 PM
#6
Hyperactive Member
Re: Move/Drag skinned form
VB Code:
'2003 version
Private lastPosition As Point
Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
Me.Close()
Me.Dispose()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
lastPosition = Me.PointToScreen(New Point(e.X, e.Y))
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim newlocation As Point = Me.PointToScreen(New Point(e.X, e.Y))
Dim offsetX As Integer = newlocation.X - lastPosition.X
Dim offsety As Integer = newlocation.Y - lastPosition.Y
Me.Location = New Point(Me.Location.X + offsetX, Me.Location.Y + offsety)
lastPosition = newlocation
End If
End Sub
And the example using the apis, use mybase instead of me for the event.
-
Apr 6th, 2006, 01:04 PM
#7
Thread Starter
Frenzied Member
Re: Move/Drag skinned form
thx works well
-
Apr 6th, 2006, 06:57 PM
#8
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|