Recently in a small project to develop a small program with the use of other software, one of the software in the use of work needs to generate reports, there is no other application interface on the display desktop display, it is necessary to automatically hide and load the form, by reading Windows The API enables this to be shared with everyone: The original C # comes with the Hide () and show () method, but the form is defined as a static type when the project is developed, and its corresponding hidden and display form methods cannot be invoked through the This keyword. This is done here through the functions provided by the Windows API, first to get a handle to the currently running form, as follows:
Define a global variable
IntPtr Handle;
[DllImport ("user32.dll", EntryPoint = "FindWindow")]
Private extern static IntPtr FindWindow (string lpclassname, string lpwindowname);
Handle = FindWindow (null, "form name"),//lpwindowname for form name
Next define the constants
Private Const int sw_hide = 0//hidden
Private Const int sw_restore = 9//display
Import Win32 Reference
[DllImport ("User32.dll")]
private static extern bool Showwindowasync (IntPtr hWnd, int ncmdshow);
[DllImport ("User32.dll")]
public static extern int ShowWindow (int hwnd, int ncmdshow);
function can be implemented by calling the method with the global variable handle
Showwindowasync (handle,sw_hide);//Implement Auto-hide
ShowWindow (Handle,sw_restore);//Auto Popup form
Questions about the C # form auto-hide and load