Solved

Cannot programmically press button, getting a WM_MOUSELEAVE after WM_LBUTTONDOWN

Posted on 2016-08-21
7
54 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web Form VB.Net  import CSV 4 36
VB.net Open Folder in Windows Explorer 3 24
Sql server insert 13 26
i have to take the screenshot of command prompt? how to do this? 1 35
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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

15 Experts available now in Live!

Get 1:1 Help Now