夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm)

有时候需要让窗口中某一块的内容全屏显示,比如视频播放、地图等等。经过摸索,暂时发现两种可行方法,如果有谁知道其他方法,敬请告知

1.使用winapi “SetParent” 接口:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
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);

执行上面的代码后,我们的 control已经可以全屏显示了,但还有一个小问题,我们应该再提供一个功能,让用户按某个键后,退出全屏,要不然关不掉,给谁用都比较郁闷。这个时候应该给控件添加相关事件,取到键后返回。我们以Esc键为例:

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) {
SetParent(control.Handle, 原来的parent.Handle);
control.Dock = DockStyle.Fill;
}
}
}

修改后的代码如下:

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();// 获得焦点,否则也得不到按键
SetParent(control.Handle, IntPtr.Zero);

2.新建一个窗口,设置 FormBorderStyle为None,WindowState为Maximized,TopMost为True。然后具体代码如下:

AddEventKeyUp(control);
原来的parent.Controls.Clear();
frmFullscreen frm = new frmFullscreen();
frm.Controls.Add(control);
frm.ShowDialog();
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();
原来的parent.Controls.Add(control);// 这里不能和下面的Close顺序错了,要不然会引起错误,因为Close后把控件销毁了。
frm.Close();
frm = null;
}
}
}
}

经过实际使用验证,第二种方法很好,没有任何问题。只是需要多开一个窗口。第一种方法有点小问题,就是如果控件上有右键菜单什么的,一调用 会跑到主界面去了。好像鼠标有时候也不太灵。

C# WinForm中 让控件全屏显示的实现代码的更多相关文章

  1. winfrom实现控件全屏效果

    用常规方法实现全屏显示时,由于采用的三方控件导致界面顶端一直有一条半透明的类似标题栏的东西无法去除,原因一直没找到. 下面综合整理下网上两位博主的用WindowsAPI实现全屏的方法: 控件全屏显示: ...

  2. Winform中checklistbox控件的常用方法

    Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...

  3. Extjs 疑难杂症 (LoadMark 遮罩、Panel Update无效、chrome浏览器date控件全屏)

    一.在extjs gridPanel中使用LoadMark无效,三步搞定. 原代码: grid = new Ext.grid.GridPanel({ store: store, title:'资料列表 ...

  4. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  5. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  6. Winform中TextBox控件开启自动提示补全功能

    问题:Winform开发中,有一个TextBox控件用以输入姓名,现希望在输入名字时能够自动提示所有可能的名字. 解答:winform中的TextBox控件含有如下三个属性:   ① AutoComp ...

  7. Winform中Picture控件图片的拖拽显示

    注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...

  8. winform中comboBox控件加默认选项的问题

    winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值? combob ...

  9. c#WinForm中TeeChart控件的注册和使用

    首先要注册好TeeChart控件,注册方法参考:https://blog.csdn.net/my_clear_mind/article/details/79741020 完成注册之后,新建一个WinF ...

随机推荐

  1. Installing Lua in Mac

    Lua is distributed in source form. You need to build it before using it. Building Lua should be stra ...

  2. How does database indexing work?

    When data is stored on disk based storage devices, it is stored as blocks of data. These blocks are ...

  3. Bash 小知识点

    变量定义的时候=两边不能有空格,例如: a='Hello World' 如果变量和其它字符相连,可以用{}把变量引起来,这样就可以和相连的字符隔离 除了在变量赋值和在FOR循环语句头中,BASH中的变 ...

  4. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

  5. Unity打包APK横屏时的注意事项

    由于你在Unity设置了横屏. 所以也需要在安卓的AndroidManifest.xml文件中, application/activity下声明为横屏.否则会黑屏,根本不给你报错,愁死你. 加上这一句 ...

  6. 一个简单的将GUI程序的log信息输出到关联的Console窗口中(AllocConsole SetConsoleTitle WriteConsole 最后用ShowWindow(GetConsoleWindow)进行显示)

    // .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...

  7. C#操作.csv文件Demo

    1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...

  8. [cocoapods]cocoapods问题解决

    错误1. While executing gem no such name 错误原因:gem 网址被挡住了. 解决办法:设置https://ruby.taobao.org/ 详情参考 http://w ...

  9. MyEclipse 2014 + JSP+ Servlet

    来自:http://blog.csdn.net/21aspnet/article/details/21867241 1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本. 2).下 ...

  10. taglist

    http://blog.csdn.net/duguteng/article/details/7412652 这两天看到网上有将vim 改造成功能强大的IDE的blog,突然心血来潮,亲身经历了一下. ...