Advertisement
Guest User

Untitled

a guest
Aug 26th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.57 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. EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
  272. EnumWindows(callback, IntPtr.Zero);
  273. return new WindowSnapCollection(windowSnaps.ToArray(), true);
  274. }
  275.  
  276. /// <summary>
  277. /// Get the collection of WindowSnap instances fro all available windows
  278. /// </summary>
  279. /// <returns>return collections of WindowSnap instances</returns>
  280. public static WindowSnapCollection GetAllWindows()
  281. {
  282. return GetAllWindows(false, false);
  283. }
  284.  
  285. /// <summary>
  286. /// Take a Snap from the specific Window
  287. /// </summary>
  288. /// <param name="hWnd">Handle of the Window</param>
  289. /// <param name="useSpecialCapturing">if you need to capture from the minimized windows set it true,otherwise false</param>
  290. /// <returns></returns>
  291. public static WindowSnap GetWindowSnap(IntPtr hWnd, bool useSpecialCapturing)
  292. {
  293. if (!useSpecialCapturing)
  294. return new WindowSnap(hWnd, false);
  295. return new WindowSnap(hWnd, NeedSpecialCapturing(hWnd));
  296. }
  297.  
  298. private static bool NeedSpecialCapturing(IntPtr hWnd)
  299. {
  300. if (IsIconic(hWnd)) return true;
  301. return false;
  302. }
  303.  
  304. private static Bitmap GetWindowImage(IntPtr hWnd, Size size)
  305. {
  306. try
  307. {
  308. if (size.IsEmpty || size.Height < 0 || size.Width < 0) return null;
  309.  
  310. Bitmap bmp = new Bitmap(size.Width, size.Height);
  311. Graphics g = Graphics.FromImage(bmp);
  312. IntPtr dc = g.GetHdc();
  313.  
  314. PrintWindow(hWnd, dc, 0);
  315.  
  316. g.ReleaseHdc();
  317. g.Dispose();
  318.  
  319. return bmp;
  320. }
  321. catch { return null; }
  322. }
  323.  
  324. private static string GetWindowText(IntPtr hWnd)
  325. {
  326. int length = GetWindowTextLength(hWnd) + 1;
  327. StringBuilder name = new StringBuilder(length);
  328.  
  329. GetWindowText(hWnd, name, length);
  330. return name.ToString();
  331. }
  332.  
  333. private static Rectangle GetWindowPlacement(IntPtr hWnd)
  334. {
  335. RECT rect = new RECT();
  336.  
  337. GetWindowRect(hWnd, ref rect);
  338.  
  339. return rect;
  340. }
  341.  
  342. private static void EnterSpecialCapturing(IntPtr hWnd)
  343. {
  344. /*if (XPAppearance.MinAnimate)
  345. {
  346. XPAppearance.MinAnimate = false;
  347. minAnimateChanged = true;
  348. }
  349.  
  350.  
  351. winLong = GetWindowLong(hWnd, GWL_EXSTYLE);
  352. SetWindowLong(hWnd, GWL_EXSTYLE, winLong | WS_EX_LAYERED);
  353. SetLayeredWindowAttributes(hWnd, 0, 1, LWA_ALPHA);
  354.  
  355. ShowWindow(hWnd, ShowWindowEnum.Restore);
  356.  
  357. SendMessage(hWnd, WM_PAINT, 0, 0);*/
  358. if (XPAppearance.MinAnimate)
  359. {
  360. XPAppearance.MinAnimate = false;
  361. minAnimateChanged = true;
  362. }
  363.  
  364.  
  365. winLong = GetWindowLong(hWnd, GWL_EXSTYLE);
  366. SetWindowLong(hWnd, GWL_EXSTYLE, winLong | WS_EX_LAYERED);
  367. SetLayeredWindowAttributes(hWnd, 0, 1, LWA_ALPHA);
  368.  
  369. //ShowWindow(hWnd, ShowWindowEnum.Restore);
  370. ShowWindow(hWnd, ShowWindowEnum.ShowNormalNoActivate);
  371.  
  372. SendMessage(hWnd, WM_PAINT, 0, 0);
  373. }
  374.  
  375. private static void ExitSpecialCapturing(IntPtr hWnd)
  376. {
  377. /* ShowWindow(hWnd, ShowWindowEnum.Minimize);
  378. SetWindowLong(hWnd, GWL_EXSTYLE, winLong);
  379.  
  380. if (minAnimateChanged)
  381. {
  382. XPAppearance.MinAnimate = true;
  383. minAnimateChanged = false;
  384. }*/
  385. //ShowWindow(hWnd, ShowWindowEnum.Minimize);
  386. ShowWindow(hWnd, ShowWindowEnum.ShowMinNoActivate);
  387.  
  388. SetWindowLong(hWnd, GWL_EXSTYLE, winLong);
  389.  
  390. if (minAnimateChanged)
  391. {
  392. XPAppearance.MinAnimate = true;
  393. minAnimateChanged = false;
  394. }
  395.  
  396. }
  397.  
  398. #endregion
  399.  
  400. private Bitmap image;
  401. private Size size;
  402. private Point location;
  403. private string text;
  404. private IntPtr hWnd;
  405. private bool isIconic;
  406.  
  407. /// <summary>
  408. /// Get the Captured Image of the Window
  409. /// </summary>
  410. public Bitmap Image
  411. {
  412. get
  413. {
  414. if (this.image != null)
  415. return this.image;
  416. return null;
  417. }
  418. }
  419. /// <summary>
  420. /// Get Size of Snapped Window
  421. /// </summary>
  422. public Size Size
  423. {
  424. get { return this.size; }
  425. }
  426. /// <summary>
  427. /// Get Location of Snapped Window
  428. /// </summary>
  429. public Point Location
  430. {
  431. get { return this.location; }
  432. }
  433. /// <summary>
  434. /// Get Title of Snapped Window
  435. /// </summary>
  436. public string Text
  437. {
  438. get { return this.text; }
  439. }
  440. /// <summary>
  441. /// Get Handle of Snapped Window
  442. /// </summary>
  443. public IntPtr Handle
  444. {
  445. get { return this.hWnd; }
  446. }
  447. /// <summary>
  448. /// if the state of the window is minimized return true otherwise returns false
  449. /// </summary>
  450. public bool IsMinimized
  451. {
  452. get { return this.isIconic; }
  453. }
  454.  
  455. private WindowSnap(IntPtr hWnd, bool specialCapturing)
  456. {
  457. this.isIconic = IsIconic(hWnd);
  458. this.hWnd = hWnd;
  459.  
  460. if (specialCapturing)
  461. EnterSpecialCapturing(hWnd);
  462.  
  463. #region Child Support (Enter)
  464.  
  465. WINDOWINFO wInfo = new WINDOWINFO();
  466. wInfo.cbSize = WINDOWINFO.GetSize();
  467. GetWindowInfo(hWnd, ref wInfo);
  468.  
  469. bool isChild = false;
  470. IntPtr parent = GetParent(hWnd);
  471. Rectangle pos = new Rectangle();
  472. Rectangle parentPos = new Rectangle();
  473.  
  474. if (forceMDI && parent != IntPtr.Zero && (wInfo.dwExStyle & ExtendedWindowStyles.WS_EX_MDICHILD) == ExtendedWindowStyles.WS_EX_MDICHILD
  475. //&& (
  476. //(wInfo.dwStyle & WindowStyles.WS_CHILDWINDOW) == WindowStyles.WS_CHILDWINDOW||
  477. //(wInfo.dwStyle & WindowStyles.WS_CHILD) == WindowStyles.WS_CHILD)
  478. )//added 10 october 2007
  479.  
  480. {
  481. StringBuilder name = new StringBuilder();
  482. GetClassName(parent, name, RUNDLL.Length + 1);
  483. if (name.ToString() != RUNDLL)
  484. {
  485. isChild = true;
  486. pos = GetWindowPlacement(hWnd);
  487. MoveWindow(hWnd, int.MaxValue, int.MaxValue, pos.Width, pos.Height, true);
  488.  
  489. SetParent(hWnd, IntPtr.Zero);
  490.  
  491. parentPos = GetWindowPlacement(parent);
  492. }
  493. }
  494. #endregion
  495.  
  496. Rectangle rect = GetWindowPlacement(hWnd);
  497.  
  498. this.size = rect.Size;
  499. this.location = rect.Location;
  500. this.text = GetWindowText(hWnd);
  501. this.image = GetWindowImage(hWnd, this.size);
  502.  
  503. #region Child Support (Exit)
  504.  
  505. if (isChild)
  506. {
  507. SetParent(hWnd, parent);
  508.  
  509. //int x = pos.X - parentPos.X;
  510. //int y = pos.Y - parentPos.Y;
  511.  
  512. int x = wInfo.rcWindow.Left - parentPos.X;
  513. int y = wInfo.rcWindow.Top - parentPos.Y;
  514.  
  515. if ((wInfo.dwStyle & WindowStyles.WS_THICKFRAME) == WindowStyles.WS_THICKFRAME)
  516. {
  517. x -= SystemInformation.Border3DSize.Width;
  518. y -= SystemInformation.Border3DSize.Height;
  519. }
  520.  
  521. MoveWindow(hWnd, x, y, pos.Width, pos.Height, true);
  522. }
  523. #endregion
  524.  
  525. if (specialCapturing)
  526. ExitSpecialCapturing(hWnd);
  527. }
  528.  
  529. /// <summary>
  530. /// Gets the Name and Handle of the Snapped Window
  531. /// </summary>
  532. /// <returns></returns>
  533. public override string ToString()
  534. {
  535. StringBuilder str = new StringBuilder();
  536. str.AppendFormat("Window Text: {0}, Handle: {1}", this.text, this.hWnd.ToString());
  537.  
  538. return str.ToString();
  539. }
  540. }
  541. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement