Skip to content

Instantly share code, notes, and snippets.

@daramkun
Created January 19, 2015 12:01
Show Gist options
  • Select an option

  • Save daramkun/e0514df897560b1fa2a2 to your computer and use it in GitHub Desktop.

Select an option

Save daramkun/e0514df897560b1fa2a2 to your computer and use it in GitHub Desktop.
#include <Windows.h>
int x = 0, y = 0;
LRESULT CALLBACK WndProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch ( uMsg )
{
case WM_PAINT:
{
PAINTSTRUCT ps;
// 창의 기본 DC를 가져온다
HDC hDC = BeginPaint ( hWnd, &ps );
// 기본 DC의 호환 DC (Compatible DC)를 생성한다
// 호환 DC는 기본 DC와 시스템적으로 동일한 구성 (색의 가지수 등)을 가지게 된다
HDC hCompDC = CreateCompatibleDC ( hDC );
// 기본 DC의 호환 비트맵 (Compatible Bitmap)을 생성한다.
// 동일한 색상 깊이를 가지게 되므로 색이 깨지지 않는다.
HBITMAP hCompBitmap = CreateCompatibleBitmap ( hDC, 800, 600 );
// 호환 DC에 호환 비트맵을 적용
HBITMAP oldBitmap = ( HBITMAP ) SelectObject ( hCompDC, hCompBitmap );
// 예제용으로 사용할 브러시와 펜 생성
HBRUSH whiteBrush = CreateSolidBrush ( RGB ( 255, 255, 255 ) );
HBRUSH oldBrush = ( HBRUSH ) SelectObject ( hCompDC, whiteBrush );
HPEN whitePen = CreatePen ( PS_SOLID, 2, RGB ( 255, 255, 255 ) );
HPEN oldPen = ( HPEN ) SelectObject ( hCompDC, whitePen );
// 예제용 선
MoveToEx ( hCompDC, 10, 10, nullptr );
LineTo ( hCompDC, 100, 100 );
// 예제용 사각형. 키보드 키에 따라 움직인다
Rectangle ( hCompDC, 200 + x, 200 + y, 250 + x, 250 + y );
// 사용이 끝난 펜과 브러시를 원상태로 되돌린다
SelectObject ( hCompDC, oldPen );
SelectObject ( hCompDC, oldBrush );
// 호환 DC를 기본 DC에 BitBlt로 고속으로 복사한다
BitBlt ( hDC, 0, 0, 800, 600, hCompDC, 0, 0, SRCCOPY );
// 사용이 끝난 호환 DC 및 비트맵을 제거한다.
SelectObject ( hCompDC, oldBitmap );
DeleteObject ( hCompBitmap );
DeleteDC ( hCompDC );
// 창의 기본 DC를 해제한다
EndPaint ( hWnd, &ps );
}
break;
case WM_KEYDOWN:
switch ( wParam )
{
case VK_LEFT:
x -= 10;
break;
case VK_RIGHT:
x += 10;
break;
case VK_UP:
y -= 10;
break;
case VK_DOWN:
y += 10;
break;
}
break;
case WM_CLOSE:
PostQuitMessage ( 0 );
break;
default:
return DefWindowProc ( hWnd, uMsg, wParam, lParam );
}
return 0;
}
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
{
WNDCLASS wndClass = { CS_VREDRAW | CS_HREDRAW, WndProc, 0, 0, hInstance, 0, 0, ( HBRUSH ) GetStockObject ( WHITE_BRUSH ), nullptr, L"asdf" };
RegisterClass ( &wndClass );
RECT rect = { 0, 0, 800, 600 };
AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW, FALSE );
int width = rect.right - rect.left, height = rect.bottom - rect.top;
int x = GetSystemMetrics ( SM_CXSCREEN ) / 2 - width / 2, y = GetSystemMetrics ( SM_CYSCREEN ) / 2 - height / 2;
HWND hWnd = CreateWindow ( L"asdf", L"Test", WS_OVERLAPPEDWINDOW, x, y, width, height, nullptr, nullptr, hInstance, nullptr );
ShowWindow ( hWnd, SW_SHOW );
UpdateWindow ( hWnd );
MSG msg;
while ( true )
{
if ( PeekMessage ( &msg, nullptr, 0, 0, PM_NOREMOVE ) )
{
if ( !GetMessage ( &msg, nullptr, 0, 0 ) )
break;
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
else
{
InvalidateRect ( hWnd, nullptr, FALSE );
Sleep ( 1 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment