1. Use the WINAPI "SetParent" interface: <span class="Apple-converted-space"></span>
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, for 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 <span class="Apple-converted-space"></span> :
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) { <span class="Apple-converted-space"></span>
if (control!= null) {
setparent (Control. Handle, the original parent. Handle);
control. Dock = DockStyle.Fill;
}
}
}
the modified code is as follows: <span class="Apple-converted-space"></span>
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 ();//Get the spotlight, or you won't get the <span class="Apple-converted-space"></span> key
setparent (Control. Handle, IntPtr.Zero);
2. Create a new window and set FormBorderStyle to be None,windowstate maximized,topmost to true. Then the specific code is as <span class="Apple-converted-space"></span> 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) { <span class="Apple-converted-space"></span>
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