How to make Windows Form app truly Full Screen (and to hide Taskbar) in C#? 转
2010-04-26 10:44
716 查看
One of sound-like-simple questions is “how to make your application truly Full Screen” i.e. not showing Taskbar or anything like that.
Initial approach is obvious:
[/code]
Does it work? Well, sort of. If your Taskbar have default setting unchecked for “Keep the taskbar on top of other windows”, this will present your application in all it’s glory all over screen estate.
However, if the Taskbar is set to appear on top of all others, this won’t help – your application won’t cover it.
Let’s go further – next step is to use P/Invoke and to engage Win32 API services. There is easy way to hide particular window. So, find the Taskbar and hide it:
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
int hWnd = FindWindow("Shell_TrayWnd", "");
ShowWindow(hWnd, SW_HIDE);
targetForm.WindowState = FormWindowState.Maximized; targetForm.FormBorderStyle = FormBorderStyle.None; targetForm.TopMost = true;
[/code]
(you need to add
Is this better? In theory yes – Taskbar is hidden, but your application still does not occupy whole screen – place where Taskbar was is not used.
Real and proven solution is to make request to WinAPI that your form take whole screen estate – Taskbar will hide itself in that case. Full information about that can be found in KB Article Q179363: How To Cover the Task Bar with a Window and here is the code:
public class WinApi
{
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int which);
[DllImport("user32.dll")]
public static extern void
SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int X, int Y, int width, int height, uint flags);
private const int SM_CXSCREEN = 0;
private const int SM_CYSCREEN = 1;
private static IntPtr HWND_TOP = IntPtr.Zero;
private const int SWP_SHOWWINDOW = 64; // 0x0040
public static int ScreenX
{
get { return GetSystemMetrics(SM_CXSCREEN);}
}
public static int ScreenY
{
get { return GetSystemMetrics(SM_CYSCREEN);}
}
public static void SetWinFullScreen(IntPtr hwnd)
{
SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
}
}
/// <summary>
/// Class used to preserve / restore state of the form
/// </summary>
public class FormState
{
private FormWindowState winState;
private FormBorderStyle brdStyle;
private bool topMost;
private Rectangle bounds;
private bool IsMaximized = false;
public void Maximize(Form targetForm)
{
if (!IsMaximized)
{
IsMaximized = true;
Save(targetForm);targetForm.WindowState = FormWindowState.Maximized; targetForm.FormBorderStyle = FormBorderStyle.None; targetForm.TopMost = true;WinApi.SetWinFullScreen(targetForm.Handle);
}
}
public void Save(Form targetForm)
{
winState = targetForm.WindowState;
brdStyle = targetForm.FormBorderStyle;
topMost = targetForm.TopMost;
bounds = targetForm.Bounds;
}
public void Restore(Form targetForm)
{
targetForm.WindowState = winState;
targetForm.FormBorderStyle = brdStyle;
targetForm.TopMost = topMost;
targetForm.Bounds = bounds;
IsMaximized = false;
}
}
[/code]
Code for example application is here: MaxWinForm.zip
Initial approach is obvious:
targetForm.WindowState = FormWindowState.Maximized; targetForm.FormBorderStyle = FormBorderStyle.None; targetForm.TopMost = true;
[/code]
Does it work? Well, sort of. If your Taskbar have default setting unchecked for “Keep the taskbar on top of other windows”, this will present your application in all it’s glory all over screen estate.
However, if the Taskbar is set to appear on top of all others, this won’t help – your application won’t cover it.
Let’s go further – next step is to use P/Invoke and to engage Win32 API services. There is easy way to hide particular window. So, find the Taskbar and hide it:
private const int SW_HIDE = 0; private const int SW_SHOW = 1;
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
int hWnd = FindWindow("Shell_TrayWnd", "");
ShowWindow(hWnd, SW_HIDE);
targetForm.WindowState = FormWindowState.Maximized; targetForm.FormBorderStyle = FormBorderStyle.None; targetForm.TopMost = true;
[/code]
(you need to add
using System.Runtime.InteropServices;)
Is this better? In theory yes – Taskbar is hidden, but your application still does not occupy whole screen – place where Taskbar was is not used.
Real and proven solution is to make request to WinAPI that your form take whole screen estate – Taskbar will hide itself in that case. Full information about that can be found in KB Article Q179363: How To Cover the Task Bar with a Window and here is the code:
/// <summary> /// Selected Win AI Function Calls /// </summary>
public class WinApi
{
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int which);
[DllImport("user32.dll")]
public static extern void
SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int X, int Y, int width, int height, uint flags);
private const int SM_CXSCREEN = 0;
private const int SM_CYSCREEN = 1;
private static IntPtr HWND_TOP = IntPtr.Zero;
private const int SWP_SHOWWINDOW = 64; // 0x0040
public static int ScreenX
{
get { return GetSystemMetrics(SM_CXSCREEN);}
}
public static int ScreenY
{
get { return GetSystemMetrics(SM_CYSCREEN);}
}
public static void SetWinFullScreen(IntPtr hwnd)
{
SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
}
}
/// <summary>
/// Class used to preserve / restore state of the form
/// </summary>
public class FormState
{
private FormWindowState winState;
private FormBorderStyle brdStyle;
private bool topMost;
private Rectangle bounds;
private bool IsMaximized = false;
public void Maximize(Form targetForm)
{
if (!IsMaximized)
{
IsMaximized = true;
Save(targetForm);targetForm.WindowState = FormWindowState.Maximized; targetForm.FormBorderStyle = FormBorderStyle.None; targetForm.TopMost = true;WinApi.SetWinFullScreen(targetForm.Handle);
}
}
public void Save(Form targetForm)
{
winState = targetForm.WindowState;
brdStyle = targetForm.FormBorderStyle;
topMost = targetForm.TopMost;
bounds = targetForm.Bounds;
}
public void Restore(Form targetForm)
{
targetForm.WindowState = winState;
targetForm.FormBorderStyle = brdStyle;
targetForm.TopMost = topMost;
targetForm.Bounds = bounds;
IsMaximized = false;
}
}
[/code]
Code for example application is here: MaxWinForm.zip
相关文章推荐
- Fix Unable to Pin App to Taskbar and Start Menu Error in Windows 7
- How to create (and deploy) a windows service in C# ?
- How to "enalbe mouse integration" and make screen display fully in Oracle VM VirtualBox in Ubuntu
- New feature in C# 3.0 - aims to make data programmable in a general purpose and truly object oriented syntax
- View and Data API Tips: how to make viewer full screen
- How to make BBED(Oracle Block Brower and EDitor Tool) on Unix/Linux/Windows
- [android] how to hide system bar and navigation bar in android
- how to make Completion Handlers and use it in Swift
- How to Integrate Excel in a Windows Form Application using the WebBrowser
- How to close (auto hide) WPF window after 10 sec using a timer in C#?
- How to compile and run a simple java file in jenkins on Windows
- How to Make an Activity Fullscreen
- How to use Pageheap.exe in Windows XP, Windows 2000, and Windows Server 2003
- How to create a DLL library in C and then use it with C#
- How to make BBED(Oracle Block Brower and EDitor Tool) on Unix/Linux/Windows
- How to get Intellisense for Web.config and App.config in Visual Studio .NET?(转载)
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- How To Reset Win+PrtScr Screenshot Counter in Windows 10 | 8.1
- How to Add Splash Screen in Your iOS App
- 【转】How to call c++ exported method and classes in c#