这篇文章主要介绍了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; //为了保证左边的控件不被挡上减了一个210
float h = this.Height - 50;//为了底下的按钮不被挡上,因为还要用到,因此减了50
this.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# 窗体视频控件进入全屏模式和退出全屏模式
窗体控件进入全屏模式和退出全屏模式,视频播放的时候用到此功能。
工具类代码
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>
/// 全屏事件
/// </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);
/
沃梦达教程
本文标题为:C#窗体程序实现全屏及取消全屏步骤
猜你喜欢
- C语言qsort()函数的使用方法详解 2023-04-26
- C++ 数据结构超详细讲解顺序表 2023-03-25
- ubuntu下C/C++获取剩余内存 2023-09-18
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- Easyx实现扫雷游戏 2023-02-06
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- Qt计时器使用方法详解 2023-05-30
- C语言详解float类型在内存中的存储方式 2023-03-27
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- C语言手把手带你掌握带头双向循环链表 2023-04-03
