窗體全屏的方法
隱藏任務欄設置工作區域
窗體最大化設置窗體邊框樣式
全屏窗體代碼
public partial class FrmFullScreen : Form
{
Boolean m_IsFullScreen = false;//標記是否全屏
Rectangle rectOld = RectangleEmpty;
public FrmFullScreen()
{
InitializeComponent();
}
/// <summary>
/// 全屏按鈕的Click事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFullScreen_Click(object sender EventArgs e)
{
m_IsFullScreen = !m_IsFullScreen;//點一次全屏再點還原
SetFormFullScreen(m_IsFullScreen);
thisSuspendLayout();
if (m_IsFullScreen)//全屏
{
thisWindowState = FormWindowStateMaximized;
thisFormBorderStyle = FormBorderStyleNone;
}
else//還原 TODO:還原後的窗體應該與全屏前的大小一致
{
thisWindowState = FormWindowStateNormal;
thisFormBorderStyle = FormBorderStyleSizable;
}
thisResumeLayout(false);
}
/// <summary>
/// 全屏的快捷功能F相當於單機按鈕Esc健如果全屏則退出全屏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFullScreen_KeyDown(object sender KeyEventArgs e)
{
if (eKeyCode == KeysF)
{
btnFullScreenPerformClick();
eHandled = true;
}
else if (eKeyCode == KeysEscape)//esc鍵盤退出全屏
{
if (m_IsFullScreen)
{
eHandled = true;
SetFormFullScreen(false);
thisWindowState = FormWindowStateNormal;//還原
thisFormBorderStyle = FormBorderStyleSizable;
}
}
}
/// <summary>
/// 設置全屏或這取消全屏
/// </summary>
/// <param name="fullscreen">true:全屏 false:恢復</param>
/// <param name="rectOld">設置的時候此參數返回原始尺寸恢復時用此參數設置恢復</param>
/// <returns>設置結果</returns>
public Boolean SetFormFullScreen(Boolean fullscreen)// ref Rectangle rectOld
{
Rectangle rectOld=RectangleEmpty;
Int hwnd = ;
hwnd = FindWindow("Shell_TrayWnd" null);//獲取任務欄的句柄
if (hwnd == ) return false;
if (fullscreen)//全屏
{
ShowWindow(hwnd SW_HIDE);//隱藏任務欄
SystemParametersInfo(SPI_GETWORKAREA ref rectOld SPIF_UPDATEINIFILE);//get 屏幕范圍
Rectangle rectFull = ScreenPrimaryScreenBounds;//全屏范圍
SystemParametersInfo(SPI_SETWORKAREA ref rectFull SPIF_UPDATEINIFILE);//窗體全屏幕顯示
}
else//還原
{
ShowWindow(hwnd SW_SHOW);//顯示任務欄
SystemParametersInfo(SPI_SETWORKAREA ref rectOld SPIF_UPDATEINIFILE);//窗體還原
}
return true;
}
#region userdll
[DllImport("userdll" EntryPoint = "ShowWindow")]
public static extern Int ShowWindow(Int hwnd Int nCmdShow);
public const Int SW_SHOW = ; public const Int SW_HIDE = ;
[DllImport("userdll" EntryPoint = "SystemParametersInfo")]
private static extern Int SystemParametersInfo(Int uAction Int uParam ref Rectangle lpvParam Int fuWinIni);
public const Int SPIF_UPDATEINIFILE = x;
public const Int SPI_SETWORKAREA = ;
public const Int SPI_GETWORKAREA = ;
[DllImport("userdll" EntryPoint = "FindWindow")]
private static extern Int FindWindow(string lpClassName string lpWindowName);
#endregion
}
From:http://tw.wingwit.com/Article/program/net/201311/14040.html