Solved

Cannot programmically press button, getting a WM_MOUSELEAVE after WM_LBUTTONDOWN

Posted on 2016-08-21
7
61 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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