Solved

Cannot programmically press button, getting a WM_MOUSELEAVE after WM_LBUTTONDOWN

Posted on 2016-08-21
7
65 Views
Last Modified: 2016-08-25
I am trying to press a graphical button that does not have a HWND. I have the following code:

        Dim MousePos As Long = MakeDWord(clientPoint.X, clientPoint.Y)
        Dim iDownResult As Long = SendMessage(wndHandle, WM_LBUTTONDOWN, MK_LBUTTON, MousePos)
        Dim iUpResult As Long = SendMessage(wndHandle, WM_LBUTTONUP, MK_LBUTTON, MousePos)

However the button is not actually being pressed. The button graphic changes to the "pressed down" graphic, but the expected action from the button being pressed does not occur. Checking the window with SPY++ running shows that the messages are being received with correct arguments, however a "WM_MOUSELEAVE" is occurring after both SendMessages. The windows are active, have focus, and are on top of the screen.

When I test it by pressing the button manually there is no "WM_MOUSELEAVE".

How can I get past this and have the button actually press.

Thanks
0
Comment
Question by:tolvor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:Reece Cottam
ID: 41764607
If this is in VB.NET, why not fire off a custom event instead of using messages?

It seems like you're making this very complicated by doing it that way, is there a reason you're doing that?
0
 

Author Comment

by:tolvor
ID: 41764608
The button is in another application that I don't have access to the code. I want my application to send mouse clicks to keep another application active.
0
 
LVL 1

Expert Comment

by:Reece Cottam
ID: 41764610
and this is in Winforms I presume?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:tolvor
ID: 41764750
Yes, this is a WinForms based application, using vb.net, Visual Studio 2013, on a Windows 10 platform.
0
 
LVL 1

Accepted Solution

by:
Reece Cottam earned 500 total points
ID: 41764934
Have you tried the SendInput method instead of SendMessage?

Like this;

 public class ClickOnPointTool
  {

    [DllImport("user32.dll")]
    static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);

    [DllImport("user32.dll")]
    internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs,  int cbSize);

#pragma warning disable 649
    internal struct INPUT
    {
      public UInt32 Type;
      public MOUSEKEYBDHARDWAREINPUT Data;
    }

    [StructLayout(LayoutKind.Explicit)]
    internal struct MOUSEKEYBDHARDWAREINPUT
    {
      [FieldOffset(0)]
      public MOUSEINPUT Mouse;
    }

    internal struct MOUSEINPUT
    {
      public Int32 X;
      public Int32 Y;
      public UInt32 MouseData;
      public UInt32 Flags;
      public UInt32 Time;
      public IntPtr ExtraInfo;
    }


    public static void ClickOnPoint(IntPtr wndHandle , Point clientPoint)
    {
      var oldPos = Cursor.Position;

      /// get screen coordinates
      ClientToScreen(wndHandle, ref clientPoint);

      /// set cursor on coords, and press mouse
      Cursor.Position = new Point(clientPoint.X, clientPoint.Y);

      var inputMouseDown = new INPUT();
      inputMouseDown.Type = 0; /// input type mouse
      inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down

      var inputMouseUp = new INPUT();
      inputMouseUp.Type = 0; /// input type mouse
      inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up

      var inputs = new INPUT[] { inputMouseDown, inputMouseUp };
      SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));

      /// return mouse
      Cursor.Position = oldPos;
    }

  }
0
 
LVL 1

Expert Comment

by:Reece Cottam
ID: 41764951
Also, are you triggering the button up? if you leave it down then the event won't fire
0
 

Author Closing Comment

by:tolvor
ID: 41771205
It works. Many thanks  :-)
1

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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