1. Use the WINAPI "SetParent" interface:
Copy Code code as follows:
[DllImport ("user32.dll", SetLastError = True)]
static extern IntPtr setparent (IntPtr hwndchild, IntPtr hwndnewparent);
Copy Code code 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);
Execution of the above code, our control can be full screen, but there is a small problem, we should provide a function, let the user press a key, exit full screen, or shut down, to who is more depressed. You should add related events to the control at this time, and then return it after the key is fetched. Let's take the ESC key for example:
Copy Code code as follows:
private void Addeventkeyup (control control) {
if (control!= null) {
Control. KeyUp + = new Keyeventhandler (control_keyup);
foreach (Control C-control. Controls) {//need to be added to the child control, otherwise it may not be available.
Addeventkeyup (c);
}
}
}
void Control_keyup (object sender, KeyEventArgs e) {
if (E.keycode = = Keys.escape) {
if (control!= null) {
SetParent (Control. Handle, the original parent. Handle);
Control. Dock = DockStyle.Fill;
}
}
}
The modified code is as follows:
Copy Code code 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 ()///get focused, or not get keys
SetParent (Control. Handle, IntPtr.Zero);
2. Create a new window and set FormBorderStyle to None,windowstate to Maximized,topmost true. Then the specific code is as follows:
Copy Code code as follows:
Addeventkeyup (Control);
The original parent. Controls.clear ();
Frmfullscreen frm = new Frmfullscreen ();
frm. Controls.Add (Control);
frm. ShowDialog ();
Copy Code code as follows:
private void Addeventkeyup (control control) {
if (control!= null) {
Control. KeyUp + = new Keyeventhandler (control_keyup);
foreach (Control C-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);//This cannot be the same as the following close order, or it will cause errors, because the controls were destroyed after close.
frm. Close ();
frm = null;
}
}
}
}
After the actual use of validation, the second method is good, there is no problem. Just need to open one more window. The first method has a little problem, that is, if the control has a right-click menu or something, a call will run to the main interface. It seems that the mouse is sometimes not very good.
Author: Xiarong
E-mail: Lyout (at) 163.com