Solved

Cannot programmically press button, getting a WM_MOUSELEAVE after WM_LBUTTONDOWN

Posted on 2016-08-21
7
40 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now