Click to See Complete Forum and Search --> : prevent window movement in MDI
texasdiablo
Jan 10th, 2002, 06:08 PM
I have a simple MDI application with a few children windows.
That being said, how can I change the children windows in such a way that they are not moveable.
I know I can do this at design time, but I want them initially to be moveable, and allow the user to "lock" and "unlock" the windows while the program is running...to keep windows from being moved accidentally.
Any ideas?
The Hobo
Jan 10th, 2002, 09:35 PM
Set the Movable property of the child forms to False.
texasdiablo
Jan 10th, 2002, 09:42 PM
HELLO!
You can't set that property at run time.
MerrionComputin
Jan 11th, 2002, 03:22 AM
You have two options, both of which involve subclassing.
Either:
1 Intercept the WM_SIZING/WM_MOVING messages and set the parameters of the RECT structure pointed to by lParam to the location you want your form to be stuck at.
2 Intercept the WM_NCHITTEST and always return HTCLIENT thus fooling Windows into thinking your form has no caption or borders that can be selected.
The good news is that both of these are fairly straight forward if you use the EventVB.dll (http://www.merrioncomputing.com/Download/EventVB.dll) thus:
'\\ Form declarations
Dim WithEvents vbLink As EventVB.ApiFunctions
Dim WithEvents vbWnd As EventVB.ApiWindow
Dim bLocked As Boolean
'\\ Start subclassing when form loads
Private Sub Form_Load()
Set vbLink = New EventVB.ApiFunctions
Set vbWnd = New EventVB.ApiWindow
vbWnd.hwnd = Me.hWnd
vbLink.SubclassedWindows.Add vbWnd
End Sub
'\\ Stop subclassing when form unloads
Private Sub Form_Unload()
vbLink.SubclassedWindows.Remove vbWnd
Set vbWnd = Nothing
Set vbLink = Nothing
End Sub
'\\ Option 1 - prevent the WM_SIZING/WM_MOVING having any effect
Private Sub vbWnd_Sizing(ByVal SizeEdges As EventVB.WindowSizingEdges, DragRectangle As EventVB.APIRect)
If blocked Then
With DragRectangle
.Left = vbWnd.RECT.Left
.Right = vbWnd.RECT.Right
.Top = vbWnd.RECT.Top
.Bottom = vbWnd.RECT.Bottom
End With
End If
End Sub
Private Sub vbWnd_Moving(ByVal MoveEdges As EventVB.WindowSizingEdges, MoveRectangle As EventVB.APIRect)
If blocked Then
With MoveRectangle
.Left = vbWnd.RECT.Left
.Right = vbWnd.RECT.Right
.Top = vbWnd.RECT.Top
.Bottom = vbWnd.RECT.Bottom
End With
End If
End Sub
'\\ Option 2 - fool the hittest
Private Sub vbWnd_HitTest(ByVal x As Long, ByVal y As Long, ReturnValue As EventVB.enHitTestResult, Override As Boolean)
If bLocked Then
ReturnValue = HTCLIENT
Override = True
End If
End Sub
Hope this helps,
Duncan
Oafo
Jan 11th, 2002, 03:46 PM
First of all, don't get an attitude for no reason. Second, put this in a module..
Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Public Const MF_APPEND = &H100&
Public Const MF_BYPOSITION = &H400&
Function bMenu(hWnd As Long, Position As Long, Enabled As Boolean)
Dim hSysMenu As Long
If Enabled = True Then
hSysMenu = GetSystemMenu(hWnd&, MF_APPEND)
DrawMenuBar hSysMenu&
Else
hSysMenu = GetSystemMenu(hWnd&, 0&)
RemoveMenu hSysMenu&, Position, MF_BYPOSITION
End If
End Function
To stop your MDIForm from moving, use this code..
bMenu MDIForm1.hWnd, 1, False
And to make it moveable again...
bMenu MDIForm1.hWnd, 1, True
texasdiablo
Jan 11th, 2002, 07:26 PM
I was hoping to prevent movement of the CHILDREN within the MDI. I tried this code with the hWnd of a child window, and it was still able to be moved...
any ideas?
Oafo
Jan 11th, 2002, 10:38 PM
hmm, it worked for me. Lets say you wanted to make the Form called "frmChild" unmoveable, then your code would look like this in frmChild's code..
Private Sub Form_Load
bMenu Me.hWnd, 1, False
End Sub
Using the function I gave you earlier of course.
Oafo
Jan 11th, 2002, 10:41 PM
Here's an example project if you couldn't get it to work still.
texasdiablo
Jan 12th, 2002, 01:07 AM
I don't know whats goin on here.
Your version works perfectly, but when I try the EXACT implementation on my MDI array, it doesn't prevent the window from moving! What could possibly be the problem?
texasdiablo
Jan 12th, 2002, 01:09 AM
It will work for the PARENT form (The MDIForm), but none of the children. Could it be because they are Fixed Single (type 1) child windows?
texasdiablo
Jan 12th, 2002, 01:12 AM
Sorry for all the posts in a row.
I did some testing, and that code only works for "Sizeable Windows" and fails to prevent movement of "Fixed Single" windows!
Any more ideas short of making them sizeable?
Oafo
Jan 12th, 2002, 11:38 AM
Can you post up your project? I'll be able to tell what's wrong if you do that.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.