Code for making the control display in full screen in C #

Source: Internet
Author: User

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

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.