winform下实现pictureBox全屏播放
最近开发一个项目,需要通过双击pictureBox实现全屏的功能,网上查找资料,加上一点摸索,最终实现了。做一下记录,以备以后需要。
主要功能都在下面这个类里面
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 UAVRadar
{
/// <summary>
/// 定义全屏抽象类
/// </summary>
public abstract class FullScreenObject
{
public abstract void FullScreen(bool flag);
}
/// <summary>
/// 桌面全屏
/// </summary>
public unsafe class FullScreenHelper : FullScreenObject
{
public bool m_bFullScreen = false; WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT(); Control m_control = null; public FullScreenHelper(Control 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)
{
// 取消全屏设置
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); }
else
{ // 记录原来的数据
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();
int nScreenHeight = ShellSDK.GetSystemMetrics();
m_control.Dock = DockStyle.None;
m_control.Left = ;
m_control.Top = ;
m_control.Width = nScreenWidth;
m_control.Height = nScreenHeight;
ShellSDK.SetParent(m_control.Handle, ShellSDK.GetDesktopWindow());
ShellSDK.SetWindowPos(m_control.Handle, -, , , m_control.Right - m_control.Left, m_control.Bottom - m_control.Top, ); }
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, , m_rect.left, m_rect.top, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top, );
ShellSDK.SetForegroundWindow(m_father_hwnd);
}
else
{
m_father_hwnd = ShellSDK.GetParent(m_control.Handle);
RECT rect;
RECT rect_fature;
ShellSDK.GetWindowRect(m_control.Handle, out 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_fature);
ShellSDK.SetWindowPos(m_control.Handle, , , , rect_fature.right - rect_fature.left, rect_fature.bottom - rect_fature.top, );
}
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 WINDOWPLACEMENT
{
public uint length;
public uint flags;
public uint showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public RECT rcNormalPosition;
}
/// <summary>
/// 图像点位位置
/// </summary>
public struct POINT
{
public int x;
public int y;
} /// <summary>
/// 图像区域对象
/// </summary>
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
}
然后界面调用就比较简单了,pictureBox控件定义一个DoubleClick事件,然后调用如下代码即可
private FullScreenHelper fullScreenHelper = null;
private void pictureBox2_DoubleClick(object sender, EventArgs e)
{
if (fullScreenHelper == null)
{
fullScreenHelper = new FullScreenHelper(pictureBox2);
fullScreenHelper.FullScreen(true);
}
else
{
fullScreenHelper.FullScreen(false);
fullScreenHelper = null;
}
}
winform下实现pictureBox全屏播放的更多相关文章
- [Winform]Media Player组件全屏播放的设置
摘要 在设置程序开始运行时,让视频全屏播放时,直接设置 windowsMediaPlay.fullScreen = true; 会报错,代码如下 windowsMediaPlay.URL = _vid ...
- 用原生VideoView进行全屏播放时的问题
之前参加了一个课程,里面有一节讲到了用视频作为启动界面.讲师用的是自定义VideoView,重写onMeasure方法,因为原生的VideoView在那情况下不能实现全屏播放.当时没有深入研究,现在补 ...
- audio与video控件/标签的隐藏,iso/Android下自动全屏播放,短暂黑屏问题
(一)audio音频标签 <audio src="xxx.mp3"></audio> (二)video视频标签 <video src="xx ...
- MUI ios下用video标签默认全屏播放
前几天用Hbuilder+MUI做了个应用,里边用到<video>标签,在Android下正常,但是在苹果手机老是默认全屏播放. 解决办法: 首先在video标签加上playsinline ...
- 【Win 10应用开发】实现全屏播放的方法
有人会问,以前的MediaElement控件不是有现成的一排操作按钮吗?而且可以直接进入全屏播放.是的,我们知道,以往的Store App都是在全屏模式下运行的,只要MediaElement控件填满整 ...
- iphone H5视频行内播放(禁止全屏播放)
一般用户都知道,ios在网页点击视频播放时,视频会弹出全屏播放框. video标签的playsinline.webkit-playsinline标记根本就不会起作用. 还有传闻说对于没有声音的视频不会 ...
- HTML5新标签video在iOS上默认全屏播放
今天做一个app时发现一个问题,应用html5中的video标签加载视频,在Android手机上默认播放大小,但是换成iPhone手机上出问题了,默认弹出全屏播放,查找了好多论坛,都没有谈论这个的.然 ...
- video自动全屏播放
video自动全屏播放 关于Screen.lockOrientation() https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockO ...
- iOS 全局禁止横屏,但UIWebView 全屏播放视频,横屏,解决办法(任意页面横竖屏或禁止)
iOS 全局禁止横屏,但UIWebView 全屏播放视频,横屏,解决办法 时间 2015-07-14 20:59:00 博客园-原创精华区 原文 http://www.cnblogs.com/fe ...
随机推荐
- 不使用构造方法创建Java对象: objenesis的基本使用方法
转载:http://blog.csdn.net/codershamo/article/details/52015206 objenesis简介: objenesis是一个小型Java类库用来实例化一个 ...
- python:控制鼠标和键盘
程序: # # _*_ coding:UTF-8 _*_ import win32api import win32con import win32gui from ctypes import * im ...
- html中引入另一个html文件
https://segmentfault.com/q/1010000002954318
- LeetCode OJ:Number of 1 Bits(比特1的位数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- New Concept English three(10)
The great ship, Titanic, sailed for New York from Southampton on April 10th, 1912. She was carrying ...
- JVM的理解
1.Java程序是交由JVM执行的,所以我们在谈Java内存区域划分的时候事实上是指JVM内存区域划分.在讨论JVM内存区域划分之前,先来看一下Java程序具体执行的过程: 也相当与 注:JVM(ja ...
- [Python] 项目打包发布
一.setuptools - 官方文档: Building and Distributing Packages with Setuptools- 中文文档: Python包管理工具setuptools ...
- hdu 1098
http://acm.hdu.edu.cn/showproblem.php?pid=1098 假设x=m时,65|f(m),即65|5*m^13+13*m^5+k*a*m 计算f(m+1)=(5*m^ ...
- Swift UITextField各种属性的设置
//MARK: 文本输入框 func _initTextField() { //如果需要在模拟器中调用电脑的键盘 快捷键:command + shift + k let textField = UIT ...
- iOS开发错误汇总
人非圣贤孰能无过 dyld: Library not loaded: /... 过而能改善莫大焉 iOS下dyld: Library not loaded: 错误信息解决方案