Pressing button

  • Thread starter Thread starter ReneMarxis
  • Start date Start date
R

ReneMarxis

Hello

i need some hints on how to implement a small app that does click an
"Ok" button on a form that comes up from one other app once every 10
minutes.

Something like "Push the freaking button".

Any ideas where to get some information for this?

_thanks rene
 
Hello

i need some hints on how to implement a small app that does click an
"Ok" button on a form that comes up from one other app once every 10
minutes.

Something like "Push the freaking button".

Any ideas where to get some information for this?

_thanks rene

You need to use PInvokes.

You'll need

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter, string className, IntPtr windowTitle);

[DllImport("user32.dll")]
private static extern bool PostMessage(
IntPtr hWnd, // handle to destination window
UInt32 Msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]


That lot, then the process is basically find the window by name
(assuming it has a name)

window=FindWindow(null, "Popup box")

Then you have to find the control and send it a message, the following
is a bit scrubby, it's out of an application I wrote a while ago, but
it should give you the idea. In this case you can see I was trying to
drive a VB6 application thus the reference to ThunderRT6CommandButton.

int hWnd =
(int)FindWindowEx((IntPtr)int.Parse(txt1.Text) ,IntPtr.Zero,"ThunderRT6CommandButton",null);//"Text1");
hWnd = (int)FindWindowEx((IntPtr)int.Parse(txt1.Text) ,(IntPtr)
hWnd,"ThunderRT6CommandButton",null);//"Text1");
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONDOWN,0,0).ToString();
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONUP,0,0).ToString();


That's not a complete solution, but it should point you in the right
direction.
 
I agree you will need P/Invoke to do this, and I agree with the call to
FindWindow. However, I think that once the button is found, you should call
SendInput instead of SendMessage to synthesize the click event. This way,
the proper sequence of messages is generated. You just have to find a
region within the button to send the mouse click to.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DeveloperX said:
Hello

i need some hints on how to implement a small app that does click an
"Ok" button on a form that comes up from one other app once every 10
minutes.

Something like "Push the freaking button".

Any ideas where to get some information for this?

_thanks rene

You need to use PInvokes.

You'll need

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter, string className, IntPtr windowTitle);

[DllImport("user32.dll")]
private static extern bool PostMessage(
IntPtr hWnd, // handle to destination window
UInt32 Msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]


That lot, then the process is basically find the window by name
(assuming it has a name)

window=FindWindow(null, "Popup box")

Then you have to find the control and send it a message, the following
is a bit scrubby, it's out of an application I wrote a while ago, but
it should give you the idea. In this case you can see I was trying to
drive a VB6 application thus the reference to ThunderRT6CommandButton.

int hWnd =
(int)FindWindowEx((IntPtr)int.Parse(txt1.Text)
,IntPtr.Zero,"ThunderRT6CommandButton",null);//"Text1");
hWnd = (int)FindWindowEx((IntPtr)int.Parse(txt1.Text) ,(IntPtr)
hWnd,"ThunderRT6CommandButton",null);//"Text1");
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONDOWN,0,0).ToString();
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONUP,0,0).ToString();


That's not a complete solution, but it should point you in the right
direction.
 
I agree you will need P/Invoke to do this, and I agree with the call to
FindWindow. However, I think that once the button is found, you should call
SendInput instead of SendMessage to synthesize the click event. This way,
the proper sequence of messages is generated. You just have to find a
region within the button to send the mouse click to.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




You need to use PInvokes.
You'll need
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll")]
private static extern bool PostMessage(
IntPtr hWnd, // handle to destination window
UInt32 Msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
That lot, then the process is basically find the window by name
(assuming it has a name)
window=FindWindow(null, "Popup box")
Then you have to find the control and send it a message, the following
is a bit scrubby, it's out of an application I wrote a while ago, but
it should give you the idea. In this case you can see I was trying to
drive a VB6 application thus the reference to ThunderRT6CommandButton.
int hWnd =
(int)FindWindowEx((IntPtr)int.Parse(txt1.Text)
,IntPtr.Zero,"ThunderRT6CommandButton",null);//"Text1");
hWnd = (int)FindWindowEx((IntPtr)int.Parse(txt1.Text) ,(IntPtr)
hWnd,"ThunderRT6CommandButton",null);//"Text1");
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONDOWN,0,0).ToString();
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONUP,0,0).ToString();
That's not a complete solution, but it should point you in the right
direction.- Hide quoted text -

- Show quoted text -

Good point. You can see there was a fair bit of guess work going on
with the above :)

Rene, for all things pinvokey take a look at http://www.pinvoke.net/

Here's their page for SendInput:
http://www.pinvoke.net/search.aspx?search=sendinput&namespace=[All]
 
Hello to all

many thanks for your answers :)

But i missed out a point very sorry: I did that thing already in a c++
application, so i did know that already (although i did not try it
on .Net)
What i originally wanted to do is to "subscribe" somehow to the
system, so i get called every time a window is created. If i use the
above solution i have to look lets say every 500 ms if that window is
present. This costs some performance...
There should be a possibility to hook into the window creation somehow
right?
 
Nicholas Paldino said:
I agree you will need P/Invoke to do this, and I agree with the call to
FindWindow. However, I think that once the button is found, you should
call SendInput instead of SendMessage to synthesize the click event. This
way, the proper sequence of messages is generated. You just have to find
a region within the button to send the mouse click to.

PostMessage with WM_NOTIFY, BN_CLICKED is best of all.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DeveloperX said:
Hello

i need some hints on how to implement a small app that does click an
"Ok" button on a form that comes up from one other app once every 10
minutes.

Something like "Push the freaking button".

Any ideas where to get some information for this?

_thanks rene

You need to use PInvokes.

You'll need

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter, string className, IntPtr windowTitle);

[DllImport("user32.dll")]
private static extern bool PostMessage(
IntPtr hWnd, // handle to destination window
UInt32 Msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]


That lot, then the process is basically find the window by name
(assuming it has a name)

window=FindWindow(null, "Popup box")

Then you have to find the control and send it a message, the following
is a bit scrubby, it's out of an application I wrote a while ago, but
it should give you the idea. In this case you can see I was trying to
drive a VB6 application thus the reference to ThunderRT6CommandButton.

int hWnd =
(int)FindWindowEx((IntPtr)int.Parse(txt1.Text)
,IntPtr.Zero,"ThunderRT6CommandButton",null);//"Text1");
hWnd = (int)FindWindowEx((IntPtr)int.Parse(txt1.Text) ,(IntPtr)
hWnd,"ThunderRT6CommandButton",null);//"Text1");
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONDOWN,0,0).ToString();
txt4.Text=PostMessage((IntPtr)hWnd,
(UInt32)WindowsMessages.WM_LBUTTONUP,0,0).ToString();


That's not a complete solution, but it should point you in the right
direction.
 
ReneMarxis said:
Hello to all

many thanks for your answers :)

But i missed out a point very sorry: I did that thing already in a c++
application, so i did know that already (although i did not try it
on .Net)
What i originally wanted to do is to "subscribe" somehow to the
system, so i get called every time a window is created. If i use the
above solution i have to look lets say every 500 ms if that window is
present. This costs some performance...
There should be a possibility to hook into the window creation somehow
right?

There are CBT hooks, but they are even more of a performance hit :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top