Bouger une form par un picturebox ?

Résolu
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 31 oct. 2010 à 11:04
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 6 déc. 2010 à 15:13
Bonjour,

comment on fait pour bouger une form en vb8 par un picturebox ?

Dans vb6 j'utilisai les API, j'voudrais savoir de quoi est capable le
System.Windows.Forms.MouseEventArgs ?


    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown        
        If e.Button = Forms.MouseButtons.Left Then
            '??? bouge Form1.vb vers e.x et e.y ???
        End If
    End Sub

15 réponses

seb4stien13 Messages postés 31 Date d'inscription lundi 17 novembre 2008 Statut Membre Dernière intervention 11 septembre 2011
3 déc. 2010 à 14:07
Sinon comme cela
Public Class Form1
    Dim NewPoint As New System.Drawing.Point()
    Dim X, Y As Integer
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        X = Control.MousePosition.X - Me.Location.X
        Y = Control.MousePosition.Y - Me.Location.Y
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
            NewPoint = Control.MousePosition
            NewPoint.X -= (X)
            NewPoint.Y -= (Y)
            Me.Location = NewPoint
        End If

    End Sub
End Class
3
cs_breton51 Messages postés 78 Date d'inscription jeudi 21 avril 2005 Statut Membre Dernière intervention 15 novembre 2018
6 déc. 2010 à 15:05
autre possibilité avec les api

    Public Const HTCAPTION As Integer = 2
    Public Const WM_NCLBUTTONDOWN As Integer = 161

    <DllImport("user32.dll", EntryPoint:="SendMessageA")> _
    Public Shared Function SendMessage(ByVal hwnd As Integer, _
                                       ByVal wMsg As Integer, _
                                       ByVal wParam As Integer, _
                                       ByRef lParam As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Public Shared Function ReleaseCapture() As Integer
    End Function

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left And Me.WindowState <> FormWindowState.Maximized Then
            ReleaseCapture()
            SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
        End If
    End Sub


cette façon de faire a l'avantage d'utiliser les mécanismes de windows et donc si l'affichage du contenu des fenêtres pendant leur déplacement est désactivé le rafraichissement de la fenêtre ne se fera qu'un fois la fenêtre repositionner et non tout au long du déplacement de la fenêtre.
3
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 11:34
MARCHE PAS !

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim a As PictureBox = PictureBox1
        If e.Button Forms.MouseButtons.Left And a.MouseButtons.Left Forms.MouseButtons.Left Then
            Me.Left = e.X
            Me.Top = e.Y
        End If
        Me.Dispose()
    End Sub
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 11:38
MARCHE PAS !

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim a As MouseEventArgs = PictureBox1.MouseDown
        If e.Button Forms.MouseButtons.Left And a.Button Forms.MouseButtons.Left Then
            Me.Left = e.X
            Me.Top = e.Y
        End If
        Me.Dispose()
    End Sub
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 11:42
GRAPHIQUEMENT EXTRA NAZ !

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button Forms.MouseButtons.Left And bDOWN True Then
            Me.Left = e.X
            Me.Top = e.Y
        End If
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        bDOWN = True
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        bDOWN = False
    End Sub
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 11:50
LA FORM ALHEIMER !

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button Forms.MouseButtons.Left And bDOWN True Then
            Me.Left = Me.Left + (e.X - Me.Left)
            Me.Top = Me.Top + (e.Y - Me.Top)
        End If
    End Sub
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 12:12
LA FORM X0 Y0 !

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Dim b As System.Drawing.Point
        Dim a As System.Drawing.Rectangle
        Dim cH, cW As Integer

        a = Screen.PrimaryScreen.WorkingArea
        cH = Me.Size.Height - Me.Left
        cW = Me.Size.Width - Me.Top
        b.X = e.X
        b.Y = e.Y
        b.X = cH + b.X
        b.Y = cW + b.Y

        If bDOWN = True Then
            Me.Top = a.Y
            Me.Left = a.X
            bDOWN = False
        End If
    End Sub
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 12:20
LA FORM YOYO !

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Dim b As System.Drawing.Point
        Dim a As System.Drawing.Rectangle
        Dim cH, cW As Integer

        a = Screen.PrimaryScreen.WorkingArea
        cH = a.Height - Me.Size.Height
        cW = a.Width - Me.Size.Width
        b.X = e.X
        b.Y = e.Y

        If bDOWN = True Then
            Me.Top = (cH - Me.Top) + b.Y
            Me.Left = cW - Me.Left
            bDOWN = False
        End If
    End Sub
0
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
31 oct. 2010 à 12:51
Salut

La bonne méthode, c'est ce que tu as appelé "GRAPHIQUEMENT EXTRA NAZ"
Le e.X et e.Y représente la position de la souris (je suppose)
Quand tu veux déplacer ta forme, si tu places le coin haut gauche de ta forme sur le e.X et e.Y, tu vas la décaler.
En fait, il faut que :
- MouseDown : tu mémorises la différence entre le Left et e.X, et Top et le e.Y = position de ta forme par rapport aux x et y du 'e'
- MouseMove : tu places ta forme par rapport aux x et y du 'e', auxquels tu retrancheras les calculs faits dans MouseDown.

Bien sûr, il faut garder ta variable bDown.

Vala
Jack, MVP VB
NB : Je ne répondrai pas aux messages privés

Le savoir est la seule matière qui s'accroit quand on la partage (Socrate)
0
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
31 oct. 2010 à 12:52
Voir aussi les API avec SetCapture et ReleaseCapture
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
31 oct. 2010 à 15:54
MERCI JACK ^^

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        mod_form.OBJ_MouseMove(MAIN_Accueil.ActiveForm, e)
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        mod_form.OBJ_MouseDown(e)
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        mod_form.OBJ_MouseUp(e)
    End Sub


Module mod_form
    Private iX, iY As Integer
    Private bDOWN As Boolean = False

    Public Sub OBJ_MouseMove(ByVal FormToMove As Form, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Forms.MouseButtons.Left Then
            FormToMove.Left = e.X - iX + FormToMove.Left
            FormToMove.Top = e.Y - iY + FormToMove.Top
        End If
    End Sub

    Public Sub OBJ_MouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        bDOWN = True
        iY = e.Y
        iX = e.X
    End Sub

    Public Sub OBJ_MouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        bDOWN = False
    End Sub
End Module
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
3 déc. 2010 à 18:42
je suis sur le point de tester ton code seb, mais il me reconnais pas ME.
Il faut importer ou implementer quoi ?
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
3 déc. 2010 à 18:48
laisse tomber, c'est d'ma faute ^^
WAOUHOUUU !!!
C'est terriblement fluide et efficace !
MERCI SEB, +1
0
seb4stien13 Messages postés 31 Date d'inscription lundi 17 novembre 2008 Statut Membre Dernière intervention 11 septembre 2011
4 déc. 2010 à 01:04
De rien le plaisir est pour moi^^
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
6 déc. 2010 à 15:13
Mon ancienne méthode en .NET; merci seb4stien13 !!!
0
Rejoignez-nous