[Webinar] Streamline your web hosting managementRegister Today


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 71

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


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: 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.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

608 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