Form Move Event

Posted on 2000-03-12
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 50 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 (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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

838 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