Is it possible to open a WPF Application from a C# Windows button click event?
You can launch it like any other applicaiton. Use the Process.Start method. If you need more control, you can create an instance of the Process class (Process process = new Process())and adjust its properties. You can see the Process class's members here.
Absolutely. You should look into System.Diagnostics.Process.
Related
I am trying to develope a windows application to display console window as output.
If i changed output type in properties to "console Application" both the console window and form showing. But i need to show only form window first, when i click a button then only the console window need to display the output value.
Please guide me...
Thanks in advance.
Each process can have only at most one console associated with it. There are several paths you can take: either you hide the console window immediately on startup and show it later (via ShowWindow), or you leave the project as a window application, and create the console manually using AllocConsole later.
In the case you wanted multiple consoles, you can e.g. create a dummy process (cmd) and attach your process to its console, using AttachConsole. The managed way would be to use remoting or other techniques to communicate with the dummy process (with your own implementation), and print texts through that.
Use NativeMethods.AllocConsole(); to allocate a console and I/O with it.
Use NativeMethods.FreeConsole(); to close and free this console.
You wanna output the Console.WriteLines of a Windows Forms app inside the form?
In that case just use Console.SetOut(Stream) and pass in something you're wrapping so you can also output it somewhere else.
Another option is to switch all your Console.WriteLines to Trace.WriteLine and add a TraceListener somewhere that does what you want.
I am using
System.Diagnostics.Process.Start(ProcessInfo);
to open a TEXT file in notepad from within my windows form application.
Detailed code snippet is
ProcessStartInfo PSI = new ProcessStartInfo("notepad.exe", LogFile);
PSI.WindowStyle = ProcessWindowStyle.Normal;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
However, it opens notepad on the default desktop but not on the extended desktop on which the main application is running.
Now, the question is, how to open notepad on the active desktop i.e. Window on which the current application is running?
Other that specifying the initial window state (normal, hidden, etc), you have basically no control over how the newly launched application starts up and where it shows itself.
The best bet here is to launch the application, then use its window handle to tell it to move. This all requires using P/Invoke, to call MoveWindow. The C# signatures for all of those functions are on pinvoke.net.
Here's a very simple (VB.NET) example that shows the basic idea. In this case, as #Lloyd points out, the window handle you need is the Process.MainWindowHandle you get back from Process.Start. Note that Process.MainWindowHandle isn't filled in immediately; you typically need to call WaitForInputIdle to make sure the window actually gets created. If MainWindowHandle is 0 then you'll know it's too soon.
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 need to register my WPF application as a shell app in order to restrict some functionalities in my standard WPF C# application.
Some examples of functinalities to be restricted: Keystrokes (Alt +F4, Ctrl+Alt+Del etc.), Mouse (Right click etc.)
How can this be done?
You can't restrict Ctrl+Alt+Del with a shell app, but you can do that by changing policy settings.
You can do this in C# by using WMI. Take a look at WMI Code Creator.
As for Alt+F4 and Mouse events, if they are only within your application, again you don't need shell extensions. You can just handle your main window OnClose event and return Cancel.
I have a process that was written by WPF and I want to start it in my program. I used "process.start()" and its worked successfully. But now I want to start my process as a child of a form.
Do you mean you have a wpf usercontrol/form, and you want to display that in your winforms application?
If so, see this link: http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms
You can't start process as a child of a Form! Process is just another application that is running on the computer no matter if it was build in windows forms or wpf or even assembly language.
You may mean starting new form as a child from main form?
using (ChildForm form = new ChildForm())
form.ShowDialog(this); // this refers to main form object