How to minimize forms that belong to a particular application, when the parent application (In my case Autodesk Inventor) is minimized ? When you create software or addins, you would want the forms/dialogs to be minimized and not floating around when parent application is minimized.
For that we need to deal with hWnd of parent application. “hWnd” stands for Window Handle, which is the API call to the window(parent application). Since we come across this too often in Inventor customization to make addins, I have created a Class and a couple of methods for better clarity. Before I go into details, lets see what are the different types of Forms/Dialogs you would deal while developing Windows based software.
The modal forms are used when you want the user to enter some values and unless the form/dialog is closed, he/she cannot interact with other controls in the application. All the MessageBox’s are of modal types. The image on the left is also an example of Modal forms. Here, the user has to enter/select details of Graph plots in our addin IN-Motion.
2. Modeless Form
The modeless forms are used when user can enter values into the form and also can interact with other controls in application, even when the form is not minimized. The image on the left is the Simulation playback deck in IN-Motion.
Coming back to our problem of minimizing forms with parent application, below is the code. If the user minimizes Inventor application(parent form), its child forms are also minimized.
[sourcecode language=’c#’]
//
//Declare and set..here m_inventorApplication is the application
//MainFrameHWND returns its handle.
//WindowsWrapperForForm is a Class, defined at the bottom
WindowsWrapperForForm m_windowsWrapperForForm = new
WindowsWrapperForForm((IntPtr)m_inventorApplication.MainFrameHWND);
//Declare and set a form .. ModalCmdDlg is our modal form
ModalCmdDlg m_modalCmdDlg = new ModalCmdDlg();
//Declare and set a form .. ModelessCmdDlg is our modeless form
ModelessCmdDlg m_modelessCmdDlg = new ModelessCmdDlg();
//Show Modal form
ShowModalForm(m_modalCmdDlg);
//Show Modeless form
ShowModelessForm(m_modelessCmdDlg);
//Methods
private void ShowModalForm(Form _modalCmdDlg)
{
_modalCmdDlg.Activate();
_modalCmdDlg.ShowInTaskbar = false;
//ShowDialog is used..for Modal forms
_modalCmdDlg.ShowDialog(m_windowsWrapperForForm);
}
private void ShowModelessForm(Form _modelessCmdDlg)
{
_modelessCmdDlg.Activate();
_modelessCmdDlg.ShowInTaskbar = false;
//Show is used..for Modeless forms
_modelessCmdDlg.Show(m_windowsWrapperForForm);
}
//Below is the code for Class WindowsWrapperForForm
//****************Class***************
class WindowsWrapperForForm : System.Windows.Forms.IWin32Window
{
private IntPtr m_hwnd;
public WindowsWrapperForForm(IntPtr handle)
{
m_hwnd = handle;
}
#region IWin32Window Members
public IntPtr Handle
{
get { return m_hwnd; }
}
#endregion
}
//****************EndClass*******************
[/sourcecode]