/reporting/crashsender/ScreenCap.h

http://crashrpt.googlecode.com/ · C Header · 180 lines · 104 code · 37 blank · 39 comment · 0 complexity · 01426dcdd9563939e1e2b16c63242914 MD5 · raw file

  1. /*************************************************************************************
  2. This file is a part of CrashRpt library.
  3. Copyright (c) 2003-2013 The CrashRpt project authors. All Rights Reserved.
  4. Use of this source code is governed by a BSD-style license
  5. that can be found in the License.txt file in the root of the source
  6. tree. All contributing project authors may
  7. be found in the Authors.txt file in the root of the source tree.
  8. ***************************************************************************************/
  9. #ifndef __SCREENCAP_H__
  10. #define __SCREENCAP_H__
  11. #include "stdafx.h"
  12. extern "C" {
  13. #include "png.h"
  14. }
  15. #include "jpeglib.h"
  16. // Window information
  17. struct WindowInfo
  18. {
  19. CString m_sTitle; // Window title
  20. CRect m_rcWnd; // Window rect
  21. DWORD dwStyle;
  22. DWORD dwExStyle;
  23. };
  24. // Monitor info
  25. struct MonitorInfo
  26. {
  27. CString m_sDeviceID; // Device ID
  28. CRect m_rcMonitor; // Monitor rectangle in screen coordinates
  29. CString m_sFileName; // Image file name corresponding to this monitor
  30. };
  31. // Desktop screen shot info
  32. struct ScreenshotInfo
  33. {
  34. // Constructor
  35. ScreenshotInfo()
  36. {
  37. m_bValid = FALSE;
  38. }
  39. BOOL m_bValid; // If TRUE, this structure's fields are valid.
  40. time_t m_CreateTime; // Time of screenshot capture.
  41. CRect m_rcVirtualScreen; // Coordinates of virtual screen rectangle.
  42. std::vector<MonitorInfo> m_aMonitors; // The list of monitors captured.
  43. std::vector<WindowInfo> m_aWindows; // The list of windows captured.
  44. };
  45. // Screenshot type
  46. enum SCREENSHOT_TYPE
  47. {
  48. SCREENSHOT_TYPE_VIRTUAL_SCREEN = 0, // Screenshot of entire desktop.
  49. SCREENSHOT_TYPE_MAIN_WINDOW = 1, // Screenshot of given process' main window.
  50. SCREENSHOT_TYPE_ALL_PROCESS_WINDOWS = 2 // Screenshot of all process windows.
  51. };
  52. // What format to use when saving screenshots
  53. enum SCREENSHOT_IMAGE_FORMAT
  54. {
  55. SCREENSHOT_FORMAT_PNG = 0, // Use PNG format
  56. SCREENSHOT_FORMAT_JPG = 1, // Use JPG format
  57. SCREENSHOT_FORMAT_BMP = 2 // Use BMP format
  58. };
  59. // Desktop screenshot capture
  60. class CScreenCapture
  61. {
  62. public:
  63. // Constructor.
  64. CScreenCapture();
  65. // Destructor.
  66. ~CScreenCapture();
  67. // Takes desktop screenshot and returns information about it.
  68. BOOL TakeDesktopScreenshot(
  69. LPCTSTR szSaveToDir,
  70. ScreenshotInfo& ssi,
  71. SCREENSHOT_TYPE type=SCREENSHOT_TYPE_VIRTUAL_SCREEN,
  72. DWORD dwProcessId = 0,
  73. SCREENSHOT_IMAGE_FORMAT fmt=SCREENSHOT_FORMAT_PNG,
  74. int nJpegQuality = 95,
  75. BOOL bGrayscale=FALSE,
  76. int nIdStartFrom=0);
  77. private:
  78. // Returns current virtual screen rectangle
  79. void GetScreenRect(LPRECT rcScreen);
  80. // Returns an array of visible windows for the specified process or
  81. // the main window of the process.
  82. BOOL FindWindows(DWORD dwProcessId, BOOL bAllProcessWindows,
  83. std::vector<WindowInfo>* paWindows);
  84. // Captures the specified screen area and returns the list of image files
  85. BOOL CaptureScreenRect(
  86. std::vector<CRect> arcCapture,
  87. CString sSaveDirName,
  88. int nIdStartFrom,
  89. SCREENSHOT_IMAGE_FORMAT fmt,
  90. int nJpegQuality,
  91. BOOL bGrayscale,
  92. std::vector<MonitorInfo>& monitor_list);
  93. // Monitor enumeration callback.
  94. static BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor,
  95. HDC /*hdcMonitor*/, LPRECT lprcMonitor, LPARAM dwData);
  96. // Window enumeration callback.
  97. static BOOL CALLBACK EnumWndProc(HWND hWnd, LPARAM lParam);
  98. /* PNG management functions */
  99. // Initializes PNG file header
  100. BOOL PngInit(int nWidth, int nHeight, BOOL bGrayscale, CString sFileName);
  101. // Writes a scan line to the PNG file
  102. BOOL PngWriteRow(LPBYTE pRow, int nRowLen, BOOL bGrayscale);
  103. // Closes PNG file
  104. BOOL PngFinalize();
  105. /* JPEG management functions */
  106. // Initializes JPEG file header.
  107. BOOL JpegInit(int nWidth, int nHeight, BOOL bGrayscale, int nQuality, CString sFileName);
  108. // Writes a scan line to JPEG file.
  109. BOOL JpegWriteRow(LPBYTE pRow, int nRowLen, BOOL bGrayscale);
  110. // Closes PNG file.
  111. BOOL JpegFinalize();
  112. /* BMP management functions */
  113. // Initializes BMP file header
  114. BOOL BmpInit(int nWidth, int nHeight, BOOL bGrayscale, CString sFileName);
  115. // Writes a scan line to the BMP file
  116. BOOL BmpWriteRow(LPBYTE pRow, int nRowLen, BOOL bGrayscale);
  117. // Closes BMP file
  118. BOOL BmpFinalize();
  119. // The following structure stores window find data.
  120. struct FindWindowData
  121. {
  122. DWORD dwProcessId; // Process ID.
  123. BOOL bAllProcessWindows; // If TRUE, finds all process windows, else only the main one
  124. std::vector<WindowInfo>* paWindows; // Output array of window handles
  125. };
  126. /* Internal member variables. */
  127. CPoint m_ptCursorPos; // Current mouse cursor pos
  128. std::vector<CRect> m_arcCapture; // Array of capture rectangles
  129. CURSORINFO m_CursorInfo; // Cursor info
  130. int m_nIdStartFrom; // An ID for the current screenshot image
  131. CString m_sSaveDirName; // Directory name to save screenshots to
  132. SCREENSHOT_IMAGE_FORMAT m_fmt; // Image format
  133. int m_nJpegQuality; // Jpeg quality
  134. BOOL m_bGrayscale; // Create grayscale image or not
  135. FILE* m_fp; // Handle to the file
  136. png_structp m_png_ptr; // libpng stuff
  137. png_infop m_info_ptr; // libpng stuff
  138. struct jpeg_compress_struct m_cinfo; // libjpeg stuff
  139. struct jpeg_error_mgr m_jerr; // libjpeg stuff
  140. std::vector<MonitorInfo> m_monitor_list; // The list of monitor devices
  141. };
  142. #endif //__SCREENCAP_H__