1. 程式人生 > >winform下實現pictureBox全屏播放

winform下實現pictureBox全屏播放

char top 最終 項目 window eve wpa user using

最近開發一個項目,需要通過雙擊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         }

winform下實現pictureBox全屏播放