这篇文章主要介绍了C#窗体程序实现全屏及取消全屏步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
由于项目需要,需要用vs窗体程序实现播放视频的窗口的全屏和取消全屏。

具体实现界面如图:

这是初始状态,视频框的右上角就是控制全屏的按钮

这是全屏后的状态,此时全屏按钮变成了取消全屏的样式

注:为了界面的美观我的全屏并没有把左边的那些控件也盖住,但是是可以设置的,下边代码部分我会进行讲解。

1、首先说明一下我所用的控件及我的项目中控件的名称,以便大家理解。

显示视频的黑框是一个picturebox即代码中的VideoPlayWnd,全屏/取消全屏是一个button即代码中的button4

2、具体代码如下:

全屏和取消全屏是一个按钮即button4

private Int16 zoom = -1;//用来控制点击此button4的时候,是需要全屏还是取消全屏//视频窗口的缩放代码private void button4_Click(object sender, EventArgs e){if(zoom<0){float w = this.Width - 210; //为了保证左边的控件不被挡上减了一个210float h = this.Height - 50;//为了底下的按钮不被挡上,因为还要用到,因此减了50this.VideoPlayWnd.Size = Size.Ceiling(new SizeF(w, h));//重新部署视频框即这个picturebox空间的长宽VideoPlayWnd.Location = new System.Drawing.Point(210, 0);//视频框的左上角坐标,会发现与我上边减去的210一致(大家肯定都理解为什么我就不说了)button4.Location = new System.Drawing.Point(795, 0);//不能只有picturebox变了,这个button的位置也要保证和视频框同步,所以需要重设坐标// button4.BackgroundImage = Image.FromFile(@"E:\lwj\addpersoninfo\addpersoninfo\Resources\退出全屏.png");//绝对路径(这是我测试的时候用的)button4.BackgroundImage = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"退出全屏.png");//AppDomain.CurrentDomain.BaseDirectory语句可以获取到你当前项目的bin目录,所以需要把图片放到对应目录下zoom = 1;//全屏之后,再点击需要取消全屏,因此要改变zoom的值}else{ //以下为取消全屏,即还原自己的视频框和按钮,具体操作类似全屏,就不细致注释了VideoPlayWnd.Location = new System.Drawing.Point(495, 360);this.VideoPlayWnd.Size = Size.Ceiling(new SizeF(323, 271));button4.Location = new System.Drawing.Point(795, 360);button4.BackgroundImage = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"全屏.png");zoom = -1;//取消全屏后再点击就要进行全屏,因此继续设为-1(保证小于0,以满足全屏操作的if条件)}}

以上代码中的按钮是给它加了一个全屏样式的背景图片,并在点击时切换背景图片。

补充知识:C# 窗体视频控件进入全屏模式和退出全屏模式

窗体控件进入全c#教程屏模式和退出全屏模式,视频播放的时候用到此功能。

工具类代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace CvNetVideo.Play
{class FullScreenHelper{bool m_bFullScreen = false;IntPtr m_OldWndParent = IntPtr.Zero;WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT();Control m_control = null;public FullScreenHelper(Control c){m_control = c;}struct POINT{int x;int y;};struct RECT{public int left;public int top;public int right;public int bottom;};//锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态。锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态[DllImport("User32.dll")]public static extern bool LockWindowUpdate(IntPtr hWndLock);//函数来设置弹出式窗口,层叠窗口或子窗口的父窗口。新的窗口与窗口必须属于同一应用程序[DllImport("User32.dll")]public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);//函数设置指定窗口的显示状态和恢复,最大化,最小化位置。函数功能: 函及原型 [DllImport("User32.dll")]public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);//函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号[DllImport("User32.dll")]public static extern bool SetForegroundWindow(IntPtr hWnd);//该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域[DllImport("User32.dll")]public static extern IntPtr GetDesktopWindow();//函数名。该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置[DllImport("User32.dll")]public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);//是用于得到被定义的系统数据或者系统配置信息的一个专有名词 [DllImport("User32.dll")]public static extern int GetSystemMetrics(int nIndex); [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]public static extern IntPtr GetParent(IntPtr hWnd);[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern System.IntPtr GetForegroundWindow();[DllImport("user32")]public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);[DllImport("user32.dll")]public static extern uint ScreenToClient(IntPtr hwnd, ref POINT p);public void FullScreen(bool flag){m_bFullScreen = flag;if (!m_bFullScreen){LockWindowUpdate(m_control.Handle);SetParent(m_control.Handle, m_OldWndParent);SetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);SetForegroundWindow(m_OldWndParent);LockWindowUpdate(IntPtr.Zero);}else{GetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);int nScreenWidth = GetSystemMetrics(0);int nScreenHeight = GetSystemMetrics(1);m_OldWndParent = GetParent(m_control.Parent.Handle);SetParent(m_control.Handle, GetDesktopWindow());WINDOWPLACEMENT wp1 = new WINDOWPLACEMENT();wp1.length = (uint)Marshal.SizeOf(wp1);wp1.showCmd = 1;wp1.rcNormalPosition.left = 0;wp1.rcNormalPosition.top = 0;wp1.rcNormalPosition.right = nScreenWidth;wp1.rcNormalPosition.bottom = nScreenHeight;SetWindowPlacement(m_control.Handle, ref wp1);SetForegroundWindow(GetDesktopWindow());SetForegroundWindow(m_control.Handle);}m_bFullScreen = !m_bFullScreen;}struct WINDOWPLACEMENT{public uint length;public uint flags;public uint showCmd;public POINT ptMinPosition;public POINT ptMaxPosition;public RECT rcNormalPosition;};}
}

调用方式
///
/// 全屏事件

 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void UCVideo_DoubleClick(object sender, EventArgs e){//全屏设置//sdlVideo.SDL_MaximizeWindow();fullScreenHelper = new CvNetVideo.Play.FullScreenHelper(this);fullScreenHelper.FullScreen(true);Console.WriteLine("Entrance FullScreen Mode");}/// <summary>/// 按键弹起事件/// </summary>private void UCVideo_KeyUp(object sender, KeyEventArgs e){// ESC 退出全屏if (e.KeyCode == Keys.Escape&& fullScreenHelper!=null){fullScreenHelper.FullScreen(false);fullScreenHelper = null;Console.WriteLine("Exit FullScreen Mode");}   }

测试效果图

注意:在使用SDL的全屏操作过程中设置是无效的,播放视频过程中不能实现修改。代码如下:

public void SDL_MaximizeWindow(){Console.WriteLine("设置全屏");SDL.SDL_MaximizeWindow(screen);SDL.SDL_SetWindowFullscreen(screen, SDL.SDL_GetWindowFlags(screen));SDL.SDL_ShowWindow(screen);//int width, height;获取最大窗口值//SDL2.SDL.SDL_GetWindowMaximumSize(screen, out width, out height);设置最大窗口值//if (width>0&&height>0)//{// SDL2.SDL.SDL_SetWindowMaximumSize(screen, width, height);// Console.WriteLine("设置全屏......成功!width=" + width + ",height=" + height);//}//else//{// Console.WriteLine("设置全屏......失败!width=" + width + ",height=" + height);// SDL2.SDL.SDL_SetWindowMaximumSize(screen, w, h);//}}

工具代码功能改进

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace CvNetVideo.Play
{/// <summary>/// 定义全屏抽象类/// </summary>public abstract class FullScreenObject{public abstract void FullScreen(bool flag);}/// <summary>/// 桌面全屏/// </summary>public unsafe class FullScreenHelper: FullScreenObject{bool m_bFullScreen = false;WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT();UCVideo m_control = null;public FullScreenHelper(UCVideo control){m_control = control;}private IntPtr m_OldWndParent = IntPtr.Zero;DockStyle old_docker_style;int old_left;int old_width;int old_height;int old_top;public override void FullScreen(bool flag){m_bFullScreen = flag;if (!m_bFullScreen){#region 方式一:窗体容积改变时不能全屏,未能解决IE全屏显示不全问题//ShellSDK.LockWindowUpdate(m_control.Handle);//ShellSDK.SetParent(m_control.Handle, m_OldWndParent);//ShellSDK.SetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);//ShellSDK.SetForegroundWindow(m_OldWndParent);//ShellSDK.LockWindowUpdate(IntPtr.Zero);#endregion#region 方式二:在容器改变时可以实现全屏,未能解决IE全屏显示不全问题// 取消全屏设置m_control.Dock = old_docker_style;m_control.Left = old_left;m_control.Top = old_top;m_control.Width = old_width;m_control.Height = old_height;ShellSDK.SetParent(m_control.Handle, m_OldWndParent);#endregion}else{#region 方式一:窗体容积改变时不能全屏,未能解决IE全屏显示不全问题//ShellSDK.GetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);//int nScreenWidth = ShellSDK.GetSystemMetrics(0);//int nScreenHeight = ShellSDK.GetSystemMetrics(1);//m_OldWndParent = ShellSDK.GetParent(m_control.Handle);//ShellSDK.SetParent(m_control.Handle, ShellSDK.GetDesktopWindow());//WINDOWPLACEMENT wp1 = new WINDOWPLACEMENT();//wp1.length = (uint)Marshal.SizeOf(wp1);//wp1.showCmd = 1;//wp1.rcNormalPosition.left = 0;//wp1.rcNormalPosition.top = 0;//wp1.rcNormalPosition.right = Screen.PrimaryScreen.Bounds.Width/*nScreenWidth*/;//wp1.rcNormalPosition.bottom = Screen.PrimaryScreen.WorkingArea.Height/* nScreenHeight*/;//ShellSDK.SetWindowPlacement(m_control.Handle, ref wp1);//ShellSDK.SetForegroundWindow(ShellSDK.GetDesktopWindow());//ShellSDK.SetForegroundWindow(m_control.Handle);#endregion#region 方式二:在容器改变时可以实现全屏,未能解决IE全屏显示不全问题// 记录原来的数据old_docker_style = m_control.Dock;old_left = m_control.Left;old_width = m_control.Width;old_height = m_control.Height;old_top = m_control.Top;m_OldWndParent = ShellSDK.GetParent(m_control.Handle);// 设置全屏数据int nScreenWidth = ShellSDK.GetSystemMetrics(0);int nScreenHeight = ShellSDK.GetSystemMetrics(1);m_control.Dock = DockStyle.None;m_control.Left = 0;m_control.Top = 0;m_control.Width = nScreenWidth;m_control.Height = nScreenHeight;ShellSDK.SetParent(m_control.Handle, ShellSDK.GetDesktopWindow());ShellSDK.SetWindowPos(m_control.Handle, -1, 0, 0, m_control.Right - m_control.Left, m_control.Bottom - m_control.Top, 0);#endregion}m_bFullScreen = !m_bFullScreen;}}/// <summary>/// 在容器内部全屏/// </summary>public class FullScreenInContainerHelper : FullScreenObject{bool m_bFullScreen = false;Control m_control = null;public FullScreenInContainerHelper(Control control){m_control = control;}private IntPtr m_OldWndParent = IntPtr.Zero;private IntPtr m_father_hwnd;private RECT m_rect = new RECT();public override void FullScreen(bool flag){m_bFullScreen = flag;if (!m_bFullScreen){ShellSDK.SetParent(m_control.Handle, m_father_hwnd);ShellSDK.SetWindowPos(m_control.Handle, 0, m_rect.left, m_rect.top, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top, 0);ShellSDK.SetForegroundWindow(m_father_hwnd);}else{m_father_hwnd = ShellSDK.GetParent(m_control.Handle);ShellSDK.GetWindowRect(m_control.Handle, out RECT rect);POINT pt = new POINT();pt.x = rect.left;pt.y = rect.top;ShellSDK.ScreenToClient(m_father_hwnd, ref pt);rect.right = rect.right - rect.left + pt.x;rect.bottom = rect.bottom - rect.top + pt.y;rect.left = pt.x;rect.top = pt.y;m_rect = rect;ShellSDK.GetWindowRect(m_father_hwnd, out RECT rect_fature);ShellSDK.SetWindowPos(m_control.Handle, 0, 0, 0, rect_fature.right - rect_fature.left, rect_fature.bottom - rect_fature.top, 0);}m_bFullScreen = !m_bFullScreen;}}/// <summary>/// Windows系统API-SDK/// </summary>public class ShellSDK{//锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态。锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态[DllImport("User32.dll")]public static extern bool LockWindowUpdate(IntPtr hWndLock);//函数来设置弹出式窗口,层叠窗口或子窗口的父窗口。新的窗口与窗口必须属于同一应用程序[DllImport("User32.dll")]public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);//函数设置指定窗口的显示状态和恢复,最大化,最小化位置。函数功能: 函及原型 [DllImport("User32.dll")]public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);//函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号[DllImport("User32.dll")]public static extern bool SetForegroundWindow(IntPtr hWnd);//该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域[DllImport("User32.dll")]public static extern IntPtr GetDesktopWindow();//函数名。该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置[DllImport("User32.dll")]public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);//是用于得到被定义的系统数据或者系统配置信息的一个专有名词 [DllImport("User32.dll")]public static extern int GetSystemMetrics(int nIndex); [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]public static extern IntPtr GetParent(IntPtr hWnd);[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern System.IntPtr GetForegroundWindow();[DllImport("user32")]public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);[DllImport("user32.dll")]public static extern uint ScreenToClient(IntPtr hwnd, ref POINT p);}/// <summary>/// 图像区域对象/// </summary>public struct RECT{public int left;public int top;public int right;public int bottom;}/// <summary>/// 图像点位位置/// </summary>public struct POINT{public int x;public int y;}/// <summary>/// 图像窗口对象/// </summary>public struct WINDOWPLACEMENT{public uint length;public uint flags;public uint showCmd;public POINT ptMinPosition;public POINT ptMaxPosition;public RECT rcNormalPosition;}
}

以上这篇C#窗体程序实现全屏及python基础教程取消全屏步骤就是

C#窗体程序实现全屏及取消全屏步骤相关推荐

  1. 实现全选和取消全选功能

    <body> <div class="wrap"> <table> <thead> <tr> <th> &l ...

  2. JavaScript实现全选和取消全选

    源代码 <!DOCTYPE html> <html> <head lang="en"><meta charset="UTF-8& ...

  3. react实现全选、取消全选和个别选择

    react里面实现全选和取消全选,个别选择等操作,效果如下 代码: import React, {Component} from 'react' export default class Demo e ...

  4. 在项目中学习.NET的JQuery CheckBox方法(全选、取消全选、其他)

    一.在项目中遇到的CheckBox的全选和取消全选以及其他等解决方案如下: // 对全选和取消全选的事件 $("#CheckAll").click(function () {    ...

  5. axure实现复选框全选_AxureRP8实战手册-案例73(全选与取消全选效果)

    案例73. 全选与取消全选效果 案例来源: 百度音乐-音乐盒 案例效果: 初始状态/取消全选时:(图5-117) 全选后取消任一选项时:(图5-118) 全选/单选全部选中时:(图5-119) 案例描 ...

  6. DataGridView添加一行数据、全选、取消全选、清空数据、删除选中行

    .net 2005下的Windows Form Application,一个DataGridView控件和4个Button,界面设置如下: 代码如下,有注解,相信大家都看得明白: using Syst ...

  7. php 复选框全选和取消,基于JavaScript实现复选框的全选和取消全选

    这篇文章主要为大家详细介绍了基于JavaScript实现复选框的全选和取消全选,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了js复选框的全选和取消全选的具体代码,供大家参考, ...

  8. vue全选和取消全选(无bug)

    很简单的使用vue实现全选和取消全选 直接上代码,简单易懂不懂得可以留言. <!DOCTYPE html> <html lang="en"> <hea ...

  9. AVL_全选_取消全选_打印_ZMM1035

    *&---------------------------------------------------------------------* *& Report ZMM1035 * ...

最新文章

  1. POJ 1038 Bugs Integrated Inc (复杂的状压DP)
  2. JavaScript 事件——“事件类型”中“复合事件”和“变动事件”的注意要点
  3. jenkins+svn搭建
  4. 前端开发代码架构相关想法
  5. wxWidgets:制作渲染循环
  6. CSS的display:table-cell:多行文字的垂直居中水平居中
  7. 【JDBC】Eclipse连接Mysql
  8. 数据结构与算法理论概述
  9. Android对接实现内网无纸化会议|智慧教室|实时同屏功能
  10. Spring 常见的一些面试题整理
  11. 潭州Java中级班(day_13)-异常概述
  12. 计算机发展与应用说课,计算机的发展与应用说课稿.doc
  13. 把IE武装到牙齿,IE插件全攻略(转)
  14. 书籍之 Head First HTML与CSS
  15. 智慧养老整体解决方案
  16. 一款比XMIND更好用的思维导图
  17. Javascript特效:音乐钢琴
  18. Day02 - CSS
  19. FOURCC四字符码对照表
  20. 063 邪恶八进制域名收集

热门文章

  1. sql 运算符between与大于小于
  2. 第七章:抽样与抽样分布(Sampling and sampling distribution)
  3. .Net常用术语汇总
  4. Cephalocon APAC 2018在北京成功举办
  5. Java——基础知识
  6. 如何搭建属于自己的一个网站网址-----亲测有效
  7. ipython shell_IPython 1.0发布,强大的Python交互式Shell
  8. css中单位px、pt、em和rem的区别
  9. 刚刚,2022中国大学工科排名出炉
  10. python矩阵的右下半部分【简单易懂,代码可以直接运行】