Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot programmically press button, getting a WM_MOUSELEAVE after WM_LBUTTONDOWN

Posted on 2016-08-21
7
Medium Priority
?
72 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

782 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