Implementation code (WINFORM) for the full screen display of controls in C # _c# tutorial

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.