How to stretch a panel on fullscreen?
I have a WinForms application.
Application has menus开发者_StackOverflow社区trip, toolstrip and several panels.
I want to stretch one of that panels on fullscreen. I want that panel covers all screen include taskbar.
How do I do it ?
============================================
I used an answer of Hans Passant:
public partial class Form1 : Form
{
Size _panel1Size;
public Form1()
{
InitializeComponent();
_panel1Size = panel1.Size;
}
void bFullScreen_Click(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.panel1.Size = this.ClientSize;
}
void bGoBack_Click(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.WindowState = FormWindowState.Normal;
panel1.Size = _panel1Size;
}
}
Getting a form to cover the taskbar requires it to be borderless. You'll need to detect the window state change in the OnResize event. Something like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
panel1Size = panel1.Size;
prevState = this.WindowState;
}
private Size panel1Size;
private FormWindowState prevState;
protected override void OnResize(EventArgs e) {
if (prevState != this.WindowState) {
prevState = this.WindowState;
if (this.WindowState == FormWindowState.Maximized) {
this.FormBorderStyle = FormBorderStyle.None;
panel1.Size = this.ClientSize;
}
else if (this.WindowState == FormWindowState.Normal) {
this.FormBorderStyle = FormBorderStyle.Sizable;
panel1.Size = panel1Size;
}
}
base.OnResize(e);
}
private void button1_Click(object sender, EventArgs e) {
this.WindowState = FormWindowState.Normal;
}
}
There's a flaw, it won't restore to the exact same size. No easy fix for that.
On Form's Load
event add:
MyPanel.Size = this.Size;
MyPanel.Location = this.Location;
This should stretch your panel to full screen.
精彩评论