Go Premium for a chance to win a PS4. Enter to Win


Form Move Event

Posted on 2000-03-12
Medium Priority
Last Modified: 2012-06-27
How can I catch Form Move Event ?

Any ideas ?...
Question by:Maximka
LVL 70

Expert Comment

by:Éric Moreau
ID: 2609589
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
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2609746
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.


Accepted Solution

Vbmaster earned 200 total points
ID: 2610219
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2610410
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>.
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2611103
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!

Author Comment

ID: 2611137
Thanx man, it just Excellent!!!

Expert Comment

ID: 2611406
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? ;)

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question