Guest User

Untitled

a guest
Jan 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <algorithm>
  4.  
  5. struct CStopWatch final
  6. {
  7. const char* title;
  8. LARGE_INTEGER frequency;
  9. LARGE_INTEGER startTime;
  10.  
  11. CStopWatch( const char* pszTitle )
  12. : title( pszTitle )
  13. {
  14. ::QueryPerformanceFrequency( &frequency );
  15. ::QueryPerformanceCounter( &startTime );
  16. }
  17. ~CStopWatch( void )
  18. {
  19. LARGE_INTEGER endTime;
  20. ::QueryPerformanceCounter( &endTime );
  21. double elapsedSec = 1000 * (endTime.QuadPart - startTime.QuadPart) / (double)frequency.QuadPart;
  22. char buf[128];
  23. ::sprintf_s( buf, "%.3f msec, %s\n", elapsedSec, title );
  24. ::OutputDebugStringA( buf );
  25. }
  26. };
  27.  
  28. constexpr size_t MAX_COUNT = 60;
  29.  
  30. void case_FillRect(HDC hDC, const RECT& rc)
  31. {
  32. CStopWatch sw( __FUNCTION__ );
  33. for ( size_t n = 0; n < MAX_COUNT; n++ ) {
  34. COLORREF color = RGB(n, n, n);
  35. HBRUSH brush = ::CreateSolidBrush( color );
  36. ::FillRect( hDC, &rc, brush );
  37. ::DeleteObject( brush );
  38. }
  39. }
  40.  
  41. void case_ExtTextOut(HDC hDC, const RECT& rc)
  42. {
  43. CStopWatch sw( __FUNCTION__ );
  44. for ( size_t n = 0; n < MAX_COUNT; n++ ) {
  45. COLORREF color = RGB(n, n, n);
  46. COLORREF bkColorOld = ::SetBkColor( hDC, color );
  47. ::ExtTextOutW( hDC, rc.left, rc.top, ETO_OPAQUE, &rc, L"", 0, NULL );
  48. ::SetBkColor( hDC, bkColorOld );
  49. }
  50. }
  51.  
  52. void case_PatBlt(HDC hDC, const RECT& rc)
  53. {
  54. CStopWatch sw( __FUNCTION__ );
  55. for ( size_t n = 0; n < MAX_COUNT; n++ ) {
  56. COLORREF color = RGB(n, n, n);
  57. HBRUSH brush = ::CreateSolidBrush( color );
  58. HGDIOBJ brushOld = ::SelectObject( hDC, brush );
  59. ::PatBlt( hDC, rc.left, rc.top, rc.right, rc.bottom, PATCOPY );
  60. ::SelectObject( hDC, brushOld );
  61. ::DeleteObject( brush );
  62. }
  63. }
  64.  
  65. static void FillRect_DIB(DWORD* pDIBPixels, RECT rc, int cx, int cy, COLORREF color)
  66. {
  67. LONG width = rc.right - rc.left;
  68. LONG height = rc.bottom - rc.top;
  69.  
  70. if (rc.top < 0)
  71. {
  72. height += rc.top;
  73. rc.top = 0;
  74. }
  75. if (rc.left < 0)
  76. {
  77. width += rc.left;
  78. rc.left = 0;
  79. }
  80. if (rc.top >= cy || rc.left >= cx || height < 1 || width < 1)
  81. {
  82. return;
  83. }
  84. if (rc.left + width > cx)
  85. {
  86. width = cx - rc.left;
  87. }
  88. if (rc.top + height > cy)
  89. {
  90. height = cy - rc.top;
  91. }
  92.  
  93. BYTE r = GetRValue(color);
  94. BYTE g = GetGValue(color);
  95. BYTE b = GetBValue(color);
  96. DWORD dwColor = (r << 16) | (g << 8) | b;
  97.  
  98. pDIBPixels += cx * rc.top + rc.left;
  99.  
  100. if (width == cx)
  101. {
  102. std::fill_n(pDIBPixels, cx * height, dwColor);
  103. }
  104. else
  105. {
  106. for (LONG i = 0; i < height; ++i)
  107. {
  108. std::fill_n(pDIBPixels, width, dwColor);
  109. pDIBPixels += cx;
  110. }
  111. }
  112. }
  113.  
  114. void case_DIB(DWORD* pDIBPixels, const RECT& rc, int cx, int cy)
  115. {
  116. CStopWatch sw( __FUNCTION__ );
  117. for ( size_t n = 0; n < MAX_COUNT; n++ ) {
  118. COLORREF color = RGB(n, n, n);
  119. ::FillRect_DIB( pDIBPixels, rc, cx, cy, color );
  120. }
  121. }
  122.  
  123. //! Main関数
  124. int WINAPI wWinMain(
  125. HINSTANCE hInstance, //!< handle to current instance
  126. HINSTANCE hPrevInstance, //!< handle to previous instance
  127. LPWSTR lpCmdLine, //!< pointer to command line
  128. int nCmdShow //!< show state of window
  129. )
  130. {
  131. ::SetProcessDPIAware();
  132.  
  133. HWND hDesktopWnd = ::GetDesktopWindow();
  134. RECT rc;
  135. ::GetWindowRect( hDesktopWnd, &rc );
  136. int cx = rc.right - rc.left;
  137. int cy = rc.bottom - rc.top;
  138.  
  139. HDC hDesktopDC = ::GetDC( hDesktopWnd );
  140. HDC hCompatibleDC = ::CreateCompatibleDC( NULL );
  141. HBITMAP hCompatibleBitmap = ::CreateCompatibleBitmap( hCompatibleDC, cx, cy );
  142. HGDIOBJ hPrevObj = ::SelectObject( hCompatibleDC, hCompatibleBitmap );
  143.  
  144. BITMAPINFO bmi = {0};
  145. BITMAPINFOHEADER& bmih = bmi.bmiHeader;
  146. bmih.biSize = sizeof(bmih);
  147. bmih.biBitCount = 32;
  148. bmih.biWidth = cx;
  149. bmih.biHeight = -cy;
  150. bmih.biPlanes = 1;
  151. bmih.biCompression = BI_RGB;
  152. bmih.biSizeImage = 0;
  153. bmih.biXPelsPerMeter = 0;
  154. bmih.biYPelsPerMeter = 0;
  155. bmih.biClrUsed = 0;
  156. bmih.biClrImportant = 0;
  157. void* pvBits = nullptr;
  158. HBITMAP hDIB = CreateDIBSection(
  159. NULL,
  160. &bmi,
  161. DIB_RGB_COLORS,
  162. &pvBits,
  163. NULL,
  164. 0
  165. );
  166.  
  167.  
  168. for (int i = 0; i < 8; ++i)
  169. {
  170. char buff[64];
  171. sprintf_s(buff, "x, y, w, h : %d, %d, %d, %d\n", rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
  172. ::OutputDebugStringA(buff);
  173. case_FillRect( hCompatibleDC, rc );
  174. case_ExtTextOut( hCompatibleDC, rc );
  175. case_PatBlt( hCompatibleDC, rc );
  176. case_DIB( (DWORD*)pvBits, rc, cx, cy );
  177. rc.right >>= 1;
  178. rc.bottom >>= 1;
  179. }
  180.  
  181. ::SelectObject( hCompatibleDC, hPrevObj );
  182. ::DeleteObject( hCompatibleBitmap );
  183. ::DeleteObject( hDIB );
  184. ::DeleteDC( hCompatibleDC );
  185. ::ReleaseDC( hDesktopWnd, hDesktopDC );
  186. return 0;
  187. }
Add Comment
Please, Sign In to add comment