• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Form Move Event

How can I catch Form Move Event ?

Any ideas ?...
1 Solution
Éric MoreauSenior .Net ConsultantCommented:
You can verify the coordinates at fixed interval (with a timer control for example):

Private Sub Form_Load()
    With Timer1
        .Interval = 500
        .Enabled = True
    End With
End Sub

Private Sub Timer1_Timer()
Static lngLeft As Long
Static lngTop As Long

    If lngLeft <> Me.Left Or lngTop <> Me.Top Then
        lngLeft = Me.Left
        lngTop = Me.Top
    End If
End Sub
Guy Hengel [angelIII / a3]Billing EngineerCommented:
The previous solution works, but it has the unconvient that you will always have the 500 ms delay.
You may instead
* change to C++, Delphi or other language developing (-;
* try to implement a frame that you use instead of a standard form caption (the move corresponds to a "OLEDrag" of the frame). OK, this is not nice as solution
* I think there must be a control of some third-party vendor that gives you this (i saw it once, but i don't know any longer). I may try to find it again.

It's always amusing to hear the comment 'change to C++' whenver someone don't find the solution in the helpfile. ;)

Using subclassing this is a really easy task, here's the code for a form I just created for this specific task. It uses the DLL file at http://www.vbaccelerator.com/codelib/ssubtmr/ssubtmr.htm (it's FREE and you can also get the full source code there), but it aint too difficult to convert it to a non-dll version if you would like to. I would recommend using the DLL file anyway, because subclassing without the DLL means you get a very unstable IDE - VB will crash whenever you press the Stop button).

Anyway here's the code, this will show the current position of the window every time the window is moved.

Begin VB.Form frmPos
   Caption         =   "Sub-Classing Demo 1"
   ClientHeight    =   3180
   ClientLeft      =   5265
   ClientTop       =   2010
   ClientWidth     =   3780
   Icon            =   "frmPos.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   3180
   ScaleWidth      =   3780
Attribute VB_Name = "frmPos"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Implements ISubclass
Private m_emr As EMsgResponse


Private Type WINDOWPOS
  hwnd As Long
  hWndInsertAfter As Long
  x As Long
  y As Long
  cx As Long
  cy As Long
  flags As Long
End Type

Private Sub Form_Load()

  AttachMessage Me, Me.hwnd, WM_WINDOWPOSCHANGED

End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  DetachMessage Me, Me.hwnd, WM_WINDOWPOSCHANGED

End Sub
Private Property Let ISubclass_MsgResponse(ByVal RHS As SSubTimer.EMsgResponse)
  m_emr = RHS

End Property
Private Property Get ISubclass_MsgResponse() As SSubTimer.EMsgResponse
  m_emr = emrConsume
  ISubclass_MsgResponse = m_emr

End Property
Private Function ISubclass_WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long


  'Copy parameter to local variable for processing
  CopyMemory mmiT, ByVal lParam, LenB(mmiT)
  'Show the position of the form
  Me.Caption = mmiT.x & "," & mmiT.y
  'Copy modified results back to parameter
  CopyMemory ByVal lParam, mmiT, LenB(mmiT)

End Function
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Hmmm... I agree with vbmaster that change to C++ or Delphi is a cop-out, however subclassing should be undertaken with EXTREME CAUTION.

I dont mean to be patronising as I am very wary of it meyseld, but subclassing is  a powerful technique it is VERY DANGEROUS and I would only advocate it for situations where your code is pretty much finished, or if you don't mind seeing lots of blue-screens as you try to debug.

I repeat : subclassing gives you lots of control over windows operations, but unless you are very sure you know how the fundamentals of Windows works STEER CLEAR.

I admit that using timer controls is a bit of a hack I but I would advocate using emoreau's approach unless you are 100% confident with subclassing or are fairly sure that your code is complete and wont need debugging <smile>.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
For VBmaster:
I know what it means to implement in VB something that is not in the helpfile (.;
BUT even if you seem to prefer VB over C++, if I see it closely, what you do is call Windows API in you Subclass(which is nothing else than C).

For Maximka:
This solution is clearly a good Windows solution!
MaximkaAuthor Commented:
Thanx man, it just Excellent!!!
As for the crashes in VB.. I use the DLL file from vbaccelerator as long as I debug, and when it's time to distribute the product it's a easy task to replace the DLL with the pure-VB code.

angelIII: and if you look really close you see that when you create a window in a VB program you are actually calling the OS and telling it to create a window, so no more windows then? ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now