C#窗体全屏功能实例代码
程序员文章站
2022-06-22 16:05:32
最近有朋友让我给他弄个应用程序全屏的功能,例如银行的取号程序界面。所以我从网上查询了一些实现的方法。
c#应用程序中如何实现全屏幕显示功能?
效果就像windows自带...
最近有朋友让我给他弄个应用程序全屏的功能,例如银行的取号程序界面。所以我从网上查询了一些实现的方法。
c#应用程序中如何实现全屏幕显示功能?
效果就像windows自带的屏幕保护程序和众多的游戏那样,无论是否设置了“将任务栏保持在其他窗口的前端”都不显示任务栏
实现方式一
在网上找来一些简单的实现方式:
this.formborderstyle = formborderstyle.none; //设置窗体为无边框样式 this.windowstate = formwindowstate.maximized; //最大化窗体
然后再设置窗体的位置和大小,例如:width=1024 height=768 left=0 top=0等size的值
把以上两句代码直接放到form1_load的方法中,就可以了,比较简单,我就不贴代码了。
实现方式二
调用系统的api函数,如user32.dll中的findwindow和showwindow函数,具体代码如下:
[dllimport("user32.dll", entrypoint = "showwindow")] public static extern int32 showwindow(int32 hwnd, int32 ncmdshow); [dllimport("user32.dll", entrypoint = "findwindow")] private static extern int32 findwindow(string lpclassname, string lpwindowname);
代码如下:
using system; using system.windows.forms; using system.drawing; using system.runtime.interopservices; namespace fullscr { public partial class form1 : form { boolean m_isfullscreen = false;//标记是否全屏 public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { } /// <summary> /// 全屏按钮的click事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_click(object sender, eventargs e) { m_isfullscreen = !m_isfullscreen;//点一次全屏,再点还原。 this.suspendlayout(); if (m_isfullscreen)//全屏 ,按特定的顺序执行 { setformfullscreen(m_isfullscreen); this.formborderstyle = formborderstyle.none; this.windowstate = formwindowstate.maximized; this.activate();// } else//还原,按特定的顺序执行——窗体状态,窗体边框,设置任务栏和工作区域 { this.windowstate = formwindowstate.normal; this.formborderstyle = formborderstyle.sizable; setformfullscreen(m_isfullscreen); this.activate(); } this.resumelayout(false); } /// <summary> /// 设置全屏或这取消全屏 /// </summary> /// <param name="fullscreen">true:全屏 false:恢复</param> /// <param name="rectold">设置的时候,此参数返回原始尺寸,恢复时用此参数设置恢复</param> /// <returns>设置结果</returns> public boolean setformfullscreen(boolean fullscreen)//, ref rectangle rectold { rectangle rectold = rectangle.empty; int32 hwnd = 0; hwnd = findwindow("shell_traywnd", null);//获取任务栏的句柄 if (hwnd == 0) return false; if (fullscreen)//全屏 { showwindow(hwnd, sw_hide);//隐藏任务栏 systemparametersinfo(spi_getworkarea, 0, ref rectold, spif_updateinifile);//get屏幕范围 rectangle rectfull = screen.primaryscreen.bounds;//全屏范围 systemparametersinfo(spi_setworkarea, 0, ref rectfull, spif_updateinifile);//窗体全屏幕显示 } else//还原 { showwindow(hwnd, sw_show);//显示任务栏 systemparametersinfo(spi_setworkarea, 0, ref rectold, spif_updateinifile);//窗体还原 } return true; } #region user32.dll public const int32 spif_updateinifile = 0x1; public const int32 spi_setworkarea = 47; public const int32 spi_getworkarea = 48; public const int32 sw_show = 5; public const int32 sw_hide = 0; [dllimport("user32.dll", entrypoint = "showwindow")] public static extern int32 showwindow(int32 hwnd, int32 ncmdshow); [dllimport("user32.dll", entrypoint = "findwindow")] private static extern int32 findwindow(string lpclassname, string lpwindowname); [dllimport("user32.dll", entrypoint = "systemparametersinfo")] private static extern int32 systemparametersinfo(int32 uaction, int32 uparam, ref rectangle lpvparam, int32 fuwinini); #endregion } }
完善后的代码:
非常感谢@iheartwater的热心帮助,更改后的代码能够解决”全屏后,窗体能够恢复到原来的状态,包括位置(loaction)和大小(size)“,唉,其实,原因还挺简单的。
modified code public partial class frmfullscreen : form { boolean m_isfullscreen = false;//标记是否全屏 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;//点一次全屏,再点还原。 this.suspendlayout(); if (m_isfullscreen)//全屏 ,按特定的顺序执行 { setformfullscreen(m_isfullscreen); this.formborderstyle = formborderstyle.none; this.windowstate = formwindowstate.maximized; this.activate();// } else//还原,按特定的顺序执行——窗体状态,窗体边框,设置任务栏和工作区域 { this.windowstate = formwindowstate.normal; this.formborderstyle = formborderstyle.sizable; setformfullscreen(m_isfullscreen); this.activate(); } this.resumelayout(false); } /// <summary> /// 全屏的快捷功能,f11相当于单机按钮;esc健,如果全屏则退出全屏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnfullscreen_keydown(object sender, keyeventargs e) { if (e.keycode == keys.f11) { btnfullscreen.performclick(); e.handled = true; } else if (e.keycode == keys.escape)//esc键盘退出全屏 { if (m_isfullscreen) { e.handled = true; this.windowstate = formwindowstate.normal;//还原 this.formborderstyle = formborderstyle.sizable; setformfullscreen(false); } } } /// <summary> /// 设置全屏或这取消全屏 /// </summary> /// <param name="fullscreen">true:全屏 false:恢复</param> /// <param name="rectold">设置的时候,此参数返回原始尺寸,恢复时用此参数设置恢复</param> /// <returns>设置结果</returns> public boolean setformfullscreen(boolean fullscreen)//, ref rectangle rectold { rectangle rectold=rectangle.empty; int32 hwnd = 0; hwnd = findwindow("shell_traywnd", null);//获取任务栏的句柄 if (hwnd == 0) return false; if (fullscreen)//全屏 { showwindow(hwnd, sw_hide);//隐藏任务栏 systemparametersinfo(spi_getworkarea, 0, ref rectold, spif_updateinifile);//get 屏幕范围 rectangle rectfull = screen.primaryscreen.bounds;//全屏范围 systemparametersinfo(spi_setworkarea, 0, ref rectfull, spif_updateinifile);//窗体全屏幕显示 } else//还原 { showwindow(hwnd, sw_show);//显示任务栏 systemparametersinfo(spi_setworkarea, 0, ref rectold, spif_updateinifile);//窗体还原 } return true; } #region user32.dll [dllimport("user32.dll", entrypoint = "showwindow")] public static extern int32 showwindow(int32 hwnd, int32 ncmdshow); public const int32 sw_show = 5; public const int32 sw_hide = 0; [dllimport("user32.dll", entrypoint = "systemparametersinfo")] private static extern int32 systemparametersinfo(int32 uaction, int32 uparam, ref rectangle lpvparam, int32 fuwinini); public const int32 spif_updateinifile = 0x1; public const int32 spi_setworkarea = 47; public const int32 spi_getworkarea = 48; [dllimport("user32.dll", entrypoint = "findwindow")] private static extern int32 findwindow(string lpclassname, string lpwindowname); #endregion }
窗体全屏
窗体全屏的方法:
隐藏任务栏、设置工作区域
窗体最大化、设置窗体边框样式
上一篇: Vue2学习结合bootstrapTable遇到的问题
下一篇: 浅析C#中文件路径的操作