1. Use the winapi "SetParent" interface:
Copy codeThe Code is as follows:
[DllImport ("user32.dll", SetLastError = true)]
Static extern IntPtr SetParent (IntPtr hWndChild, IntPtr hWndNewParent );
Copy codeThe Code is as follows:
Control. Dock = DockStyle. None;
Control. Left = 0;
Control. Top = 0;
Control. Width = Screen. PrimaryScreen. Bounds. Width;
Control. Height = Screen. PrimaryScreen. WorkingArea. Height;
SetParent (control. Handle, IntPtr. Zero );
After executing the above Code, our control can be displayed in full screen, but there is still a small problem. We should provide another function to let the user press a key and exit the full screen, otherwise, it would be depressing to anyone. At this time, you should add related events to the control and get the key and return it. Take the Esc key as an example:
Copy codeThe Code is as follows:
Private void AddEventKeyUp (Control control ){
If (control! = Null ){
Control. KeyUp + = new KeyEventHandler (control_KeyUp );
Foreach (Control c in control. Controls) {// you need to add the child Control. Otherwise, the Child control may not be retrieved.
AddEventKeyUp (c );
}
}
}
Void control_KeyUp (object sender, KeyEventArgs e ){
If (e. KeyCode = Keys. Escape ){
If (control! = Null ){
SetParent (control. Handle, original parent. Handle );
Control. Dock = DockStyle. Fill;
}
}
}
The modified code is as follows:
Copy codeThe Code is as follows:
Control. Dock = DockStyle. None;
Control. Left = 0;
Control. Top = 0;
Control. Width = Screen. PrimaryScreen. Bounds. Width;
Control. Height = Screen. PrimaryScreen. WorkingArea. Height;
AddEventKeyUp (control );
Control. Focus (); // obtain the Focus. Otherwise, no keys are obtained.
SetParent (control. Handle, IntPtr. Zero );
2. Create a new window, set FormBorderStyle to None, WindowState to Maximized, and TopMost to True. The Code is as follows:
Copy codeThe Code is as follows:
AddEventKeyUp (control );
Original parent. Controls. Clear ();
FrmFullscreen frm = new frmFullscreen ();
Frm. Controls. Add (control );
Frm. ShowDialog ();
Copy codeThe Code is as follows:
Private void AddEventKeyUp (Control control ){
If (control! = Null ){
Control. KeyUp + = new KeyEventHandler (control_KeyUp );
Foreach (Control c in control. Controls ){
AddEventKeyUp (c );
}
}
}
Void control_KeyUp (object sender, KeyEventArgs e ){
If (e. KeyCode = Keys. Escape ){
If (control! = Null ){
If (frm! = Null ){
Frm. Controls. Clear ();
The original parent. Controls. Add (control); // here it cannot be in the wrong order with the following Close, or else it will cause an error because the control is destroyed after Close.
Frm. Close ();
Frm = null;
}
}
}
}
After actual use, the second method is good and there is no problem. You only need to open one more window. The first method has a small problem, that is, if the control has a right-click menu or something, a call will go to the main interface. It seems that the mouse is sometimes not very clever.
Author: Xia Rongquan
Email: lyout (at) 163.com