Created
January 18, 2019 23:11
-
-
Save beru/d52f6335f42674e88f473e69297927ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <stdio.h> | |
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 = (endTime.QuadPart - startTime.QuadPart) / (double)frequency.QuadPart; | |
char buf[128]; | |
::sprintf_s( buf, "%s elapsed=%f sec\n", title, elapsedSec ); | |
::OutputDebugStringA( buf ); | |
} | |
}; | |
constexpr size_t MAX_COUNT = 60; | |
void case1(HDC hDC, const RECT& rc) | |
{ | |
CStopWatch sw( __FUNCTION__ ); | |
for ( size_t n = 0; n < MAX_COUNT; n++ ) { | |
HBRUSH brush = ::CreateSolidBrush( RGB( 0xff, 0xff, 0 ) ); | |
::FillRect( hDC, &rc, brush ); | |
::DeleteObject( brush ); | |
} | |
} | |
void case2(HDC hDC, const RECT& rc) | |
{ | |
CStopWatch sw( __FUNCTION__ ); | |
for ( size_t n = 0; n < MAX_COUNT; n++ ) { | |
COLORREF bkColor = RGB( 0xff, 0xff, 0 ); | |
COLORREF bkColorOld = ::SetBkColor( hDC, bkColor ); | |
::ExtTextOutW( hDC, rc.left, rc.top, ETO_OPAQUE, &rc, L"", 0, NULL ); | |
::SetBkColor( hDC, bkColorOld ); | |
} | |
} | |
void case3(HDC hDC, const RECT& rc) | |
{ | |
CStopWatch sw( __FUNCTION__ ); | |
for ( size_t n = 0; n < MAX_COUNT; n++ ) { | |
HBRUSH brush = ::CreateSolidBrush( RGB( 0xff, 0xff, 0 ) ); | |
HGDIOBJ brushOld = ::SelectObject( hDC, brush ); | |
::PatBlt( hDC, rc.left, rc.top, rc.right, rc.bottom, PATCOPY ); | |
::SelectObject( hDC, brushOld ); | |
::DeleteObject( brush ); | |
} | |
} | |
void case4(HDC hDC, const RECT& rc) | |
{ | |
CStopWatch sw( __FUNCTION__ ); | |
HBRUSH brush = ::CreateSolidBrush( RGB( 0xff, 0xff, 0 ) ); | |
for ( size_t n = 0; n < MAX_COUNT; n++ ) { | |
::FillRect( hDC, &rc, brush ); | |
} | |
::DeleteObject( brush ); | |
} | |
void case5(HDC hDC, const RECT& rc) | |
{ | |
CStopWatch sw( __FUNCTION__ ); | |
COLORREF bkColor = RGB( 0xff, 0xff, 0 ); | |
COLORREF bkColorOld = ::SetBkColor( hDC, bkColor ); | |
for ( size_t n = 0; n < MAX_COUNT; n++ ) { | |
::ExtTextOutW( hDC, rc.left, rc.top, ETO_OPAQUE, &rc, L"", 0, NULL ); | |
} | |
::SetBkColor( hDC, bkColorOld ); | |
} | |
void case6(HDC hDC, const RECT& rc) | |
{ | |
CStopWatch sw( __FUNCTION__ ); | |
HBRUSH brush = ::CreateSolidBrush( RGB( 0xff, 0xff, 0 ) ); | |
for ( size_t n = 0; n < MAX_COUNT; n++ ) { | |
HGDIOBJ brushOld = ::SelectObject( hDC, brush ); | |
::PatBlt( hDC, rc.left, rc.top, rc.right, rc.bottom, PATCOPY ); | |
::SelectObject( hDC, brushOld ); | |
} | |
::DeleteObject( brush ); | |
} | |
//! 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 | |
) | |
{ | |
HWND hWnd = ::GetDesktopWindow(); | |
RECT rc; | |
::GetWindowRect( hWnd, &rc ); | |
HDC hDC = ::GetDC( hWnd ); | |
HDC hMemoryDC = ::CreateCompatibleDC( NULL ); | |
int cx = rc.right - rc.left; | |
int cy = rc.bottom - rc.top; | |
HBITMAP hBitmap = ::CreateCompatibleBitmap( hMemoryDC, cx, cy ); | |
HGDIOBJ hPrevObj = ::SelectObject( hMemoryDC, hBitmap ); | |
auto runTestCases = [](HDC hDC, const RECT& rc) { | |
case1( hDC, rc ); | |
case2( hDC, rc ); | |
case3( hDC, rc ); | |
case4( hDC, rc ); | |
case5( hDC, rc ); | |
case6( hDC, rc ); | |
}; | |
::OutputDebugStringW( L"Desktop Window DC\n" ); | |
runTestCases(hDC, rc); | |
::OutputDebugStringW( L"Memory DC\n" ); | |
runTestCases( hMemoryDC, rc ); | |
::SelectObject( hMemoryDC, hPrevObj ); | |
::DeleteObject( hBitmap ); | |
::DeleteDC( hMemoryDC ); | |
::ReleaseDC( hWnd, hDC ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment