tags:

views:

28

answers:

2

Hey, I have been searching on google and I cant seem to find anything about targeting different windows in c#, I am using Visual studio 2010.

I'm not sure how to do this but I'm pritty sure it can be done, does anyone know where I can read up about it?

I need to be able to target a different program (like notpad for example), and simulate a key press.

Thanks.

+2  A: 

If you mean interacting with different windows (possibly part of a different process), typically you would get a window handle (can be done in many ways), and then you can send messages and get data from those messages to those window handles.

For example see SendMessage which you would p/invoke from your C# app.

If you want to get updates on when certain events happen in those windows then you can use Windows Hooks.

Brian R. Bondy
Yeah, I mean something else running on the PC, I just need to target it.
Crazyd22
@Crazyd22: As per my answer above. you need to be more specific as to what you want to do with that window if you want a more specific answer though.
Brian R. Bondy
Basically, when they click a button, I want it to target another window (for example notepad), and write some text in (simulate a Key press).
Crazyd22
Yes so you want to use SendMessage and also look into the messages WM_SETFOCUS and WM_KEYDOWN or SendKeys. See my answer here and the example code in the question: http://stackoverflow.com/questions/2604898/net-sendkeys-to-calculator/2604919#2604919
Brian R. Bondy
A: 

I'm going to assume you're on WinForms (same applies for WPF, slightly different code though).

In your project you have

Form1
Form2
Form3

At the top of your Form1 class, you define this code:

Form2 frm2;
Form3 frm3;

Assuming Form1 is your startup object you add buttons for "Show Form2" and "Show Form3"

In their respective code-behind you add code like this

frm2 = new Form2();
frm2.Show();

frm3 = new Form3();
frm3.Show();

Same concept applies for WPF. If you're trying to do stuff with a window outside your application, the SendMessage Windows API is probably what you need to look into.

Nate Bross
I mean a program that I do not own, something else running on the PC.
Crazyd22