dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Detecting top-level form under mouse after mouse-down

  1. #1

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,308

    Resolved [RESOLVED] Detecting top-level form under mouse after mouse-down

    Okay, let me outline the problem, and then I'll show what I've tried.

    This is for an Add-In I'm working on, but I've outlined the problem without using an Add-In. Here's what I want to do: I want to create a MouseDown event when the left mouse button goes down on a Designer form. And, most importantly, I need to know which Designer form (if any) the left mouse button went down on.

    Okay, I can't find any event in the Add-In Designer or Extensibility Model, so I was left to my own devices.

    I tried creating a thread-wide mouse hook. That all works fine, except ... I can't identify the Designer form that the mouse is over.

    Attached is a little project (non-Add-In) that illustrates the problem. Caution, it's got the mouse hook, so be sure and stop it by closing the forms, or you'll crash the IDE. Now, as you click back and forth between Form1 and Form2, you'll notice that it'll report Form1 when you're over Form2 (and vice-versa). This is happening because the mouse hook is firing before the focus changes. To solve this, I tried setting an API timer (SetTimer/KillTimer), but the timer procedure wouldn't fire until the mouse capture was released, so that didn't work. (I need to do things while the mouse is down, and not after it comes back up.)

    I also thought about something like the following, but I can't seem to find the hWnd of the IDE's Designer forms, so I've got nothing to compare to:

    Code:
    
    Public Function hFormWndUnderMouse() As Long
        ' This returns the form (not control or other) under the mouse.
        ' It doesn't matter if the mouse is actually over a control.  It still works.
        Dim pt32 As POINTAPI
        Dim ptx As Long
        Dim pty As Long
        Const GA_ROOT = 2&
        '
        GetCursorPos pt32
        ptx = pt32.x
        pty = pt32.Y
        hFormWndUnderMouse = GetAncestor(WindowFromPointXY(ptx, pty), GA_ROOT)
    End Function
    
    

    See attached for demo project of the problem. Any ideas for a solution are appreciated.

    Thanks,
    Elroy
    Attached Files Attached Files
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  2. #2
    Lively Member
    Join Date
    Feb 2019
    Posts
    110

    Re: Detecting top-level form under mouse after mouse-down

    Try:

    Debug.Print Application.VBE.ActiveWindow.Caption
    Debug.Print Application.VBE.ActiveWindow.Type
    Debug.Print Application.VBE.Windows.Count

    See also "Window Object", "Windows collection", and "DesignerWindow property" in MSDN/VB Reference.

  3. #3

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,308

    Re: Detecting top-level form under mouse after mouse-down

    Hi qvb6,

    I actually already played around with ActiveWindow, and it has the same problem ... the thread-wide MouseDown event fires before the ActiveWindow changes when a new window (possibly a Designer, possibly something else like the Properties Window) gets the focus.

    I've got an idea though. I'm guessing/hoping that the Designer windows have a unique class name. I can check that to see if the mouse is over a Designer window. Then, each Designer window has a unique title (the name of the form they represent). Using that information, I should be able to connect back to a specific Designer window from within the Add-In while in my MouseDown event. I haven't tested yet, but I'm hopeful.

    Thanks,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  4. #4

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,308

    Re: Detecting top-level form under mouse after mouse-down

    My idea outlined above worked perfectly. I'm onto other issues which I'm working through. This one is resolved.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width