How do I open an executable as a MDI child? - c#

I have a parent form that starts another application. When the application starts it should set the application as an mdichild of parent.
I have managed to get this to work with notepad using the code below.
Process proc = new Process();
proc.StartInfo.FileName = "notepad.exe";
proc.Start();
proc.WaitForInputIdle();
SetParent(proc.MainWindowHandle, this.panel.Handle);
The problem is that it doesn't work with all applications. Specifically it doesn't work with other win forms applications developed in c#, which is what I need it for.
Any help is appreciated, Thanks.
Edit: I have access to the code for the c# app i want to open as a mdi child.

OP: I have access to the code for the c# app i want to open as a mdi
child.
To open a form of other .Net windows application, you don't need to use SetParent and you can simply add a reference of that .Net Application (dll, exe or project) to your current project and then create an instance of the main form of that application and show it like other forms of your application.
If you have access to project of that application, you can add it to your current solution and use add a reference to your current project.
If you don't have access to project of the project of that application, you can add a reference to its exe/dll to your current project.
To learn more about add reference, read How to: Add or Remove References By Using the Add Reference Dialog Box

add .exe(for example another.exe) file as reference
add another.exe references // may be differ from your mdi form
add below code
another.Form1 fr = new another.Form1(); //another.exe has Form1 if not select form.
fr.MdiParent = this;
fr.Show(); // Display the new form.

Related

Developing C# Visual Studio .exes similar to Java [duplicate]

This question already has answers here:
How do I create a C# app that decides itself whether to show as a console or windowed app?
(11 answers)
Closed 5 years ago.
I've come from developing in Java to developing in C# in Visual Studio. I'm very comfortable with the change apart from one thing: The way in which I can write hidden or GUI applications.
I used to use Swing for developing my GUI based applications in Java.
What i'd like to do is develop an application in the style of a Console Application with static void Main(string[] args){} however I want to be able to create windows and tell them to be visible/open using a method similar to jFrameObject.setVisible(bool). What I want is a console style application which I can use WPF windows and their components in, and i'd like to be able to make those windows in the window designer built in to Visual Studio 2017.
If at all possible, it would be nice if I had an option to show/hide the console too. I have tried a few different methods to do this, like this, however the console which is shown isn't linked to the C# Console object so doing Console.WriteLine(string); outputs to the debug console in Visual Studio 2017 and not the console which was opened in a window.
Thank you for any help given!
Theres no special thing if you want to open a WPF-window (even in a Console Project).
You can simply add a WPF Form using the Visual Studio Utilities (New/Window or something like this).
To open your window, you have two possibilities:
The first one is to open a dialog modal (the code doesn't continue until the form ist closed)
MyWindow window = new MyWindow();
window.ShowModal();
The second solution is open the window without waiting for the dialog to close(some kind like asynchronous)
MyWindow window = new MyWindow();
window.Show();
You can find further information on this article..
Enjoy programming in C# instead of Java.
Starting out, you can use Windows Forms which enables you to easily add GUI elements to an application, e.g. in your Console Application's Main():
var form = new System.Windows.Forms.Form();
form.Text = "Window Title";
var button = new System.Windows.Forms.Button();
button.Text = "OK";
form.Controls.Add(button);
form.ShowDialog();
This requires you to add a reference to the [System.Windows.Forms] assembly.
As for the console window [question]: Show/Hide the console window of a C# console application
If you want to do WPF, add a class representing some window:
public class SomeWindow : System.Windows.Window
{
public SomeWindow()
{
this.AddChild(new System.Windows.Controls.Button() { Content = "OK", Height = 30, Width = 100 });
}
}
Then use the application object to run the window:
new System.Windows.Application().Run(new SomeWindow());
The Main needs to be marked with the [STAThreadAttribute]. The assemblies PresentationCore, PresentationFramework and WindowsBase needs to be referenced.
You can start out with a Console application and add references to WinForms or WPF assemblies to open windows.
Alternatively you can start with a WinForms or WPF application and open a console using the Win32 API: How do I create a C# app that decides itself whether to show as a console or windowed app?

Get Application that runs a Windows Form Control

I'm making an Autocad Plugin which runs fine using Windows Forms
And I've created a user control (winforms) to replicate in any form I like in the future.
The question is
From the control's code, how do I get the instance of the application running that control?
(Probably a pure winforms problem)
Coditions:
Within the plug-in I can get the Autocad Application instance with no
problem.
This user control is meant to be in a separate assembly (dll) to be referenced in the plug-in application, so it has no direct access to the application instance.
A little explanation about the scenario:
There's a Main Assembly being run by Autocad as a plug-in.
That assembly has the Autocad application instantiated.
Now I have some useful form controls to work with Autocad, and they are in a separate assembly. (That's because I want to use them in as many different plug-ins I like).
So, Autocad runs main assembly, and main assembly runs controls of the separate assembly.
In order to work properly, those controls need to have access to the Autocad application wich is running the main assembly.
Today I use the application as a property in the controls, wich I must set before working with them. (If I forget to set that, exceptions are raised). Since I cannot have a control with a creator taking parameters.
I want the controls to detect their running application so I avoid that workaround.
Please see the following code
public class MyCommands {
[CommandMethod("NS", "TEST", "TEST", CommandFlags.Modal)]
public void TestCommand() // This method can have any name
{
Form fromAutoCADAPI = new TestForm();
Form independent1 = new TestForm();
Form independent2 = new TestForm();
//Using AutoCAD application
Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(fromAutoCADAPI);
independent1.Show();
independent2.Show();
//Using Windows Forms Application
var count = System.Windows.Forms.Application.OpenForms.Count; //should be 3
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(count.ToString());
}
}
If this is what you already know, then may be you should paste some sample code that will help understand where specifically you are stuck in your code. This is the way I have used AutoCAD application and Windows Forms application. In case you wan't to grab WPF application then you can use
var application = System.Windows.Application.Current;

Develop a program that runs in the background in .NET?

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

How can I start a process as a child of a winform in my C# program?

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

SetParent() on a VB6 application from a C# app

I am trying to use the win32 API to set the parent of an application to a panel in my C# application.
When I inspect it using Spy++ the application loads in 2 main forms ThunderRT6Main and ThunderRT6MDIForm both with the same title.
I have found the handle of both of these and tried to call SetParent on them both but the window does nothing and does not move anywhere...
Is there something funky I need to do when invoking SetParent() on a VB6 app?
ThunderRT6Main is the hidden owner of every top level form in VB6. It sets the application icon, the one you see in the Applications tab of Task Manager. You can read this interesting article
A window can have a parent or an owner but not both
Basicly you might need WS_CHILD set before calling SetParent.

Categories