|
-
Feb 28th, 2006, 11:27 AM
#1
Thread Starter
Addicted Member
Dragging my form
Hey,
I have a border-less form that is just a jpeg image. My form is 700*500 pixels. I really need to beable to drag the form but i just want to drag the top part, 60 pixels or so.
How would I do this?
would I need to cut up that part of the image and drag on that bit?
thanks
-
Feb 28th, 2006, 11:56 AM
#2
Frenzied Member
Re: Dragging my form
I dont get what you mean, do you mean resize the form?
Surley if you dragged just the top part of a form it would become out of shape and twisted!
-
Feb 28th, 2006, 12:00 PM
#3
Re: Dragging my form
What do you mean "Just the top part?" I'm not quite following you. You can set the picturebox to be dragged as well without exposing 60 pixels of the form; if that's what you're after?
VB Code:
'In your load or wherever appropriate:
AddHandler PictureBox1.MouseDown, AddressOf Me.Form1_MouseDown
AddHandler PictureBox1.MouseMove, AddressOf Me.Form1_MouseMove
AddHandler PictureBox1.MouseUp, AddressOf Me.Form1_MouseUp
'Code for events:
Private blnMouseDown As Boolean
Private intOrigX As Int32
Private intOrigY As Int32
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
blnMouseDown = True
intOrigX = e.X
intOrigY = e.Y
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If blnMouseDown Then
Me.Top = Convert.ToInt32(Me.Top + (e.Y - intOrigY))
Me.Left = Convert.ToInt32(Me.Left + (e.X - intOrigX))
Me.Refresh()
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
blnMouseDown = False
End Sub
-
Feb 28th, 2006, 12:00 PM
#4
Re: Dragging my form
This uses a command button, but changing the control to your image shouldn't be difficult.
-
Feb 28th, 2006, 12:29 PM
#5
Thread Starter
Addicted Member
Re: Dragging my form
Cheers for all the replies but i dont know which one to answer, lol
What I mean top of the form is that i just want to drag the title bit (the border).
I have tried the 2 code examples but havent managed them to work. I went on the website link about dragging borderless forms using a cmd button but when i created the cmdTest button and put in the code i get errors?:
Code:
Private mouse_offset As PointPrivate
Sub cmdTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmdTest.MouseDown
mouse_offset = New Point(-e.X, -e.Y)
End SubPrivate
Sub cmdTest_MouseMove(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmdTest.MouseDown
If e.Button = MouseButtons.Left Then
Dim mousePos As Point = Sender.findform().MousePosition
mousePos.Offset(mouse_offset.X, mouse_offset.Y)
Sender.findform().Location = mousePos
End If
End Sub
-
Feb 28th, 2006, 12:47 PM
#6
Hyperactive Member
Re: Dragging my form
wow...ugly codes....(no offence)
just use this api-Fast and effective
just read this: http://www.earlsoft.co.uk/hashvb/Dragging_windows
-
Feb 28th, 2006, 01:32 PM
#7
Thread Starter
Addicted Member
Re: Dragging my form
 Originally Posted by imadthemad
yeah thats what i thought, didn't really understand it and didnt work anyway
I don't understand that code though on that website. Where abouts do i input it? Are you sure it works with borderless forms?
Last edited by sotusotusotu; Feb 28th, 2006 at 01:37 PM.
Living a Lie, Timmy
-
Feb 28th, 2006, 01:49 PM
#8
Hyperactive Member
Re: Dragging my form
yes i have used it before
put this code in the declarations on top:
Declare Function ReleaseCapture Lib "user32" () As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2
under the form1_mousedown event\sub, put:
ReleaseCapture
SendMessage Form.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
what is cool about this is that you can use it on any control to move the whole window
i used a borderless form with a label as background, i put the code on the label and it worked. when you drag the label the whole form is dragged. Piece of cake...
-
Feb 28th, 2006, 01:50 PM
#9
Thread Starter
Addicted Member
Re: Dragging my form
cheers for the help guys!
Managed to get it working using this code:
Code:
#Region "Picture Box Mouse Moxe"
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
down = False
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
Me.Top = Me.Top + e.Y - t
Me.Left = Me.Left + e.X - w
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
down = True
w = e.X
t = e.Y
End Sub
#End Region
-
Feb 28th, 2006, 01:53 PM
#10
Hyperactive Member
Re: Dragging my form
like i said man...really length code and i bet it jams abit on runtime too...
you could've just used this:
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
ReleaseCapture
SendMessage Form.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
End Sub
-
Feb 28th, 2006, 01:53 PM
#11
Re: Dragging my form
That's vb6 code, and also... It's unmanaged.
The delegate example I posted earlier has the same effect, except in a managed state.
Still don't know what you're after though.
-
Feb 28th, 2006, 02:02 PM
#12
Hyperactive Member
Re: Dragging my form
that is not vb6 code...that is api...just use int32 instead of long and you are set...
like some1 just said to me in a thread i recently posted, managed classes are made of well written api, but sometimes the api we write is not that bad either.
in this case the differences are obvious at runtime. you will see the code that he decided to use will cause some jamming...
i mean the boolean he made to catch the mousedown event is just extra code because there is fast api releasecapture that just does it all....
-
Feb 28th, 2006, 02:09 PM
#13
Re: Dragging my form
Any is object, Form doesn't have an "hWnd" property, there was a couple things.
The difference is compatibility between future versions. If you hook APIs (which is good in certain situations), unamanged code means MS is not promising that's always going to work. In future versions of windows, the signature it might require 5 parameters instead of 4. If you use the framework as median, the framework can be updated and your code will continue as normal. Where as if you hook the API, it will through an error.
It's really up to you how you do it. Like I said, APIs are great for certain situations, but they're not something you should proactively be trying to use.
-
Feb 28th, 2006, 02:16 PM
#14
Hyperactive Member
Re: Dragging my form
works on vb and vb.net...
i use vb.net not vb but i took the code from an api site....
you are right that you shouldnt ALWAYS use api but in this case i have tried both codes and api is faster.
the unmanaged and managed code things just means that for api they are effective only 99.9% of the time or something like that so i think in the end api is better for window dragging because it is faster even if it is unmanaged code...
also you can find, even in vb.net where you have a whole class based on processes and stuff, that some api like shellexecute are faster than vb.net equivelants like process.start.
and it is easier to get the handle of a window by api instead of using processes then the mainwindowhandle property...
those are just few examples....other than that managed code is better like you said
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
|