Google Группы больше не поддерживают новые публикации и подписки в сети Usenet. Опубликованный ранее контент останется доступен.
Закрыть

Event -- Move Form

1 просмотр
Перейти к первому непрочитанному сообщению

Dan Myhre

не прочитано,
12 янв. 2002 г., 17:55:1712.01.2002
Does anyone know what event triggers when the user clicks and drags a form
across the screen? I need this event to make two other forms move at the
same time.

Thanks!


alpine

не прочитано,
12 янв. 2002 г., 20:40:4912.01.2002
On Sat, 12 Jan 2002 16:55:17 -0600, "Dan Myhre" <danm...@uwm.edu>
wrote:


There isn't a VB event that fires when a form is moved. You will need
to subclass the form to achieve what you want. Have a look at the
Subclassing demos at http://www.mvps.org/vbvision/ for an example.

HTH,
Bryan
____________________________________________________________
New Vision Software "When the going gets weird,"
Bryan Stafford "the weird turn pro."
alp...@mvps.org Hunter S. Thompson -
Microsoft MVP-Visual Basic Fear and Loathing in LasVegas

MikeD

не прочитано,
12 янв. 2002 г., 20:49:0012.01.2002
There is no such event. I'd recommend subclassing and processing the
WM_MOVING message. If you're familiar with subclassing, you shouldn't need
any further information. If you're not familiar with subclassing, read on.

While this is hardly the most elegant of code (I just threw it together real
quick, but I did do some limited testing), it shows the general idea. Start
a new standard EXE project. Add 2 additional forms (so you have Form1,
Form2, and Form3) and a standard code module (a .bas file) to it. Move and
size the 3 forms so they don't overlap one another (size is more important
since the code will reposition Form2 and Form3).

In the code module, paste the following:

-----BEGIN CODE
Option Explicit

Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_MOVING As Long = &H216

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

Private mbIsHooked As Boolean
Private mlOldWindowProc As Long

Public Function WindProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

'This is the replacement window procedure in which we'll process the
WM_MOVING message.

If uMsg = WM_MOVING Then
With Form1
Form2.Move .Left, .Top + .Height + 200
Form3.Move .Left + .Width + 200, .Top
End With
End If

'Call the default window procedure
WindProc = CallWindowProc(mlOldWindowProc, hWnd, uMsg, wParam, lParam)

End Function

Public Function Hook(hWnd As Long) As Boolean

'This function enables subclassing of the window identified by the hWnd
parameter

'Assign a default return value of True. This is to prevent the
'function from returning False (indicating a problem) if the form is
'already subclassed.
Hook = True

'If already subclassed exit the function
If mbIsHooked Then Exit Function

'Get the address for the previous window procedure
mlOldWindowProc = GetWindowLong(hWnd, GWL_WNDPROC)
If mlOldWindowProc = 0 Then
Hook = False
Exit Function
End If

If SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindProc) = 0 Then
Hook = False
Exit Function
End If

mbIsHooked = True

End Function

Public Function Unhook(hWnd As Long) As Boolean

'This function disables subclassing of the window identified by the hWnd
parameter

Unhook = True

'If not subclassed, exit
If mbIsHooked = False Then Exit Function

'Restore default window procedure
If SetWindowLong(hWnd, GWL_WNDPROC, mlOldWindowProc) = 0 Then
Unhook = False
Exit Function
End If

mbIsHooked = False

End Function

-----------END CODE

Paste the following code into Form1's General Declarations section:

--------BEGIN CODE
Option Explicit

Private Sub Form_Load()

Form2.Show
Form3.Show

With Form1
Form2.Move .Left, .Top + .Height + 200
Form3.Move .Left + .Width + 200, .Top
End With

Hook Me.hWnd

End Sub

Private Sub Form_Unload(Cancel As Integer)

Unhook Me.hWnd
Unload Form2
Unload Form3

End Sub
---------------END CODE

SAVE THE PROJECT and then run it. Form2 and Form3 will move right along
with Form1, keeping a relative position to Form1 (Form2 will be left-aligned
with Form1 while Form3 will be top-aligned with Form1).

You need to be very careful when subclassing. It's very easy to cause the
app (or VB) to crash. Don't enter into break mode, and NEVER end your app
with the End statement or by VB's End menu command or toolbar button. This
will certainly cause VB to crash.

The code is somewhat incomplete in a number of ways. For example, it
doesn't account for the possibility of Form2 or Form3 being unloaded. If
either of them are unloaded and you move Form1, the form will reload but not
be shown. So be careful about that too. Because of this, you probably
shouldn't use the form's name in code as I did above. Instead, declare your
own object variables and explicitly set references to the forms. As I said,
it's not the most elegant code.

Mike

"Dan Myhre" <danm...@uwm.edu> wrote in message
news:epxI0w7mBHA.2104@tkmsftngp07...

Dan Myhre

не прочитано,
15 янв. 2002 г., 19:49:1115.01.2002
Thank you very much!! That is the most detialed reply I have ever seen!
Thanks Mike.

Dan

"MikeD" <nob...@nowhere.edu> wrote in message
news:#HcQ8P9mBHA.1956@tkmsftngp04...

0 новых сообщений