Results 1 to 7 of 7

Thread: Move Form Event?

Hybrid View

  1. #1

    Thread Starter
    Lively Member norden's Avatar
    Join Date
    Jun 2003
    Posts
    108

    Move Form Event?

    Is is possible to catch the Move Event of a form without using a message handler?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    By 'using a message handler', do you mean subclassing?

    If so, then the only other way is to use a timer and check the position against the old position. But that's not a very nice way.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Lively Member norden's Avatar
    Join Date
    Jun 2003
    Posts
    108
    Thanks for you quick response crptcblade.

    I hope there is another way to get around it.

    Thanks again...

    -Pls excuse my grammar-

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Here's some code (that I found somewhere around here) that forces your form to stay on the screen. Just change the WM_MOVING case code as you need :

    VB Code:
    1. '** In a standard module
    2. Option Explicit
    3.  
    4. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, _
    5.                                                                             ByVal nIndex As Long, _
    6.                                                                             ByVal dwNewLong As Long) _
    7.                                                                             As Long
    8. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
    9.                                                                               ByVal hwnd As Long, _
    10.                                                                               ByVal Msg As Long, _
    11.                                                                               ByVal wParam As Long, _
    12.                                                                               ByVal lParam As Long) _
    13.                                                                               As Long
    14. Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, _
    15.                                                                             ByVal wMsg As Long, _
    16.                                                                             ByVal wParam As Long, _
    17.                                                                             ByVal lParam As Long) _
    18.                                                                             As Long
    19. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
    20.                                                                      Source As Any, _
    21.                                                                      ByVal Length As Long)
    22.  
    23. Private Const GWL_WNDPROC = (-4)
    24. Private Const WM_MOVING = &H216
    25.  
    26. Private Const WMSZ_LEFT = 1
    27. Private Const WMSZ_RIGHT = 2
    28. Private Const WMSZ_TOP = 3
    29. Private Const WMSZ_TOPLEFT = 4
    30. Private Const WMSZ_TOPRIGHT = 5
    31. Private Const WMSZ_BOTTOM = 6
    32. Private Const WMSZ_BOTTOMLEFT = 7
    33. Private Const WMSZ_BOTTOMRIGHT = 8
    34.  
    35. Private Type RECT
    36.     Left As Long
    37.     Top As Long
    38.     Right As Long
    39.     Bottom As Long
    40. End Type
    41.  
    42. Private mPrevProc As Long
    43.  
    44. Public Sub Hook(ByVal hwnd As Long)
    45.     mPrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
    46. End Sub
    47.  
    48. Public Sub Unhook(ByVal hwnd As Long)
    49.    
    50.     Call SetWindowLong(hwnd, GWL_WNDPROC, mPrevProc)
    51.     mPrevProc = 0&
    52.    
    53. End Sub
    54.  
    55. Private Function PScreenWidth() As Long
    56.     PScreenWidth = (Screen.Width \ 15)
    57. End Function
    58.  
    59. Private Function PScreenHeight() As Long
    60.     PScreenHeight = (Screen.Height \ 15)
    61. End Function
    62.  
    63.  
    64. Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, _
    65.                         ByVal wParam As Long, ByVal lParam As Long) _
    66.                         As Long
    67. On Error Resume Next
    68.  
    69. Dim r As RECT
    70. Dim lTemp As Long
    71.  
    72.     If uMsg = WM_MOVING Then
    73.         Call CopyMemory(r, ByVal lParam, Len(r))
    74.        
    75.         If r.Left < 0 Then
    76.             lTemp = r.Right - r.Left
    77.             r.Left = 0
    78.             r.Right = lTemp
    79.         End If
    80.         If r.Right > PScreenWidth Then
    81.             lTemp = r.Right - r.Left
    82.             r.Right = PScreenWidth
    83.             r.Left = r.Right - lTemp
    84.         End If
    85.         If r.Top < 0 Then
    86.             lTemp = r.Bottom - r.Top
    87.             r.Top = 0
    88.             r.Bottom = lTemp
    89.         End If
    90.         If r.Bottom > PScreenHeight Then
    91.             lTemp = r.Bottom - r.Top
    92.             r.Bottom = PScreenHeight
    93.             r.Top = r.Bottom - lTemp
    94.         End If
    95.        
    96.         Call CopyMemory(ByVal lParam, r, Len(r))
    97.     End If
    98.  
    99.     If mPrevProc& Then
    100.         WndProc = CallWindowProc(mPrevProc, hwnd, uMsg, wParam, lParam)
    101.     Else
    102.         WndProc = DefWindowProc(hwnd, uMsg, wParam, lParam)
    103.     End If
    104.  
    105. End Function

    VB Code:
    1. '** In your form
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.     Call Hook(Me.hwnd)
    6. End Sub
    7.  
    8. Private Sub Form_Unload(Cancel As Integer)
    9.     Call Unhook(Me.hwnd)
    10. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Lively Member norden's Avatar
    Join Date
    Jun 2003
    Posts
    108
    Thanks manavo11, but I really don't want to subclass it if possible.

  6. #6
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    You could install a WH_CBT hook which would notify you if any of your forms moved - although if you are shy of subclassing then hooking is probably anaethema to you....

    How about a component that does the subclassing on your behalf?

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by norden
    Thanks manavo11, but I really don't want to subclass it if possible.
    If you don't mind me asking, why? (BTW I hadn't seen your post)


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width