By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

Simulate Mouse Click Without Focus

P: n/a
Is it possible to simulate a mouse click in the window I made (it's a
Form), but not give it focus?

I tried using WinAPI's mouseevent and SendMessage of
WM_LBUTTONDOWN/WM_LBUTTONUP as suggested by the folks in #winapi on
EFnet, but I think those require the window to have focus. Either that,
or I'm doing something else wrong. The click actually seems to work
once, and then stops working... I have no idea what is the problem
actually. How do I go about debugging this? I tried using SetPixel to
draw where it was simulating a click but I don't see anything drawn...

Thanks for reading.

Dec 11 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Just call the method.

Cor

<el***********@gmail.comschreef in bericht
news:11**********************@f1g2000cwa.googlegro ups.com...
Is it possible to simulate a mouse click in the window I made (it's a
Form), but not give it focus?

I tried using WinAPI's mouseevent and SendMessage of
WM_LBUTTONDOWN/WM_LBUTTONUP as suggested by the folks in #winapi on
EFnet, but I think those require the window to have focus. Either that,
or I'm doing something else wrong. The click actually seems to work
once, and then stops working... I have no idea what is the problem
actually. How do I go about debugging this? I tried using SetPixel to
draw where it was simulating a click but I don't see anything drawn...

Thanks for reading.

Dec 11 '06 #2

P: n/a
I don't understand. Call which method?

Cor Ligthert [MVP] wrote:
Just call the method.

Cor

<el***********@gmail.comschreef in bericht
news:11**********************@f1g2000cwa.googlegro ups.com...
Is it possible to simulate a mouse click in the window I made (it's a
Form), but not give it focus?

I tried using WinAPI's mouseevent and SendMessage of
WM_LBUTTONDOWN/WM_LBUTTONUP as suggested by the folks in #winapi on
EFnet, but I think those require the window to have focus. Either that,
or I'm doing something else wrong. The click actually seems to work
once, and then stops working... I have no idea what is the problem
actually. How do I go about debugging this? I tried using SetPixel to
draw where it was simulating a click but I don't see anything drawn...

Thanks for reading.
Dec 11 '06 #3

P: n/a
okay, I think I understand - you mean the method for the Form. I get
it.

However, I don't have the source code to that form. it's just an .exe

What can I do now?

eliss.carm...@gmail.com wrote:
I don't understand. Call which method?

Cor Ligthert [MVP] wrote:
Just call the method.

Cor

<el***********@gmail.comschreef in bericht
news:11**********************@f1g2000cwa.googlegro ups.com...
Is it possible to simulate a mouse click in the window I made (it's a
Form), but not give it focus?
>
I tried using WinAPI's mouseevent and SendMessage of
WM_LBUTTONDOWN/WM_LBUTTONUP as suggested by the folks in #winapi on
EFnet, but I think those require the window to have focus. Either that,
or I'm doing something else wrong. The click actually seems to work
once, and then stops working... I have no idea what is the problem
actually. How do I go about debugging this? I tried using SetPixel to
draw where it was simulating a click but I don't see anything drawn...
>
Thanks for reading.
>
Dec 11 '06 #4

P: n/a
WhatIWantToDo(null,null)
<el***********@gmail.comschreef in bericht
news:11**********************@80g2000cwy.googlegro ups.com...
>I don't understand. Call which method?

Cor Ligthert [MVP] wrote:
>Just call the method.

Cor

<el***********@gmail.comschreef in bericht
news:11**********************@f1g2000cwa.googlegr oups.com...
Is it possible to simulate a mouse click in the window I made (it's a
Form), but not give it focus?

I tried using WinAPI's mouseevent and SendMessage of
WM_LBUTTONDOWN/WM_LBUTTONUP as suggested by the folks in #winapi on
EFnet, but I think those require the window to have focus. Either that,
or I'm doing something else wrong. The click actually seems to work
once, and then stops working... I have no idea what is the problem
actually. How do I go about debugging this? I tried using SetPixel to
draw where it was simulating a click but I don't see anything drawn...

Thanks for reading.

Dec 11 '06 #5

P: n/a
<el***********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
okay, I think I understand - you mean the method for the Form. I get
it.

However, I don't have the source code to that form. it's just an .exe

What can I do now?
I'm not sure if the form requires focus. Most apps won't require the focus
afaik but maybe windows does some checks before sending the message. Use
spy++ on the button/form in question and see what message it is receiving.
Have a look to see how this varies if you really do click it.

Michael
Dec 11 '06 #6

P: n/a
I checked with spy++ and it said it was receiving a wm_lbuttondown and
wm_lbuttonup at a location. But I don't know where that location is...
how do I know if it's the right place? Also, only one click works (the
first one) and then clicks do nothing. How can I debug it?

Michael C wrote:
<el***********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
okay, I think I understand - you mean the method for the Form. I get
it.

However, I don't have the source code to that form. it's just an .exe

What can I do now?

I'm not sure if the form requires focus. Most apps won't require the focus
afaik but maybe windows does some checks before sending the message. Use
spy++ on the button/form in question and see what message it is receiving.
Have a look to see how this varies if you really do click it.

Michael
Dec 11 '06 #7

P: n/a
el***********@gmail.com wrote:
>However, I don't have the source code to that form. it's just an .exe
I don't understand. You said it's a window that *you made*. How did
you make it? Why don't you have the source code?

And what *exactly* are you trying to do? Spoof a click on a button? Or
on some other part of the form?

If it's for a button, I think it's better to use WM_COMMAND send to
the form itself. Look that up in MSDN to figure out what parameters to
use.

--
Lucian
Dec 11 '06 #8

P: n/a
It might not be a button. that's the thing. It could be a menu, or
textbox, or just a part of the window.

I made it, meaning I Executed it. It's another application. For
example, like I execute calc.exe

Thanks

Lucian Wischik wrote:
el***********@gmail.com wrote:
However, I don't have the source code to that form. it's just an .exe

I don't understand. You said it's a window that *you made*. How did
you make it? Why don't you have the source code?

And what *exactly* are you trying to do? Spoof a click on a button? Or
on some other part of the form?

If it's for a button, I think it's better to use WM_COMMAND send to
the form itself. Look that up in MSDN to figure out what parameters to
use.

--
Lucian
Dec 11 '06 #9

P: n/a
el***********@gmail.com wrote:
>It might not be a button. that's the thing. It could be a menu, or
textbox, or just a part of the window.
We can't give you a general answer. Every application can be written
to respond to messages however it sees fit. Some applications may
choose to respond to WM_LBUTTONDOWN only if they have focus. Other
applications may choose to respond to WM_LBUTTONUP only if they're on
the right hand half of the screen.

All that will guide you here are (1) extensive familiarity with how
applications typically handle win32 messages, (2) specific
experimentation with particular parts of these applications that
you're trying to control.

Without telling us (2), those of us who know (1) can't help at all!

I have to say that it's rare for focus to be an issue in handling
WM_LBUTTONDOWN or WM_LBUTTONUP, so the problem is elsewhere. Sometimes
application's don't like receiving the two messages in rapid
succession. You might consider putting a Sleep(500) delay and see if
it works.

For parts of windows, some applications will require appropriate
WM_MOUSEMOVEs to have come. And menus are a complete black art to me!

Really, everything you do has to proceed by gradual experimentation.
If you're going to be trying to control many different types of
applications ("buttons, menus, textboxes, parts of windows") then you
might consider investing your time in a "experimenter's toolbox" --
write an application that lets you interactively send messages to
other apps, without having to recompile/rerun all the time.

--
Lucian
Dec 11 '06 #10

P: n/a
But all applications respond to keyboard presses and mouse clicks. I
just want to pretend I'm the mouse. I want to control ALL
applications... how would a mouse/keyboard do it? Can't I just send raw
input?

Lucian Wischik wrote:
el***********@gmail.com wrote:
It might not be a button. that's the thing. It could be a menu, or
textbox, or just a part of the window.

We can't give you a general answer. Every application can be written
to respond to messages however it sees fit. Some applications may
choose to respond to WM_LBUTTONDOWN only if they have focus. Other
applications may choose to respond to WM_LBUTTONUP only if they're on
the right hand half of the screen.

All that will guide you here are (1) extensive familiarity with how
applications typically handle win32 messages, (2) specific
experimentation with particular parts of these applications that
you're trying to control.

Without telling us (2), those of us who know (1) can't help at all!

I have to say that it's rare for focus to be an issue in handling
WM_LBUTTONDOWN or WM_LBUTTONUP, so the problem is elsewhere. Sometimes
application's don't like receiving the two messages in rapid
succession. You might consider putting a Sleep(500) delay and see if
it works.

For parts of windows, some applications will require appropriate
WM_MOUSEMOVEs to have come. And menus are a complete black art to me!

Really, everything you do has to proceed by gradual experimentation.
If you're going to be trying to control many different types of
applications ("buttons, menus, textboxes, parts of windows") then you
might consider investing your time in a "experimenter's toolbox" --
write an application that lets you interactively send messages to
other apps, without having to recompile/rerun all the time.

--
Lucian
Dec 11 '06 #11

P: n/a
el***********@gmail.com wrote:
>But all applications respond to keyboard presses and mouse clicks. I
just want to pretend I'm the mouse. I want to control ALL
applications... how would a mouse/keyboard do it? Can't I just send raw
input?
Pretending to be the mouse/keyboard is much easier. The win32 function
to send raw input is "SendInput" and there's a .net wrapper around it
whose name I've forgotten.

--
Lucian
Dec 11 '06 #12

P: n/a
Thanks for your help so far. This is the most frustrating thing ever. I
switched to using mouse_event, which should be exactly like SendInput.
(Is that the wrapper?)

Anyways, I am receiving the WM_LBUTTONDOWN and WM_LBUTTONDOWN as seen
on Spy++. However, nothing is getting clicked

Here is my code to click:

short x = BitConverter.ToInt16(m_pBuff, 0);
short y = BitConverter.ToInt16(m_pBuff, 2);

Winapi.POINT p = new Winapi.POINT(x, y);
Winapi.ClientToScreen(m_target, ref p);
System.Diagnostics.Debug.WriteLine(p.X.ToString() + "," +
p.Y.ToString());
Winapi.mouse_event((uint)Winapi.MouseEventTFlags.A BSOLUTE |
(uint)Winapi.MouseEventTFlags.LEFTDOWN, (uint)p.X, (uint)p.Y, 0,
System.UIntPtr.Zero);
Winapi.mouse_event((uint)Winapi.MouseEventTFlags.A BSOLUTE |
(uint)Winapi.MouseEventTFlags.LEFTUP, (uint)p.X, (uint)p.Y, 0,
System.UIntPtr.Zero);
Here is my code from the "mirrored" application, in the WndProc which
is sending the click. It's basically an image replica of the real
application:

case (int)Winapi.WM_LBUTTONUP:
byte[] b = BitConverter.GetBytes((int)m.LParam);
System.Diagnostics.Debug.WriteLine(b.Length);
m_networkstream.Write(b, 0, b.Length);
break;
Lucian Wischik wrote:
el***********@gmail.com wrote:
But all applications respond to keyboard presses and mouse clicks. I
just want to pretend I'm the mouse. I want to control ALL
applications... how would a mouse/keyboard do it? Can't I just send raw
input?

Pretending to be the mouse/keyboard is much easier. The win32 function
to send raw input is "SendInput" and there's a .net wrapper around it
whose name I've forgotten.

--
Lucian
Dec 12 '06 #13

P: n/a
I have a hypothesis -- could it be that mouse_event coordinates are
different from actual pixel coordinates? The MSDN doc says 0,0 is the
upper left and 65535,65535 is the bottom right. That's weird. What if
my resolution is 1280,1024? Do I need to multiple the screen
coordinates I get by 65 or something?
el***********@gmail.com wrote:
Thanks for your help so far. This is the most frustrating thing ever. I
switched to using mouse_event, which should be exactly like SendInput.
(Is that the wrapper?)

Anyways, I am receiving the WM_LBUTTONDOWN and WM_LBUTTONDOWN as seen
on Spy++. However, nothing is getting clicked

Here is my code to click:

short x = BitConverter.ToInt16(m_pBuff, 0);
short y = BitConverter.ToInt16(m_pBuff, 2);

Winapi.POINT p = new Winapi.POINT(x, y);
Winapi.ClientToScreen(m_target, ref p);
System.Diagnostics.Debug.WriteLine(p.X.ToString() + "," +
p.Y.ToString());
Winapi.mouse_event((uint)Winapi.MouseEventTFlags.A BSOLUTE |
(uint)Winapi.MouseEventTFlags.LEFTDOWN, (uint)p.X, (uint)p.Y, 0,
System.UIntPtr.Zero);
Winapi.mouse_event((uint)Winapi.MouseEventTFlags.A BSOLUTE |
(uint)Winapi.MouseEventTFlags.LEFTUP, (uint)p.X, (uint)p.Y, 0,
System.UIntPtr.Zero);
Here is my code from the "mirrored" application, in the WndProc which
is sending the click. It's basically an image replica of the real
application:

case (int)Winapi.WM_LBUTTONUP:
byte[] b = BitConverter.GetBytes((int)m.LParam);
System.Diagnostics.Debug.WriteLine(b.Length);
m_networkstream.Write(b, 0, b.Length);
break;
Lucian Wischik wrote:
el***********@gmail.com wrote:
>But all applications respond to keyboard presses and mouse clicks. I
>just want to pretend I'm the mouse. I want to control ALL
>applications... how would a mouse/keyboard do it? Can't I just send raw
>input?
Pretending to be the mouse/keyboard is much easier. The win32 function
to send raw input is "SendInput" and there's a .net wrapper around it
whose name I've forgotten.

--
Lucian
Dec 12 '06 #14

P: n/a
<el***********@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
>I checked with spy++ and it said it was receiving a wm_lbuttondown and
wm_lbuttonup at a location. But I don't know where that location is...
how do I know if it's the right place? Also, only one click works (the
first one) and then clicks do nothing. How can I debug it?
Move the mouse to that position using Cursor.Position or place a form there.
You can;t draw to that location so have to place something there.

Michael
Dec 12 '06 #15

P: n/a
el***********@gmail.com wrote:
>Thanks for your help so far. This is the most frustrating thing ever. I
switched to using mouse_event, which should be exactly like SendInput.
(Is that the wrapper?)
Doh! Read the MSDN docs of functions that are mentioned to you but you
don't yet know! mouse_event is deprecated in favour of SendInput.

>Anyways, I am receiving the WM_LBUTTONDOWN and WM_LBUTTONDOWN as seen
on Spy++. However, nothing is getting clicked
The following (complete) program works, in that I programmatically
synthesize a click on the button and the button's OnClick event does
indeed get called. You might use this as a starting point for your
further explorations.

Like you, I created a button within my own C# application and spoofed
a click on that. What you will probably find is that (1) C# and .net
applications might respond different to mouse events from other
applications, and (2) response by a button INSIDE your own process
will be different from response by a button OUTSIDE. The reason for my
conjecture (1) is that I wouldn't expect the .net authors to follow
the win32 mouse model slavishly. The reason for my conjecture number
(2) is that an external process will have its own asynchronous
message-loop.

Things that I discovered: I couldn't send both MOUSEEVENTF_LBUTTONDOWN
and MOUSEEVENTF_LBUTTONUP both within a single call to SendInput. What
I did in this code was to space them out with a timer. My reasons for
doing this is my experience that lots of people write code which
depends on the message-queue being cleared after the first event
before later stuff happens. (I coded my MOUSEINPUT structure to allow
up to two events. But, following this discovery, my code only uses the
first event.)

I also discovered about how my .net button responded to mouse events.
When it received the MOUSEEVENTF_LBUTTONDOWN, it seemed to ignore the
mouse coordinate parameters of the MOUSEINPUT structure. Instead it
queried the cursor position. I suspect that regular win32 controls
will behave differently here.

Note: I put my code in response to a timer. Partly because I
anticipated problems like the first one I discovered. Partly because
of my conjecture (2): I thought that if I put my code in response to a
mouseclick of mine, then it would end up in a confused state when it
gets another mouseclick immediately.

I've spent five long paragraphs explaining these things. That's
because, as I said, controlling applications through synthesized
mouse-clicks requires a lot of experience with how windows deals with
mice and messages, and a lot of experiments. It also means that no one
will be able to help you "in abstract". They'll only be able to help
when you give very concrete details of the application you're trying
to control, and maybe not even then.

Note: it's because of all these irritations that I suggested using
SendMessage(WM_COMMAND) instead. That reliably bypasses all the
irritations. It works great for sending many kinds of notifications of
button-clicks or slideybar-drags and so on.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{

public partial class Form1:Form
{
[System.Runtime.InteropServices.StructLayout(System .Runtime.InteropServices.LayoutKind.Sequential)]
public struct MOUSEINPUT
{ public static MOUSEINPUT Zero()
{ MOUSEINPUT mi;

mi.type1=INPUT_MOUSE;mi.dx1=0;mi.dy1=0;mi.mouseDat a1=0;mi.dwFlags1=0;mi.time1=0;mi.dwExtraInfo1=IntP tr.Zero;

mi.type2=INPUT_MOUSE;mi.dx2=0;mi.dy2=0;mi.mouseDat a2=0;mi.dwFlags2=0;mi.time2=0;mi.dwExtraInfo2=IntP tr.Zero;
return mi;
}
public int Size() {return (int)Count()*28;}
public UInt32 Count() {if (dwFlags2!=0) return 2; else if
(dwFlags1!=0) return 1; else return 0;}
public Int32 type1, dx1,dy1, mouseData1, dwFlags1, time1; public
IntPtr dwExtraInfo1;
public Int32 type2, dx2,dy2, mouseData2, dwFlags2, time2; public
IntPtr dwExtraInfo2;
}
const Int32 INPUT_MOUSE=0;
const Int32 MOUSEEVENTF_LEFTDOWN=0x0002;
const Int32 MOUSEEVENTF_LEFTUP = 0x0004;
const Int32 MOUSEEVENTF_ABSOLUTE = 0x8000;
[System.Runtime.InteropServices.DllImport("user32.d ll")]
public static extern UInt32 SendInput(UInt32 nInputs, ref MOUSEINPUT
pInputs, Int32 cbSize);
public Form1()
{ InitializeComponent();
}

private void button2_Click(object sender,EventArgs e)
{ MessageBox.Show("click2");
}

int state=1;

private void button1_Click(object sender,EventArgs e)
{ state=1;
}

private void timer1_Tick(object sender,EventArgs e)
{ if (state==0) return;
MOUSEINPUT mi = MOUSEINPUT.Zero();
if (state==1)
{ Point pt=new Point(button2.Width/2,button2.Height/2),
oldpos=Cursor.Position;
Cursor.Position = button2.PointToScreen(pt);
mi.dwFlags1=MOUSEEVENTF_LEFTDOWN;
uint r=SendInput(mi.Count(), ref mi, mi.Size());
if (r==mi.Count()) Console.Beep();
state=3; return;
}
if (state==3)
{ mi.dx1=0; mi.dy1=0; mi.dwFlags1=MOUSEEVENTF_LEFTUP; uint
r=SendInput(1,ref mi, mi.Size());
if (r==1) Console.Beep();
state=0; return;
}
}

}
}
--
Lucian
Dec 12 '06 #16

P: n/a
AHHhhhh... this helps a lot. By picking apart your example, I found out
that the Cursor needs to be above the button for the click to work. The
reason I didn't think this was needed at first was because I was able
to do the fake click thing correctly in WinAPI and minesweeper. I
didn't know buttons were different. Works great now.... I guess
Lucian Wischik wrote:
el***********@gmail.com wrote:
Thanks for your help so far. This is the most frustrating thing ever. I
switched to using mouse_event, which should be exactly like SendInput.
(Is that the wrapper?)

Doh! Read the MSDN docs of functions that are mentioned to you but you
don't yet know! mouse_event is deprecated in favour of SendInput.

Anyways, I am receiving the WM_LBUTTONDOWN and WM_LBUTTONDOWN as seen
on Spy++. However, nothing is getting clicked

The following (complete) program works, in that I programmatically
synthesize a click on the button and the button's OnClick event does
indeed get called. You might use this as a starting point for your
further explorations.

Like you, I created a button within my own C# application and spoofed
a click on that. What you will probably find is that (1) C# and .net
applications might respond different to mouse events from other
applications, and (2) response by a button INSIDE your own process
will be different from response by a button OUTSIDE. The reason for my
conjecture (1) is that I wouldn't expect the .net authors to follow
the win32 mouse model slavishly. The reason for my conjecture number
(2) is that an external process will have its own asynchronous
message-loop.

Things that I discovered: I couldn't send both MOUSEEVENTF_LBUTTONDOWN
and MOUSEEVENTF_LBUTTONUP both within a single call to SendInput. What
I did in this code was to space them out with a timer. My reasons for
doing this is my experience that lots of people write code which
depends on the message-queue being cleared after the first event
before later stuff happens. (I coded my MOUSEINPUT structure to allow
up to two events. But, following this discovery, my code only uses the
first event.)

I also discovered about how my .net button responded to mouse events.
When it received the MOUSEEVENTF_LBUTTONDOWN, it seemed to ignore the
mouse coordinate parameters of the MOUSEINPUT structure. Instead it
queried the cursor position. I suspect that regular win32 controls
will behave differently here.

Note: I put my code in response to a timer. Partly because I
anticipated problems like the first one I discovered. Partly because
of my conjecture (2): I thought that if I put my code in response to a
mouseclick of mine, then it would end up in a confused state when it
gets another mouseclick immediately.

I've spent five long paragraphs explaining these things. That's
because, as I said, controlling applications through synthesized
mouse-clicks requires a lot of experience with how windows deals with
mice and messages, and a lot of experiments. It also means that no one
will be able to help you "in abstract". They'll only be able to help
when you give very concrete details of the application you're trying
to control, and maybe not even then.

Note: it's because of all these irritations that I suggested using
SendMessage(WM_COMMAND) instead. That reliably bypasses all the
irritations. It works great for sending many kinds of notifications of
button-clicks or slideybar-drags and so on.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{

public partial class Form1:Form
{
[System.Runtime.InteropServices.StructLayout(System .Runtime.InteropServices.LayoutKind.Sequential)]
public struct MOUSEINPUT
{ public static MOUSEINPUT Zero()
{ MOUSEINPUT mi;

mi.type1=INPUT_MOUSE;mi.dx1=0;mi.dy1=0;mi.mouseDat a1=0;mi.dwFlags1=0;mi.time1=0;mi.dwExtraInfo1=IntP tr.Zero;

mi.type2=INPUT_MOUSE;mi.dx2=0;mi.dy2=0;mi.mouseDat a2=0;mi.dwFlags2=0;mi.time2=0;mi.dwExtraInfo2=IntP tr.Zero;
return mi;
}
public int Size() {return (int)Count()*28;}
public UInt32 Count() {if (dwFlags2!=0) return 2; else if
(dwFlags1!=0) return 1; else return 0;}
public Int32 type1, dx1,dy1, mouseData1, dwFlags1, time1; public
IntPtr dwExtraInfo1;
public Int32 type2, dx2,dy2, mouseData2, dwFlags2, time2; public
IntPtr dwExtraInfo2;
}
const Int32 INPUT_MOUSE=0;
const Int32 MOUSEEVENTF_LEFTDOWN=0x0002;
const Int32 MOUSEEVENTF_LEFTUP = 0x0004;
const Int32 MOUSEEVENTF_ABSOLUTE = 0x8000;
[System.Runtime.InteropServices.DllImport("user32.d ll")]
public static extern UInt32 SendInput(UInt32 nInputs, ref MOUSEINPUT
pInputs, Int32 cbSize);
public Form1()
{ InitializeComponent();
}

private void button2_Click(object sender,EventArgs e)
{ MessageBox.Show("click2");
}

int state=1;

private void button1_Click(object sender,EventArgs e)
{ state=1;
}

private void timer1_Tick(object sender,EventArgs e)
{ if (state==0) return;
MOUSEINPUT mi = MOUSEINPUT.Zero();
if (state==1)
{ Point pt=new Point(button2.Width/2,button2.Height/2),
oldpos=Cursor.Position;
Cursor.Position = button2.PointToScreen(pt);
mi.dwFlags1=MOUSEEVENTF_LEFTDOWN;
uint r=SendInput(mi.Count(), ref mi, mi.Size());
if (r==mi.Count()) Console.Beep();
state=3; return;
}
if (state==3)
{ mi.dx1=0; mi.dy1=0; mi.dwFlags1=MOUSEEVENTF_LEFTUP; uint
r=SendInput(1,ref mi, mi.Size());
if (r==1) Console.Beep();
state=0; return;
}
}

}
}
--
Lucian
Dec 12 '06 #17

P: n/a
Setting the cursor position is a really good idea. It definitely helped
me debug this problem. Thanks!

Michael C wrote:
<el***********@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
I checked with spy++ and it said it was receiving a wm_lbuttondown and
wm_lbuttonup at a location. But I don't know where that location is...
how do I know if it's the right place? Also, only one click works (the
first one) and then clicks do nothing. How can I debug it?

Move the mouse to that position using Cursor.Position or place a form there.
You can;t draw to that location so have to place something there.

Michael
Dec 12 '06 #18

P: n/a
<el***********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
AHHhhhh... this helps a lot. By picking apart your example, I found out
that the Cursor needs to be above the button for the click to work. The
reason I didn't think this was needed at first was because I was able
to do the fake click thing correctly in WinAPI and minesweeper. I
didn't know buttons were different. Works great now.... I guess
Maybe there is an issue with capturing the mouse. When a button gets a mouse
down message it will generally call SetCapture. This way if the user moves
the mouse off the button (with the mouse down) it will get a message this
has happened. If the user does this and then loses the capture without
letting go of the mouse (eg pushing ctrl-esc to show the start menu) then
the button will know it's been released. Maybe SetCapture fails if the mouse
is not over the button.

Michael
Dec 12 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.