Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdio.h>
- #include <algorithm>
- struct CStopWatch final
- {
- const char* title;
- LARGE_INTEGER frequency;
- LARGE_INTEGER startTime;
- CStopWatch( const char* pszTitle )
- : title( pszTitle )
- {
- ::QueryPerformanceFrequency( &frequency );
- ::QueryPerformanceCounter( &startTime );
- }
- ~CStopWatch( void )
- {
- LARGE_INTEGER endTime;
- ::QueryPerformanceCounter( &endTime );
- double elapsedSec = 1000 * (endTime.QuadPart - startTime.QuadPart) / (double)frequency.QuadPart;
- char buf[128];
- ::sprintf_s( buf, "%.3f msec, %s\n", elapsedSec, title );
- ::OutputDebugStringA( buf );
- }
- };
- constexpr size_t MAX_COUNT = 60;
- void case_FillRect(HDC hDC, const RECT& rc)
- {
- CStopWatch sw( __FUNCTION__ );
- for ( size_t n = 0; n < MAX_COUNT; n++ ) {
- COLORREF color = RGB(n, n, n);
- HBRUSH brush = ::CreateSolidBrush( color );
- ::FillRect( hDC, &rc, brush );
- ::DeleteObject( brush );
- }
- }
- void case_ExtTextOut(HDC hDC, const RECT& rc)
- {
- CStopWatch sw( __FUNCTION__ );
- for ( size_t n = 0; n < MAX_COUNT; n++ ) {
- COLORREF color = RGB(n, n, n);
- COLORREF bkColorOld = ::SetBkColor( hDC, color );
- ::ExtTextOutW( hDC, rc.left, rc.top, ETO_OPAQUE, &rc, L"", 0, NULL );
- ::SetBkColor( hDC, bkColorOld );
- }
- }
- void case_PatBlt(HDC hDC, const RECT& rc)
- {
- CStopWatch sw( __FUNCTION__ );
- for ( size_t n = 0; n < MAX_COUNT; n++ ) {
- COLORREF color = RGB(n, n, n);
- HBRUSH brush = ::CreateSolidBrush( color );
- HGDIOBJ brushOld = ::SelectObject( hDC, brush );
- ::PatBlt( hDC, rc.left, rc.top, rc.right, rc.bottom, PATCOPY );
- ::SelectObject( hDC, brushOld );
- ::DeleteObject( brush );
- }
- }
- static void FillRect_DIB(DWORD* pDIBPixels, RECT rc, int cx, int cy, COLORREF color)
- {
- LONG width = rc.right - rc.left;
- LONG height = rc.bottom - rc.top;
- if (rc.top < 0)
- {
- height += rc.top;
- rc.top = 0;
- }
- if (rc.left < 0)
- {
- width += rc.left;
- rc.left = 0;
- }
- if (rc.top >= cy || rc.left >= cx || height < 1 || width < 1)
- {
- return;
- }
- if (rc.left + width > cx)
- {
- width = cx - rc.left;
- }
- if (rc.top + height > cy)
- {
- height = cy - rc.top;
- }
- BYTE r = GetRValue(color);
- BYTE g = GetGValue(color);
- BYTE b = GetBValue(color);
- DWORD dwColor = (r << 16) | (g << 8) | b;
- pDIBPixels += cx * rc.top + rc.left;
- if (width == cx)
- {
- std::fill_n(pDIBPixels, cx * height, dwColor);
- }
- else
- {
- for (LONG i = 0; i < height; ++i)
- {
- std::fill_n(pDIBPixels, width, dwColor);
- pDIBPixels += cx;
- }
- }
- }
- void case_DIB(DWORD* pDIBPixels, const RECT& rc, int cx, int cy)
- {
- CStopWatch sw( __FUNCTION__ );
- for ( size_t n = 0; n < MAX_COUNT; n++ ) {
- COLORREF color = RGB(n, n, n);
- ::FillRect_DIB( pDIBPixels, rc, cx, cy, color );
- }
- }
- //! Main関数
- int WINAPI wWinMain(
- HINSTANCE hInstance, //!< handle to current instance
- HINSTANCE hPrevInstance, //!< handle to previous instance
- LPWSTR lpCmdLine, //!< pointer to command line
- int nCmdShow //!< show state of window
- )
- {
- ::SetProcessDPIAware();
- HWND hDesktopWnd = ::GetDesktopWindow();
- RECT rc;
- ::GetWindowRect( hDesktopWnd, &rc );
- int cx = rc.right - rc.left;
- int cy = rc.bottom - rc.top;
- HDC hDesktopDC = ::GetDC( hDesktopWnd );
- HDC hCompatibleDC = ::CreateCompatibleDC( NULL );
- HBITMAP hCompatibleBitmap = ::CreateCompatibleBitmap( hCompatibleDC, cx, cy );
- HGDIOBJ hPrevObj = ::SelectObject( hCompatibleDC, hCompatibleBitmap );
- BITMAPINFO bmi = {0};
- BITMAPINFOHEADER& bmih = bmi.bmiHeader;
- bmih.biSize = sizeof(bmih);
- bmih.biBitCount = 32;
- bmih.biWidth = cx;
- bmih.biHeight = -cy;
- bmih.biPlanes = 1;
- bmih.biCompression = BI_RGB;
- bmih.biSizeImage = 0;
- bmih.biXPelsPerMeter = 0;
- bmih.biYPelsPerMeter = 0;
- bmih.biClrUsed = 0;
- bmih.biClrImportant = 0;
- void* pvBits = nullptr;
- HBITMAP hDIB = CreateDIBSection(
- NULL,
- &bmi,
- DIB_RGB_COLORS,
- &pvBits,
- NULL,
- 0
- );
- for (int i = 0; i < 8; ++i)
- {
- char buff[64];
- sprintf_s(buff, "x, y, w, h : %d, %d, %d, %d\n", rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
- ::OutputDebugStringA(buff);
- case_FillRect( hCompatibleDC, rc );
- case_ExtTextOut( hCompatibleDC, rc );
- case_PatBlt( hCompatibleDC, rc );
- case_DIB( (DWORD*)pvBits, rc, cx, cy );
- rc.right >>= 1;
- rc.bottom >>= 1;
- }
- ::SelectObject( hCompatibleDC, hPrevObj );
- ::DeleteObject( hCompatibleBitmap );
- ::DeleteObject( hDIB );
- ::DeleteDC( hCompatibleDC );
- ::ReleaseDC( hDesktopWnd, hDesktopDC );
- return 0;
- }
Add Comment
Please, Sign In to add comment