borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (TreeView+ListView control)Next Topic (Problem Z-Ording the Winow [ SetWindowPos() ]) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Link Forms together
Poster Message
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54

icon Link Forms together

How can I link two forms together.  Inother words, drag one form, the 2nd one follows in its connected location.  Also, where the 2nd form can still be torn off the first and moved freely?

21-02-2003 at 11:49 PM
View Profile Send Email to User Show All Posts | Quote Reply
SteveG
Level: Sage


Registered: 15-04-2002
Posts: 56
icon Re: Link Forms together

Don't know whether this is of help. In the form properties there is the facility to define a start-up location for the form. One of the options is Center Owner. If you set this for the second form then it will be posioned in the centre of the first form (assuming the first form loads the second) and will then be moved with the first form whilst retaining its ability to be moved independently.

22-02-2003 at 09:29 AM
View Profile Send Email to User Show All Posts | Quote Reply
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54
icon Re: Link Forms together

Thanks, but that doesn't really accomplish what I'm trying to do.  I want to create a form that sticks to the side of another form, but can be pulled off at any time.  I've tried playing with the second forms position parameters, however I can't seem to get them to change when I make a move on the first form by grabbing it's title bar.  I'd like the two forms to act as one until pulled appart.

Thanks again for your input.  

24-02-2003 at 04:14 PM
View Profile Send Email to User Show All Posts | Quote Reply
vbgen
Level: Moderator

Registered: 10-10-2002
Posts: 876
icon Re: Link Forms together

right now i can only give you a hint or tip...

play around with the x and y coordinates of the forms..

when form1's x moves, then set the form2 to move from its own x to the new x, specified by whatever amount you may have calculated as the position parameters..



____________________________
Been busy trying to take a second degree <--it's not working out...

24-02-2003 at 05:30 PM
View Profile Send Email to User Show All Posts | Quote Reply
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54
icon Re: Link Forms together

Thanks, but that's exactly what I've been trying to do.  I can move form2 based on form1's position.  The problem is that I can't seem to make it happen dynamically.  In other words, move form2 WHILE form1 is moving.  Am I missing something simple?  I have no problem moving form2 to anywhere I want.  The problem is making the two forms move concurrently.

24-02-2003 at 08:43 PM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: Link Forms together

the problem is that VB6 does not handle the on_moving event...so let's do it with subclassing

Insert this code in a Module:
Option Explicit

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long, ByVal dwnewlong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, lParam As RECT) As Long
Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Type WndProc
    oldWndProc As Long
    MainHWnd As Long
End Type

Const WM_ACTIVATE = &H6
Const WM_ACTIVATEAPP = &H1C
Const WM_CLOSE = &H10
Const WM_CREATE = &H1
Const WM_DESTROY = &H2
Const WM_ENABLE = &HA
Const WM_HOTKEY = &H312
Const WM_HSCROLL = &H114
Const WM_KEYDOWN = &H100
Const WM_KEYUP = &H101
Const WM_MOUSEMOVE = &H200
Const WM_MOUSEACTIVATE = &H21
Const WM_MOVE = &H3
Const WM_MOVING = 534
Const GWL_WNDPROC = (-4)

Dim data(0 To 100) As WndProc

Private Property Let OldhWndProc(hWnd As Long, NewValue As Long)
    Dim i As Integer
    
    For i = 0 To 100
        If data(i).MainHWnd = hWnd Then
            data(i).oldWndProc = NewValue
            Exit Property
        End If
    Next
    For i = 0 To 100
        If data(i).MainHWnd = 0 Then
            data(i).MainHWnd = hWnd
            data(i).oldWndProc = NewValue
            Exit Property
        End If
    Next
End Property

Private Property Get OldhWndProc(hWnd As Long) As Long
    Dim i As Integer
    
    For i = 0 To 100
        If data(i).MainHWnd = hWnd Then
            OldhWndProc = data(i).oldWndProc
            Exit Property
        End If
    Next
End Property

Private Sub OnMove(hWnd As Long, x As RECT)
    'enable onmoving event
    Form2.Move (150 + x.Left) * Screen.TwipsPerPixelX, (200 + x.Top) * Screen.TwipsPerPixelY
End Sub

Private Function MyWindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, lParam As RECT) As Long
    If uMsg = WM_MOVING Then
        OnMove hWnd, lParam
    End If
    MyWindowProc = CallWindowProc(OldhWndProc(hWnd), hWnd, uMsg, wParam, lParam)
End Function

Public Sub Install_OnMove(frm As Form)
    OldhWndProc(frm.hWnd) = SetWindowLong(frm.hWnd, GWL_WNDPROC, AddressOf MyWindowProc)
End Sub

Public Sub Remove_OnMove(frm As Form)
    Dim i As Long
    
    For i = 0 To 100
        If data(i).MainHWnd = frm.hWnd Then
            data(i).MainHWnd = 0
            SetWindowLong frm.hWnd, GWL_WNDPROC, data(i).oldWndProc
            Exit Sub
        End If
    Next
End Sub



Then create two forms (Form1 and Form2)
and add this code inside Form1

Option Explicit

Private Sub Form_Load()
    Form2.Show
    Install_OnMove Form1
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Remove_OnMove Form1
End Sub


Moving form1 will cause form2 to move according to the offset used in the On_Move event

____________________________
AndreaVB

25-02-2003 at 07:17 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54
icon Re: Link Forms together

Looks promissing.  Thanks, I'll give it a try.

27-02-2003 at 09:20 PM
View Profile Send Email to User Show All Posts | Quote Reply
MikeG
Level: Sage

Registered: 21-02-2003
Posts: 54
icon Re: Link Forms together

Works great!  Thanks again.

03-03-2003 at 10:13 PM
View Profile Send Email to User Show All Posts | Quote Reply
twegler
Level: Trainee

Registered: 07-09-2005
Posts: 2
icon Re: Link Forms together

i am trying to do the same thing but on mine when the user clicks on any of the 1st three buttons on form1 i want it to bring up form2 but i cant seem to figure out how to do it. any help would be very grateful.

07-09-2005 at 11:45 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: Link Forms together

Hi twegler,
Create form1
Create form2
on form1 place a command button, call it "cmd1"
dbl-click on it and add the following code:

private sub cmd1_click()
dim frm as form2
set frm=new form2
frm.show vbmodal
unload frm
set frm=nothing
end sub


this will show form2 when the user clicks a button
Hope this helps, but if it doesn't, I suggest you post a new topic on the exact problem.
regards
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

24-09-2005 at 02:04 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Link Forms together
Previous Topic (TreeView+ListView control)Next Topic (Problem Z-Ording the Winow [ SetWindowPos() ]) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder