1 Replies - 4387 Views - Last Post: 24 December 2011 - 09:30 AM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Clicking on the same control does not enter loop - using hwnd property

Posted 22 December 2011 - 12:19 PM

Public Function MouseInWindow(ByVal p_Hwnd As Long) As Boolean
Dim lt_Pt As POINTAPI
Dim l_MouseWindow As Long
GetCursorPos lt_Pt
l_MouseWindow = WindowFromPoint(lt_Pt.x, lt_Pt.y)
Do While (l_MouseWindow <> p_Hwnd) And (l_MouseWindow <> 0)
   ' Run up the chain of parent windows looking for a match
   l_MouseWindow = GetAncestor(l_MouseWindow, GA_PARENT)
Loop
MouseInWindow = (l_MouseWindow = p_Hwnd)
End Function


This function checks if the mouse is anywhere in the form (p_Hwnd is the form's handle) except for the control which has been clicked. Clicking again on the same control from which the event has been at first fired makes l_MouseWindow = p_Hwnd, which does not meet the "l_MouseWindow <> p_Hwnd" condition, and the loop is not entered. How to correct this?

E.g. Click on a control, say, a combo box. The list is dropped down. Click anywhere on the form except on that same control. The loop is entered and MouseInWindow eventually becomes True, and, according to the caller, the combo box is retracted and made not visible. Now, drop down the combo and click again on the drop down arrow in an attempt to retract the list; it does not work because "l_MouseWindow = p_Hwnd", which causes the function not to go to l_MouseWindow = GetAncestor(l_MouseWindow, GA_PARENT) and eventually makes MouseInWindow False.

This post has been edited by AN1554: 22 December 2011 - 02:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Clicking on the same control does not enter loop - using hwnd property

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Clicking on the same control does not enter loop - using hwnd property

Posted 24 December 2011 - 09:30 AM

Do
   ' Run up the chain of parent windows looking for a match
   l_MouseWindow = GetAncestor(l_MouseWindow, GA_PARENT)
Loop While (l_MouseWindow <> p_Hwnd) And (l_MouseWindow <> 0)

This will work if you're on the control, because you'll do a GetAncestor before you evaluate the condition. However, if you're on the form somewhere and not on a control, you'll do a GetAncestor before you check whether the current window handle is that of the form. If you check whether the current window handle is the form, return true if so, and if not enter the loop, that should work, but there might be a more elegant solution. If you find one please publish it.

Excellent explanation of your problem, by the way. :)

This post has been edited by BobRodes: 24 December 2011 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1