Guest User

Untitled

a guest
Aug 20th, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. namespace MinimizeCapture
  9. {
  10.  
  11. class WindowSnap
  12. {
  13.  
  14. #region Win32
  15.  
  16. private const string PROGRAMMANAGER = "Program Manager";
  17. private const string RUNDLL = "RunDLL";
  18.  
  19. private const uint WM_PAINT = 0x000F;
  20.  
  21. private const int GWL_EXSTYLE = -20;
  22. private const int WS_EX_LAYERED = 0x80000;
  23. private const int LWA_ALPHA = 0x2;
  24.  
  25. private enum ShowWindowEnum { Hide = 0, ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3, Maximize = 3, ShowNormalNoActivate = 4, Show = 5, Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8, Restore = 9, ShowDefault = 10, ForceMinimized = 11 };
  26.  
  27. private struct RECT
  28. {
  29. int left;
  30. int top;
  31. int right;
  32. int bottom;
  33.  
  34. public int Left
  35. {
  36. get { return this.left; }
  37. }
  38.  
  39. public int Top
  40. {
  41. get { return this.top; }
  42. }
  43.  
  44. public int Width
  45. {
  46. get { return right - left; }
  47. }
  48.  
  49. public int Height
  50. {
  51. get { return bottom - top; }
  52. }
  53.  
  54. public static implicit operator Rectangle(RECT rect)
  55. {
  56. return new Rectangle(rect.left, rect.top, rect.Width, rect.Height);
  57. }
  58. }
  59.  
  60. [DllImport("user32")]
  61. [return: MarshalAs(UnmanagedType.Bool)]
  62. private static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags);
  63.  
  64. [DllImport("user32.dll")]
  65. private static extern uint SendMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);
  66.  
  67. [DllImport("user32")]
  68. private static extern int GetWindowRect(IntPtr hWnd, ref RECT rect);
  69.  
  70. [DllImport("user32")]
  71. private static extern int PrintWindow(IntPtr hWnd, IntPtr dc, uint flags);
  72.  
  73. [DllImport("user32")]
  74. private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
  75.  
  76. [DllImport("user32")]
  77. private static extern int GetWindowTextLength(IntPtr hWnd);
  78.  
  79. [DllImport("user32")]
  80. [return: MarshalAs(UnmanagedType.Bool)]
  81. private static extern bool IsWindowVisible(IntPtr hWnd);
  82.  
  83. [DllImport("user32")]
  84. [return: MarshalAs(UnmanagedType.Bool)]
  85. private static extern bool IsIconic(IntPtr hWnd);
  86.  
  87. private delegate bool EnumWindowsCallbackHandler(IntPtr hWnd, IntPtr lParam);
  88.  
  89. [DllImport("user32.dll")]
  90. [return: MarshalAs(UnmanagedType.Bool)]
  91. private static extern bool EnumWindows(EnumWindowsCallbackHandler lpEnumFunc, IntPtr lParam);
  92.  
  93. [DllImport("user32")]
  94. private static extern int GetWindowLong(IntPtr hWnd, int index);
  95.  
  96. [DllImport("user32")]
  97. private static extern int SetWindowLong(IntPtr hWnd, int index, int dwNewLong);
  98.  
  99. [DllImport("user32")]
  100. private static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int flags);
  101.  
  102. #region Update for 4th October 2007
  103.  
  104. [DllImport("user32")]
  105. private static extern int GetClassName(IntPtr hWnd, StringBuilder name, int maxCount);
  106.  
  107. [DllImport("user32")]
  108. private static extern IntPtr GetParent(IntPtr hWnd);
  109.  
  110. [DllImport("user32")]
  111. private static extern IntPtr SetParent(IntPtr child, IntPtr newParent);
  112.  
  113. [DllImport("user32.dll")]
  114. private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool reDraw);
  115.  
  116. [DllImport("user32.dll")]
  117. private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
  118.  
  119. [StructLayout(LayoutKind.Sequential)]
  120. private struct WINDOWINFO
  121. {
  122. public uint cbSize;
  123. public RECT rcWindow;
  124. public RECT rcClient;
  125. public WindowStyles dwStyle;
  126. public ExtendedWindowStyles dwExStyle;
  127. public uint dwWindowStatus;
  128. public uint cxWindowBorders;
  129. public uint cyWindowBorders;
  130. public ushort atomWindowType;
  131. public ushort wCreatorVersion;
  132.  
  133. public static uint GetSize()
  134. {
  135. return (uint)Marshal.SizeOf(typeof(WINDOWINFO));
  136. }
  137. }
  138.  
  139. [Flags]
  140. private enum WindowStyles : uint
  141. {
  142. WS_OVERLAPPED = 0x00000000,
  143. WS_POPUP = 0x80000000,
  144. WS_CHILD = 0x40000000,
  145. WS_MINIMIZE = 0x20000000,
  146. WS_VISIBLE = 0x10000000,
  147. WS_DISABLED = 0x08000000,
  148. WS_CLIPSIBLINGS = 0x04000000,
  149. WS_CLIPCHILDREN = 0x02000000,
  150. WS_MAXIMIZE = 0x01000000,
  151. WS_BORDER = 0x00800000,
  152. WS_DLGFRAME = 0x00400000,
  153. WS_VSCROLL = 0x00200000,
  154. WS_HSCROLL = 0x00100000,
  155. WS_SYSMENU = 0x00080000,
  156. WS_THICKFRAME = 0x00040000,
  157. WS_GROUP = 0x00020000,
  158. WS_TABSTOP = 0x00010000,
  159.  
  160. WS_MINIMIZEBOX = 0x00020000,
  161. WS_MAXIMIZEBOX = 0x00010000,
  162.  
  163. WS_CAPTION = WS_BORDER | WS_DLGFRAME,
  164. WS_TILED = WS_OVERLAPPED,
  165. WS_ICONIC = WS_MINIMIZE,
  166. WS_SIZEBOX = WS_THICKFRAME,
  167. WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW,
  168.  
  169. WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
  170. WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
  171. WS_CHILDWINDOW = WS_CHILD,
  172. }
  173.  
  174. #endregion
  175.  
  176. #region Update for 10October 2007
  177.  
  178. [Flags]
  179. private enum ExtendedWindowStyles : uint
  180. {
  181. WS_EX_DLGMODALFRAME = 0x00000001,
  182. WS_EX_NOPARENTNOTIFY = 0x00000004,
  183. WS_EX_TOPMOST = 0x00000008,
  184. WS_EX_ACCEPTFILES = 0x00000010,
  185. WS_EX_TRANSPARENT = 0x00000020,
  186. WS_EX_MDICHILD = 0x00000040,
  187. WS_EX_TOOLWINDOW = 0x00000080,
  188. WS_EX_WINDOWEDGE = 0x00000100,
  189. WS_EX_CLIENTEDGE = 0x00000200,
  190. WS_EX_CONTEXTHELP = 0x00000400,
  191. WS_EX_RIGHT = 0x00001000,
  192. WS_EX_LEFT = 0x00000000,
  193. WS_EX_RTLREADING = 0x00002000,
  194. WS_EX_LTRREADING = 0x00000000,
  195. WS_EX_LEFTSCROLLBAR = 0x00004000,
  196. WS_EX_RIGHTSCROLLBAR = 0x00000000,
  197. WS_EX_CONTROLPARENT = 0x00010000,
  198. WS_EX_STATICEDGE = 0x00020000,
  199. WS_EX_APPWINDOW = 0x00040000,
  200. WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE),
  201. WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST)
  202. }
  203. #endregion
  204.  
  205. #endregion
  206.  
  207. #region Statics
  208.  
  209. #region Update for 10 Ocrober 2007
  210.  
  211. private static bool forceMDI = true;
  212.  
  213. /// <summary>
  214. /// if is true ,will force the mdi child to be captured completely ,maybe incompatible with some programs
  215. /// </summary>
  216. public static bool ForceMDICapturing
  217. {
  218. get { return forceMDI; }
  219. set { forceMDI = value; }
  220. }
  221. #endregion
  222.  
  223. [ThreadStatic]
  224. private static bool countMinimizedWindows;
  225.  
  226. [ThreadStatic]
  227. private static bool useSpecialCapturing;
  228.  
  229. [ThreadStatic]
  230. private static WindowSnapCollection windowSnaps;
  231.  
  232. [ThreadStatic]
  233. private static int winLong;
  234.  
  235. [ThreadStatic]
  236. private static bool minAnimateChanged = false;
  237.  
  238.  
  239. private static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
  240. {
  241. bool specialCapturing = false;
  242.  
  243. if (hWnd == IntPtr.Zero) return false;
  244.  
  245. if (!IsWindowVisible(hWnd)) return true;
  246.  
  247. if (!countMinimizedWindows)
  248. {
  249. if (IsIconic(hWnd)) return true;
  250. }
  251. else if (IsIconic(hWnd) && useSpecialCapturing) specialCapturing = true;
  252.  
  253. if (GetWindowText(hWnd) == PROGRAMMANAGER) return true;
  254.  
  255. windowSnaps.Add(new WindowSnap(hWnd, specialCapturing));
  256.  
  257. return true;
  258. }
  259.  
  260. /// <summary>
  261. /// Get the collection of WindowSnap instances fro all available windows
  262. /// </summary>
  263. /// <param name="minimized">Capture a window even it's Minimized</param>
  264. /// <param name="specialCapturring">use special capturing method to capture minmized windows</param>
  265. /// <returns>return collections of WindowSnap instances</returns>
  266. public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
  267. {
  268. windowSnaps = new WindowSnapCollection();
  269. countMinimizedWindows = minimized;//set minimized flag capture
  270. useSpecialCapturing = specialCapturring;//set specialcapturing flag
  271.  
  272. EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
  273. EnumWindows(callback, IntPtr.Zero);
  274.  
  275. return new WindowSnapCollection(windowSnaps.ToArray(), true);
  276. }
  277.  
  278. /// <summary>
  279. /// Get the collection of WindowSnap instances fro all available windows
  280. /// </summary>
  281. /// <returns>return collections of WindowSnap instances</returns>
  282. public static WindowSnapCollection GetAllWindows()
  283. {
  284. return GetAllWindows(false, false);
  285. }
  286.  
  287. /// <summary>
  288. /// Take a Snap from the specific Window
  289. /// </summary>
  290. /// <param name="hWnd">Handle of the Window</param>
  291. /// <param name="useSpecialCapturing">if you need to capture from the minimized windows set it true,otherwise false</param>
  292. /// <returns></returns>
  293. public static WindowSnap GetWindowSnap(IntPtr hWnd, bool useSpecialCapturing)
  294. {
  295. if (!useSpecialCapturing)
  296. return new WindowSnap(hWnd, false);
  297. return new WindowSnap(hWnd, NeedSpecialCapturing(hWnd));
  298. }
  299.  
  300. private static bool NeedSpecialCapturing(IntPtr hWnd)
  301. {
  302. if (IsIconic(hWnd)) return true;
  303. return false;
  304. }
  305.  
  306. private static Bitmap GetWindowImage(IntPtr hWnd, Size size)
  307. {
  308. try
  309. {
  310. if (size.IsEmpty || size.Height < 0 || size.Width < 0) return null;
  311.  
  312. Bitmap bmp = new Bitmap(size.Width, size.Height);
  313. Graphics g = Graphics.FromImage(bmp);
  314. IntPtr dc = g.GetHdc();
  315.  
  316. PrintWindow(hWnd, dc, 0);
  317.  
  318. g.ReleaseHdc();
  319. g.Dispose();
  320.  
  321. return bmp;
  322. }
  323. catch { return null; }
  324. }
  325.  
  326. private static string GetWindowText(IntPtr hWnd)
  327. {
  328. int length = GetWindowTextLength(hWnd) + 1;
  329. StringBuilder name = new StringBuilder(length);
  330.  
  331. GetWindowText(hWnd, name, length);
  332.  
  333. return name.ToString();
  334. }
  335.  
  336. private static Rectangle GetWindowPlacement(IntPtr hWnd)
  337. {
  338. RECT rect = new RECT();
  339.  
  340. GetWindowRect(hWnd, ref rect);
  341.  
  342. return rect;
  343. }
  344.  
  345. private static void EnterSpecialCapturing(IntPtr hWnd)
  346. {
  347. /*if (XPAppearance.MinAnimate)
  348. {
  349. XPAppearance.MinAnimate = false;
  350. minAnimateChanged = true;
  351. }
  352.  
  353.  
  354. winLong = GetWindowLong(hWnd, GWL_EXSTYLE);
  355. SetWindowLong(hWnd, GWL_EXSTYLE, winLong | WS_EX_LAYERED);
  356. SetLayeredWindowAttributes(hWnd, 0, 1, LWA_ALPHA);
  357.  
  358. ShowWindow(hWnd, ShowWindowEnum.Restore);
  359.  
  360. SendMessage(hWnd, WM_PAINT, 0, 0);*/
  361. if (XPAppearance.MinAnimate)
  362. {
  363. XPAppearance.MinAnimate = false;
  364. minAnimateChanged = true;
  365. }
  366.  
  367.  
  368. winLong = GetWindowLong(hWnd, GWL_EXSTYLE);
  369. SetWindowLong(hWnd, GWL_EXSTYLE, winLong | WS_EX_LAYERED);
  370. SetLayeredWindowAttributes(hWnd, 0, 1, LWA_ALPHA);
  371.  
  372. //ShowWindow(hWnd, ShowWindowEnum.Restore);
  373. ShowWindow(hWnd, ShowWindowEnum.ShowNormalNoActivate);
  374.  
  375. SendMessage(hWnd, WM_PAINT, 0, 0);
  376. }
  377.  
  378. private static void ExitSpecialCapturing(IntPtr hWnd)
  379. {
  380. /* ShowWindow(hWnd, ShowWindowEnum.Minimize);
  381. SetWindowLong(hWnd, GWL_EXSTYLE, winLong);
  382.  
  383. if (minAnimateChanged)
  384. {
  385. XPAppearance.MinAnimate = true;
  386. minAnimateChanged = false;
  387. }*/
  388. //ShowWindow(hWnd, ShowWindowEnum.Minimize);
  389. ShowWindow(hWnd, ShowWindowEnum.ShowMinNoActivate);
  390.  
  391. SetWindowLong(hWnd, GWL_EXSTYLE, winLong);
  392.  
  393. if (minAnimateChanged)
  394. {
  395. XPAppearance.MinAnimate = true;
  396. minAnimateChanged = false;
  397. }
  398.  
  399. }
  400.  
  401. #endregion
  402.  
  403. private Bitmap image;
  404. private Size size;
  405. private Point location;
  406. private string text;
  407. private IntPtr hWnd;
  408. private bool isIconic;
  409.  
  410. /// <summary>
  411. /// Get the Captured Image of the Window
  412. /// </summary>
  413. public Bitmap Image
  414. {
  415. get
  416. {
  417. if (this.image != null)
  418. return this.image;
  419. return null;
  420. }
  421. }
  422. /// <summary>
  423. /// Get Size of Snapped Window
  424. /// </summary>
  425. public Size Size
  426. {
  427. get { return this.size; }
  428. }
  429. /// <summary>
  430. /// Get Location of Snapped Window
  431. /// </summary>
  432. public Point Location
  433. {
  434. get { return this.location; }
  435. }
  436. /// <summary>
  437. /// Get Title of Snapped Window
  438. /// </summary>
  439. public string Text
  440. {
  441. get { return this.text; }
  442. }
  443. /// <summary>
  444. /// Get Handle of Snapped Window
  445. /// </summary>
  446. public IntPtr Handle
  447. {
  448. get { return this.hWnd; }
  449. }
  450. /// <summary>
  451. /// if the state of the window is minimized return true otherwise returns false
  452. /// </summary>
  453. public bool IsMinimized
  454. {
  455. get { return this.isIconic; }
  456. }
  457.  
  458. private WindowSnap(IntPtr hWnd, bool specialCapturing)
  459. {
  460. this.isIconic = IsIconic(hWnd);
  461. this.hWnd = hWnd;
  462.  
  463. if (specialCapturing)
  464. EnterSpecialCapturing(hWnd);
  465.  
  466. #region Child Support (Enter)
  467.  
  468. WINDOWINFO wInfo = new WINDOWINFO();
  469. wInfo.cbSize = WINDOWINFO.GetSize();
  470. GetWindowInfo(hWnd, ref wInfo);
  471.  
  472. bool isChild = false;
  473. IntPtr parent = GetParent(hWnd);
  474. Rectangle pos = new Rectangle();
  475. Rectangle parentPos = new Rectangle();
  476.  
  477. if (forceMDI && parent != IntPtr.Zero && (wInfo.dwExStyle & ExtendedWindowStyles.WS_EX_MDICHILD) == ExtendedWindowStyles.WS_EX_MDICHILD
  478. //&& (
  479. //(wInfo.dwStyle & WindowStyles.WS_CHILDWINDOW) == WindowStyles.WS_CHILDWINDOW||
  480. //(wInfo.dwStyle & WindowStyles.WS_CHILD) == WindowStyles.WS_CHILD)
  481. )//added 10 october 2007
  482.  
  483. {
  484. StringBuilder name = new StringBuilder();
  485. GetClassName(parent, name, RUNDLL.Length + 1);
  486. if (name.ToString() != RUNDLL)
  487. {
  488. isChild = true;
  489. pos = GetWindowPlacement(hWnd);
  490. MoveWindow(hWnd, int.MaxValue, int.MaxValue, pos.Width, pos.Height, true);
  491.  
  492. SetParent(hWnd, IntPtr.Zero);
  493.  
  494. parentPos = GetWindowPlacement(parent);
  495. }
  496. }
  497. #endregion
  498.  
  499. Rectangle rect = GetWindowPlacement(hWnd);
  500.  
  501. this.size = rect.Size;
  502. this.location = rect.Location;
  503. this.text = GetWindowText(hWnd);
  504. this.image = GetWindowImage(hWnd, this.size);
  505.  
  506. #region Child Support (Exit)
  507.  
  508. if (isChild)
  509. {
  510. SetParent(hWnd, parent);
  511.  
  512. //int x = pos.X - parentPos.X;
  513. //int y = pos.Y - parentPos.Y;
  514.  
  515. int x = wInfo.rcWindow.Left - parentPos.X;
  516. int y = wInfo.rcWindow.Top - parentPos.Y;
  517.  
  518. if ((wInfo.dwStyle & WindowStyles.WS_THICKFRAME) == WindowStyles.WS_THICKFRAME)
  519. {
  520. x -= SystemInformation.Border3DSize.Width;
  521. y -= SystemInformation.Border3DSize.Height;
  522. }
  523.  
  524. MoveWindow(hWnd, x, y, pos.Width, pos.Height, true);
  525. }
  526. #endregion
  527.  
  528. if (specialCapturing)
  529. ExitSpecialCapturing(hWnd);
  530. }
  531.  
  532. /// <summary>
  533. /// Gets the Name and Handle of the Snapped Window
  534. /// </summary>
  535. /// <returns></returns>
  536. public override string ToString()
  537. {
  538. StringBuilder str = new StringBuilder();
  539. str.AppendFormat("Window Text: {0}, Handle: {1}", this.text, this.hWnd.ToString());
  540.  
  541. return str.ToString();
  542. }
  543. }
  544. }
Add Comment
Please, Sign In to add comment