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.
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.
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.
Related
As explained here I have built a sample console application in which I change the culture in the Main function. It works well.
Now, building another sample winform application, I wonder where would be the best place to put this
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("en-US");
Thread.CurrentThread.CurrentUICulture=new CultureInfo("en-US");
If I put this in the constructor of a Form1, will this apply to all operations in this class?
What if this Form1 opens another form Form2 and an exception occurs there?
What if Form1 uses another class Class1?
I am willing to experiment in this, but if someone has an answer based on previous knowledge, I appreciate to hear it.
For a winforms application put things that deal with the context of the entire application in program.cs and be done with it.
If I put this in the constructor of a Form1, will this apply to all operations in this class?
What if this Form1 opens another form Form2 and an exception occurs there?
What if Form1 uses another class Class1?
All your other concerns are irrelevant, and are sereverly overthinking the problem.
Note : The best way to learn programming windows or any other language is to read as much as you can. Microsoft have everything documented always go straight to the source.
Some additional reading for you
Thread.CurrentUICulture Property
Thread.CurrentCulture Property
Initialization code in a WinForms App - Program.cs or MainForm?
Sorry, this more than likely has been asked at some point but I'm not even sure what I should be searching on...
I have a winform application with multiple forms. Up until this point having a one form open at a time has been fine. But now, I have a new form that I want to add on but have the ability to keep that form open while I work in other forms. I'm not even sure what this is called but I have seen it done before in other applications.
I did find this: Run two winform windows simultaneously
But this new window is a winpipe queue viewer that runs a thread. When I try initializing using the
Application.Run(new QueueViewer());
I get the error:Starting a second message loop on a single thread is not a valid operation. Use the Form.ShowDialog instead.
The problem with that is it locks the program from doing anything else until I close that form.
Thanks for your help!
Add a form to your project (let's call it Form2). Somewhere within your code (maybe in a button click event) use the following code:
Form2 f = new Form2();
f.Show();
The Show method allows you to interact with the originating form, whereas ShowDialog prevents interaction from the original form.
I have made a small program in C# that I want to run in the background and it should only appear when a certain key combination is pressed. How can I do this?
There are at least three ways to do this:
Classic Windows Service application. "Creating a Basic Windows Service in C#" article from CodeProject will help you. In that case you use System.ServiceProcess namespace. BTW, in that case you should read "System.ServiceProcess Namespace" article from MSDN. Here is a short quote from it:
The System.ServiceProcess namespace provides classes that allow you to implement, install, and control Windows service applications. Services are long-running executables that run without a user interface.
Memory-Resident Program. But this is almost impossible to do with C#. Use C++ or better C for this purpose, if you want. If you want to search by yourself, just use keyword TSR.
Last one is a dirty one. Just create a formless C# application and try to hide it from Task Manager.
To allow the program to be completely invisible is, in my opinion, a bad idea. Because the user cannot interact with the program.
I would recommend placing it in the SysTray (an icon by the clock in Windows)
trayIcon = new NotifyIcon();
trayIcon.Text = "My application";
trayIcon.Icon = TheIcon
// Add menu to the tray icon and show it.
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
To monitor keyboard you can use LowLevel Keyboard hook ( see example ) or attach a hootkey (See example)
Create a windows form application, and delete Form1
Modify program.cs Application.Run(new Form1()); to Application.Run();
You can create a Windows Service Application. It runs as a background process. No user interface. This can also start automatically when the computer boots. You can see the rest of the background processes in Task Manager or you can type in services.msc in Command Prompt.
This might help. http://msdn.microsoft.com/en-us/library/9k985bc9%28v=vs.80%29.aspx
A quick and dirty solution (I think the Window Service Application template is unavailable in Visual Studio Express and Standard):
Start a new Windows Forms Application.
Add a new class to the solution and write the code you want inside it.
Go back to the form designer, set the WindowState property to Minimized, and add a Load event to the form. In the event handler hide the form and call your class:
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
MyNewClass mynewclass=new MyNewClass();
}
The application doesn't appear in the taskbar and you don't see it when you hit Alt+Tab. You can add a systray icon to it if you want, just like magol wrote:
NotifyIcon trayIcon = new NotifyIcon();
trayIcon.Icon=new Icon(#"C:\iconfilename.ico");
trayIcon.Visible = true;
If you really want to create a program that really run in background, try to create a Windows service.
Its there if when you create a new project
I want to show an loading image when i launch my desktop application. can anybody tell me how to show this in C#? Results required as happens in visual studio 2005/2008 when we launch this application.
What you are looking for is actually a splash screen. Look at this codeproject article. It explains really well how you should do this.
http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx
What i did in my previous project was, that make the Form1 (display form) and make it wait for a while, then close it after that launch the Form2 which is the actual application.
But that was way back when i was a fresher. Now what i might do is, have a Form1 which ill launch and in the backgroundworker do all major UI processing work such as if any loading of data needs to be done, etc stuffs and keep every thing ready in memory. Then ill close the Form1 or make it hide or invisible and launch the Form2 which is the main app form and pass all the information required to it.
after i use process.start() a form1 appears on the desktop. When i click the form2 button from form1 a new form appears. how can i postmessage in form2? can someone please give me an example? I think i need to use FindWindow, but i don't know what to write as parameters. thx
Questions:
is there a way i can search in my application the elements from the form1 list (which appears after i run process.start in my app) and print them?
is there a way i can double click programatically an element from form1 - which automatically generates form2?
is there a way to postmessage in form2?
thx
i am using user32.dll
The White framework provides all the functionality you're seeking. It is commonly used for UI testing, but it can be used for any kind of UI automation.