I am subclassing a form in my project for certain reasons. I now have the need to give the ability to unsubclass it. Mostly for reducing used resources.
But in the WindowProc I have it unsubclassing upon the WM_DESTROY message. But I need to simulate what its doing in there but upon a button click and not the ending of the program.
vb Code:
Case WM_DESTROY
'REMOVE SUBCLASSING WHEN FORM IS DESTROYED (FORM1 UNLOADED)
Thans why I was wondering if there was something I was missing. Still trying to get over this damn cold and its not letting me think clearly. Today is the first day I have written any real code in almost a week.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Arrrg! I had it on a menu click and toggling a checkmark. Turned out that I had the call backwards. Trying to subclass when subclassing was already in place.
I switched it and now it works. So I guess all that is needed is the SetWindowLong call to unsub it.
One more issue BM as I know you are good at APIs,...
What message would I need to trap to determine the windowstate changing? I am currently using the WM_SYSCOMMAND message and checking the wParam for the SC_MINIMIZE, SC_MAXIMIZE or SC_RESTORE. Currently it works but the restore doesnt do all I need. When the Form1 is restored from min/max I need to have a second form move too.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Here is the meat of my windproc message that are being handled. The second form is supossed to move to the first forms edge upon the restore but it doesnt. Only when I resize of move the first form will it move. If I add code to resize form2 in the restore case then it sizes to the form1 Max'ed state.
the WM_SYSCOMMAND is processed before form is resized.
possible workaround (no guarantees it's the best). add a module level boolean, and catch the WM_WINDOWPOSCHANGED message:
Code:
Select Case Msg
Case WM_SYSCOMMAND
Select Case wParam
Case SC_MAXIMIZE
'Debug.Print "Maximize"
Case SC_MINIMIZE
'Debug.Print "Minimize"
Case SC_RESTORE
bBool = True
End Select
Case WM_WINDOWPOSCHANGED
If bBool Then
' Position Form2
bBool = False
End If
End Select
I seen that message too but I think it would conflict with WM_MOVING or are the two unrelated? Whats the dif between moving and positionchecged? Sounds like they both do the same thing?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
only WM_WINDOWPOSCHANGED will occur when the form is restored / min / max.
both WM_MOVING and WM_WINDOWPOSCHANGED will occur if the form is moved - however, since we're only using WM_WINDOWPOSCHANGED when the bBool flag is set, Form2 won't be positioned twice
The remarks about the wm_positionchanged message states ...
The WM_SIZE and WM_MOVE messages are not sent if an application handles the WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient to perform any move or size change processing during the WM_WINDOWPOSCHANGED message without calling DefWindowProc.
I don't see why that would be a problem - we don't care about the WM_SIZE or WM_MOVE messages since we're not looking for them, and we're only positioning Form2, not form1
Code:
Dim wPos As WINDOWPOS
Select Case Msg
Case WM_SYSCOMMAND
Select Case wParam
Case SC_MAXIMIZE
Form2.WindowState = vbMaximized
Case SC_MINIMIZE
Form2.WindowState = vbMinimized
Case SC_RESTORE
' set windowstate here
Form2.WindowState = vbNormal
bBool = True
End Select
Case WM_WINDOWPOSCHANGED
If bBool Then
' or here (depending on what you're wanting)
' Form2.WindowState = vbNormal
CopyMemory wPos, ByVal lParam, Len(wPos)
Form2.Move (wPos.x + wPos.cx) * Screen.TwipsPerPixelX, wPos.y * Screen.TwipsPerPixelY
bBool = False
End If
End Select
Its the same action as with the wm_sizing and wm_move.
The problem is that the second form does not re-align next to the first form.
Test: both forms are displayed next to each other (stick). Then you drag form2 away from form1, minimize or maximize, restore (form1) and when restored form2 does not re-stick to form1. Its still restores to its original position. If I slightly move form1 then it snaps and sticks to form1. Can I send a fake move message to duplicate the manual fix?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
this is gonna be easier if we process everything through one message - WM_WINDOWPOSCHANGING - it also eliminates the slight gap that would appear if you moved the two forms around.
i've done that in the attached, couple of things for you to iron out - a) Form2 doesn't snap when first loaded, b) if you Max to Min then restore Form2 goes to Normal, Form1 goes to Max. Those will both be simple to sort.
note: it might be worth your wild checking out Karl Peterson's FormPair example
edit: the buttons on Form1 don't actually do anything
When I check the menu item to subclass it doesnt snap the other form to the main form. Still have to move the form in order to get the other form to align correctly.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Hmm, was trying to make it as universal as possible without much code in the forms. If I was to send a WM_MOVE or a SC_RESTORE message that should do the trick but then still having to send anything would mean code in the main form and not the module.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.