winform下实现pictureBox全屏播放

最近开发一个项目,需要通过双击pictureBox实现全屏的功能,网上查找资料,加上一点摸索,最终实现了。做一下记录,以备以后需要。

主要功能都在下面这个类里面

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Runtime.InteropServices;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using System.Windows.Forms;
  8 
  9 namespace UAVRadar
 10 {
 11     /// <summary>  
 12     /// 定义全屏抽象类  
 13     /// </summary>  
 14     public abstract class FullScreenObject
 15     {
 16         public abstract void FullScreen(bool flag);
 17     }
 18     /// <summary>  
 19     /// 桌面全屏  
 20     /// </summary>  
 21     public unsafe class FullScreenHelper : FullScreenObject
 22     {
 23         public bool m_bFullScreen = false;
 24 
 25         WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT();
 26 
 27         Control m_control = null;
 28 
 29         public FullScreenHelper(Control control)
 30         {
 31             m_control = control;
 32         }
 33 
 34         private IntPtr m_OldWndParent = IntPtr.Zero;
 35 
 36         DockStyle old_docker_style;
 37         int old_left;
 38         int old_width;
 39         int old_height;
 40         int old_top;
 41 
 42         public override void FullScreen(bool flag)
 43         {
 44             m_bFullScreen = flag;
 45             if (!m_bFullScreen)
 46             {                
 47                 // 取消全屏设置  
 48                 m_control.Dock = old_docker_style;
 49                 m_control.Left = old_left;
 50                 m_control.Top = old_top;
 51                 m_control.Width = old_width;
 52                 m_control.Height = old_height;
 53                 ShellSDK.SetParent(m_control.Handle, m_OldWndParent);
 54                
 55             }
 56             else
 57             {
 58                
 59                 // 记录原来的数据  
 60                 old_docker_style = m_control.Dock;
 61                 old_left = m_control.Left;
 62                 old_width = m_control.Width;
 63                 old_height = m_control.Height;
 64                 old_top = m_control.Top;
 65                 m_OldWndParent = ShellSDK.GetParent(m_control.Handle);
 66                 // 设置全屏数据  
 67                 int nScreenWidth = ShellSDK.GetSystemMetrics(0);
 68                 int nScreenHeight = ShellSDK.GetSystemMetrics(1);
 69                 m_control.Dock = DockStyle.None;
 70                 m_control.Left = 0;
 71                 m_control.Top = 0;
 72                 m_control.Width = nScreenWidth;
 73                 m_control.Height = nScreenHeight;
 74                 ShellSDK.SetParent(m_control.Handle, ShellSDK.GetDesktopWindow());
 75                 ShellSDK.SetWindowPos(m_control.Handle, -1, 0, 0, m_control.Right - m_control.Left, m_control.Bottom - m_control.Top, 0);
 76                 
 77             }
 78             m_bFullScreen = !m_bFullScreen;
 79         }
 80 
 81     }
 82 
 83     /// <summary>  
 84     /// 在容器内部全屏  
 85     /// </summary>  
 86     public class FullScreenInContainerHelper : FullScreenObject
 87     {
 88         bool m_bFullScreen = false;
 89 
 90         Control m_control = null;
 91 
 92         public FullScreenInContainerHelper(Control control)
 93         {
 94             m_control = control;
 95         }
 96 
 97         private IntPtr m_OldWndParent = IntPtr.Zero;
 98         private IntPtr m_father_hwnd;
 99         private RECT m_rect = new RECT();
100 
101         public override void FullScreen(bool flag)  
102         {  
103             m_bFullScreen = flag;  
104             if (!m_bFullScreen)  
105             {  
106                 ShellSDK.SetParent(m_control.Handle, m_father_hwnd);  
107                 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);  
108                 ShellSDK.SetForegroundWindow(m_father_hwnd);  
109             }  
110             else  
111             {  
112                 m_father_hwnd = ShellSDK.GetParent(m_control.Handle);  
113                 RECT rect;
114                 RECT rect_fature;
115                 ShellSDK.GetWindowRect(m_control.Handle, out rect);  
116                 POINT pt = new POINT();  
117                 pt.x = rect.left;  
118                 pt.y = rect.top;  
119                 ShellSDK.ScreenToClient(m_father_hwnd, ref pt);  
120                 rect.right = rect.right - rect.left + pt.x;  
121                 rect.bottom = rect.bottom - rect.top + pt.y;  
122                 rect.left = pt.x;  
123                 rect.top = pt.y;  
124                 m_rect = rect;  
125                 ShellSDK.GetWindowRect(m_father_hwnd, out rect_fature);  
126                 ShellSDK.SetWindowPos(m_control.Handle, 0, 0, 0, rect_fature.right - rect_fature.left, rect_fature.bottom - rect_fature.top, 0);  
127             }  
128             m_bFullScreen = !m_bFullScreen;  
129         }
130     }
131 
132     /// <summary>  
133     /// Windows系统API-SDK  
134     /// </summary>  
135     public class ShellSDK
136     {
137         //锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态。锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态  
138         [DllImport("User32.dll")]
139         public static extern bool LockWindowUpdate(IntPtr hWndLock);
140 
141         //函数来设置弹出式窗口,层叠窗口或子窗口的父窗口。新的窗口与窗口必须属于同一应用程序  
142         [DllImport("User32.dll")]
143         public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
144 
145         //函数设置指定窗口的显示状态和恢复,最大化,最小化位置。函数功能: 函及原型     
146         [DllImport("User32.dll")]
147         public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
148 
149         //函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号  
150         [DllImport("User32.dll")]
151         public static extern bool SetForegroundWindow(IntPtr hWnd);
152 
153         //该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域  
154         [DllImport("User32.dll")]
155         public static extern IntPtr GetDesktopWindow();
156 
157         //函数名。该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置  
158         [DllImport("User32.dll")]
159         public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
160 
161         //是用于得到被定义的系统数据或者系统配置信息的一个专有名词    
162         [DllImport("User32.dll")]
163         public static extern int GetSystemMetrics(int nIndex);
164 
165         [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
166         public static extern IntPtr GetParent(IntPtr hWnd);
167         [DllImport("user32.dll", CharSet = CharSet.Auto)]
168         public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
169         [DllImport("user32.dll", CharSet = CharSet.Auto)]
170         public static extern System.IntPtr GetForegroundWindow();
171         [DllImport("user32")]
172         public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
173         [DllImport("user32.dll")]
174         public static extern uint ScreenToClient(IntPtr hwnd, ref POINT p);
175     }
176 
177     /// <summary>  
178     /// 图像窗口对象  
179     /// </summary>  
180     public struct WINDOWPLACEMENT
181     {
182         public uint length;
183         public uint flags;
184         public uint showCmd;
185         public POINT ptMinPosition;
186         public POINT ptMaxPosition;
187         public RECT rcNormalPosition;
188     }
189     /// <summary>  
190     /// 图像点位位置  
191     /// </summary>  
192     public struct POINT
193     {
194         public int x;
195         public int y;
196     }
197 
198     /// <summary>  
199     /// 图像区域对象  
200     /// </summary>  
201     public struct RECT
202     {
203         public int left;
204         public int top;
205         public int right;
206         public int bottom;
207     }
208 }

然后界面调用就比较简单了,pictureBox控件定义一个DoubleClick事件,然后调用如下代码即可

 1 private FullScreenHelper fullScreenHelper = null;
 2 private void pictureBox2_DoubleClick(object sender, EventArgs e)
 3         {
 4             if (fullScreenHelper == null)
 5             {
 6                 fullScreenHelper = new FullScreenHelper(pictureBox2);
 7                 fullScreenHelper.FullScreen(true);
 8             }
 9             else
10             {
11                 fullScreenHelper.FullScreen(false);
12                 fullScreenHelper = null;
13             }
14         }

 

posted on 2018-05-25 10:36  淹死的鸭子  阅读(1623)  评论(0编辑  收藏  举报

导航