Results 1 to 14 of 14

Thread: Dragging my form

  1. #1

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    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
    Living a Lie, Timmy

  2. #2
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    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!

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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:
    1. 'In your load or wherever appropriate:
    2.         AddHandler PictureBox1.MouseDown, AddressOf Me.Form1_MouseDown
    3.         AddHandler PictureBox1.MouseMove, AddressOf Me.Form1_MouseMove
    4.         AddHandler PictureBox1.MouseUp, AddressOf Me.Form1_MouseUp
    5.  
    6. 'Code for events:
    7.     Private blnMouseDown As Boolean
    8.     Private intOrigX As Int32
    9.     Private intOrigY As Int32
    10.  
    11.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    12.         blnMouseDown = True
    13.         intOrigX = e.X
    14.         intOrigY = e.Y
    15.     End Sub
    16.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    17.         If blnMouseDown Then
    18.             Me.Top = Convert.ToInt32(Me.Top + (e.Y - intOrigY))
    19.             Me.Left = Convert.ToInt32(Me.Left + (e.X - intOrigX))
    20.             Me.Refresh()
    21.         End If
    22.     End Sub
    23.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    24.         blnMouseDown = False
    25.     End Sub

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Dragging my form

    This uses a command button, but changing the control to your image shouldn't be difficult.

  5. #5

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    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
    Living a Lie, Timmy

  6. #6
    Hyperactive Member imadthemad's Avatar
    Join Date
    May 2005
    Posts
    344

    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

  7. #7

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    Re: Dragging my form

    Quote Originally Posted by imadthemad
    wow...ugly codes....(no offence)
    just use this api-Fast and effective

    just read this: http://www.earlsoft.co.uk/hashvb/Dragging_windows
    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

  8. #8
    Hyperactive Member imadthemad's Avatar
    Join Date
    May 2005
    Posts
    344

    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...

  9. #9

    Thread Starter
    Addicted Member sotusotusotu's Avatar
    Join Date
    Aug 2001
    Location
    Plymouth, England
    Posts
    158

    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
    Living a Lie, Timmy

  10. #10
    Hyperactive Member imadthemad's Avatar
    Join Date
    May 2005
    Posts
    344

    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

  11. #11
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  12. #12
    Hyperactive Member imadthemad's Avatar
    Join Date
    May 2005
    Posts
    344

    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....

  13. #13
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  14. #14
    Hyperactive Member imadthemad's Avatar
    Join Date
    May 2005
    Posts
    344

    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
  •  



Click Here to Expand Forum to Full Width