5

I'd like to perform a click in a windows application, without using the real mouse (so I can minimize it). Much like a bot would behave.

How would I do this?

Brian
  • 5,069
  • 7
  • 37
  • 47
Frunk
  • 180
  • 1
  • 2
  • 11
  • 2
    Is there a reason you can't just Minimize in code? – sa_ddam213 Feb 28 '13 at 21:49
  • You want simulate it in your application or another application that you don't have the source code? – Saman Gholami Feb 28 '13 at 21:49
  • No i have the source code – Frunk Feb 28 '13 at 21:53
  • And i don't want to minimize it, i want to perform the click while it's minimized – Frunk Feb 28 '13 at 21:59
  • 3
    You might get better answers if you provide more information about what you want. "Without using a real mouse" could mean without the user having to physically grab the mouse... – Steve's a D Feb 28 '13 at 22:00
  • 2
    You see what it says when you mouse over the up arrow next to this question? "This question shows reseach effort; it is useful and clear". None of these criteria are met, how this question has 3 upvotes baffles me. – Steve's a D Feb 28 '13 at 22:02
  • 2
    @Steve, it doesn't seems that difficult to understand to me; in easiest terms, I fairly sure he wants something like "Hey ApplicationX, I just clicked you at x200, y400" - While ApplicationX is actually minimized, and the real mouse pointer isn't affected – Ron Sijm Feb 28 '13 at 22:08
  • @RonSijm I seem to disagree. Regardless if it's clear or not, it still fails the "What have you tried?" test. – Steve's a D Feb 28 '13 at 22:11
  • 1
    @user2047975 Was my answer below acceptable? If so, please accept as answer. If not, please let me know what I can do to clarify or improve it for you. – Deep in the Code Mar 01 '13 at 16:08

5 Answers5

4

I think the function you're looking for is PostMessage

[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

You can read more about it here on codeproject, and download a demo project, which sends keystrokes.

This method posts messages directly on the input queue associated with the program, based on the process handle you use (hWnd)

You can also use this function to send mouse clicks with it, by posting button events, like so:

PostMessage(hWnd, WM_LBUTTONDBLCLK, 0, l);

More information about those button events can be found here on MSDN.

I'm sure if you search around the internet for samples for PostMessage mouse events you'll find plenty

Ron Sijm
  • 8,490
  • 2
  • 31
  • 48
  • This one is closest to the real answer. You need to use SendMessage to reliably ensure correct ordering of messages with applications using DirectInput however. – Zoey Feb 16 '14 at 07:55
  • Link to "article" is misleading. I would laugh at the joke but there are posts on SO claiming it can't be done with `PostMessage(hWnd, WM_LBUTTONDBLCLK, ?, ?)`. Have you tried it and confirmed it works? – Emperor Orionii Apr 16 '14 at 09:22
  • @EmperorOrionii the link appears to be broken or changed to something else. I've removed it from my answer. But yes, I have used SendMessage and PostMessage before to send keystrokes or mouse events into applications. – Ron Sijm Apr 16 '14 at 14:40
4

You can use a timer, or handle key press, and create in the timer tick or key press function simulate a mouse click, using the user32.dll file (better be in a form shape so you can handle timer interval...):

using System;
using System.Windows.Forms;
namespace Clicker
{
    public partial class Form1 : Form
    {
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    private const int MOUSEEVENT_LEFTDOWN = 0x02;
    private const int MOUSEEVENT_LEFTUP = 0x04;
    private const int MOUSEEVENT_MIDDLEDOWN = 0x20;
    private const int MOUSEEVENT_MIDDLEUP = 0x40;
    private const int MOUSEEVENT_RIGHTDOWN = 0x08;
    private const int MOUSEEVENT_RIGHTUP = 0x10;

    private int count = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
        label2.Text = "Timer is on";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        label2.Text = "Timer is off";
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
        count++;
        label3.Text = count + " amount of clicks";
    }

}

}

gever
  • 99
  • 1
  • 10
2
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);    
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

public void MouseClick()
{
   int x = 100;
   int y = 100;
   mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
   mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

I found this at http://social.msdn.microsoft.com/forums/en-US/winforms/thread/86dcf918-0e48-40c2-88ae-0a09797db1ab/.

cHao
  • 84,970
  • 20
  • 145
  • 172
Deep in the Code
  • 572
  • 1
  • 6
  • 20
  • 1
    This actually uses the real mouse – Frunk Feb 28 '13 at 21:50
  • 1
    By virtual-mouse click I assume he means, for example "MouseClick("Internet Explorer", 100, 400); which will send a click to IE, even though it stays minimized. – Ron Sijm Feb 28 '13 at 21:53
  • This does not use the physical mouse; according to the submitter, it "simulate[s] a mouse click by calling the mouse_event API", not actually using the mouse. – Deep in the Code Feb 28 '13 at 21:54
  • 4
    I think you're suppose to interpret his question as "A click in a windows application, without using the real mouse, so I can [send a click to it, while the window is minimized]" - so like a bot clicking inside the minimized window. – Ron Sijm Feb 28 '13 at 21:59
1

You'll need the resolution of the machine that it's on, use the System.Windows.Forms.Screen class, here: SO

You will then need to move the mouse to that location, or avoiding that, you may need to hook to the program that's running, and send it an event that causes it to minimize.

It's going to be hard to get something like this to work with C#, as you'll need to inject that DLL into the program. A lower level language like C may be helpful.

Here's a brief explanation / question

Community
  • 1
  • 1
plast1K
  • 469
  • 3
  • 13
0

You can use Window Automation to find Minimize button in window and perform click. I find it easy and used a lot. here is the link to understand the whole concept. https://msdn.microsoft.com/en-us/library/windows/desktop/ff486375(v=vs.85).aspx