1. GDI Object 기본
2. 지뢰찾기 기본 모양 만들어보기( Draw3dRect 함수가 핵심 )
void Draw3dRect(HDC hdc, int x, int y, int xx, int yy, BOOL down, int width )
{
COLORREF clrMain = RGB(192,192,192),
clrLight = RGB(255,255,255),
clrDark = RGB(128,128,128);
HPEN hPen1, hPen2, hOldPen;
HBRUSH hBrush, hOldBrush;
if(down)
{
hPen2 = CreatePen(PS_SOLID,1,clrLight);
hPen1 = CreatePen(PS_SOLID,1,clrDark);
}
else
{
hPen1 = CreatePen(PS_SOLID,1,clrLight);
hPen2 = CreatePen(PS_SOLID,1,clrDark);
}
hBrush = CreateSolidBrush( clrMain );
hOldPen = (HPEN)SelectObject(hdc, hPen1);
hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Rectangle(hdc, x , y, xx+1, yy+1);
for(int i=0; i < width; i++)
{
SelectObject(hdc, hPen1);
MoveToEx(hdc, xx - 1, y, 0 );
LineTo(hdc, x, y);
LineTo(hdc, x, yy - 1 );
SelectObject(hdc, hPen2);
MoveToEx(hdc, x, yy,0);
LineTo(hdc, xx, yy);
LineTo(hdc, xx, y);
x++; y++; xx--; yy--;
}
SelectObject(hdc, hOldPen);
SelectObject(hdc, hOldBrush);
DeleteObject(hPen1);
DeleteObject(hPen2);
DeleteObject(hBrush);
}
#define ORG_X 12
#define ORG_Y 55
#define BLOCKSIZE 16
void DrawBlock( HDC hdc, int x, int y )
{
Draw3dRect( hdc, ORG_X + x * BLOCKSIZE , ORG_Y + y * BLOCKSIZE,
ORG_X + (x+1) * BLOCKSIZE -1 , ORG_Y + (y+1) * BLOCKSIZE -1,
FALSE, 2);
}
enum STATE { EMPTY = 0, ONE = 1, TWO = 2, EIGHT = 8, BOMB = 99 };
struct BLOCK
{
STATE state;
int isOpen;
};
#define CLOSE 0
#define OPEN 1
#define FLAG 2
BLOCK board[9][9];
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_LBUTTONDOWN:
{
int i = 0, j = 0;
// lParam 에서 x, y를 구한다.
// 몇번째 블록인지 구한다. i, j
if( board[i][j].state == BOMB ) { /* 꽝 */ };
if( board[i][j].isOpen == FLAG | OPEN ) return 0;
if( board[i][j].state == EMPTY )
{
// 주변을 열어 준다.
}
else
{
board[i][j].isOpen = OPEN;
// 해당 블록의 크기를 정확히 구해서.. 무효화 한뒤에
// WM_PAINT가 다시 그리게 한다.
}
}
case WM_PAINT:
{
int i, j;
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
Draw3dRect( hdc, 0, 0, 168, 211, FALSE, 3 );
Draw3dRect( hdc, 9, 9, 159, 46, TRUE, 2 );
Draw3dRect( hdc, 15, 14, 57, 40, TRUE, 1 );
Draw3dRect( hdc, 109, 14, 150, 40, TRUE, 1 );
Draw3dRect( hdc, 9, 52, 159, 202, TRUE, 3 );
for ( i = 0; i < 9; ++i )
{
for ( j = 0; j < 9; ++j )
{
if( board[i][j].isOpen == CLOSE )
DrawBlock(hdc, i, j );
else if( FLAG )
{
// 블록을 그리고 깃발을 그린다.
}
else
{
//board[i][j] 가 가지고 있는 숫자를 출력
//비트맵 사용
}
}
}
EndPaint( hwnd, &ps );
}
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
Draw3dRect( hdc, 10, 10, 100, 100, TRUE, 5 );
// 1. GDI Object 생성..
HPEN hPen = CreatePen( PS_DASH, 5, RGB( 255, 0, 0 ) );
// 2. DC에 선택
HPEN old_pen = (HPEN)SelectObject( hdc, hPen );
Rectangle( hdc, 10, 10, 100, 100 );
// 3. DC에서 선택해지 - 이전의 디폴트 펜을 넣는다.
// BLACK_PEN 은 Default Object Pen 이다.!!!
SelectObject( hdc, GetStockObject( BLACK_PEN ) );
//SelectObject( hdc, old_pen );
// 4. GDI Object 파괴
DeleteObject( hPen );
EndPaint( hwnd, &ps );
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hwnd, msg, wParam, lParam);
}
more..
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
Draw3dRect( hdc, 10, 10, 100, 100, TRUE, 5 );
// 1. GDI Object 생성..
HPEN hPen = CreatePen( PS_DASH, 5, RGB( 255, 0, 0 ) );
// 2. DC에 선택
HPEN old_pen = (HPEN)SelectObject( hdc, hPen );
Rectangle( hdc, 10, 10, 100, 100 );
// 3. DC에서 선택해지 - 이전의 디폴트 펜을 넣는다.
// BLACK_PEN 은 Default Object Pen 이다.!!!
SelectObject( hdc, GetStockObject( BLACK_PEN ) );
//SelectObject( hdc, old_pen );
// 4. GDI Object 파괴
DeleteObject( hPen );
EndPaint( hwnd, &ps );
}
return 0;
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
Draw3dRect( hdc, 10, 10, 100, 100, TRUE, 5 );
// 1. GDI Object 생성..
HPEN hPen = CreatePen( PS_DASH, 5, RGB( 255, 0, 0 ) );
// 2. DC에 선택
HPEN old_pen = (HPEN)SelectObject( hdc, hPen );
Rectangle( hdc, 10, 10, 100, 100 );
// 3. DC에서 선택해지 - 이전의 디폴트 펜을 넣는다.
// BLACK_PEN 은 Default Object Pen 이다.!!!
SelectObject( hdc, GetStockObject( BLACK_PEN ) );
//SelectObject( hdc, old_pen );
// 4. GDI Object 파괴
DeleteObject( hPen );
EndPaint( hwnd, &ps );
}
return 0;
2. 지뢰찾기 기본 모양 만들어보기( Draw3dRect 함수가 핵심 )
more..
void Draw3dRect(HDC hdc, int x, int y, int xx, int yy, BOOL down, int width )
{
COLORREF clrMain = RGB(192,192,192),
clrLight = RGB(255,255,255),
clrDark = RGB(128,128,128);
HPEN hPen1, hPen2, hOldPen;
HBRUSH hBrush, hOldBrush;
if(down)
{
hPen2 = CreatePen(PS_SOLID,1,clrLight);
hPen1 = CreatePen(PS_SOLID,1,clrDark);
}
else
{
hPen1 = CreatePen(PS_SOLID,1,clrLight);
hPen2 = CreatePen(PS_SOLID,1,clrDark);
}
hBrush = CreateSolidBrush( clrMain );
hOldPen = (HPEN)SelectObject(hdc, hPen1);
hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Rectangle(hdc, x , y, xx+1, yy+1);
for(int i=0; i < width; i++)
{
SelectObject(hdc, hPen1);
MoveToEx(hdc, xx - 1, y, 0 );
LineTo(hdc, x, y);
LineTo(hdc, x, yy - 1 );
SelectObject(hdc, hPen2);
MoveToEx(hdc, x, yy,0);
LineTo(hdc, xx, yy);
LineTo(hdc, xx, y);
x++; y++; xx--; yy--;
}
SelectObject(hdc, hOldPen);
SelectObject(hdc, hOldBrush);
DeleteObject(hPen1);
DeleteObject(hPen2);
DeleteObject(hBrush);
}
#define ORG_X 12
#define ORG_Y 55
#define BLOCKSIZE 16
void DrawBlock( HDC hdc, int x, int y )
{
Draw3dRect( hdc, ORG_X + x * BLOCKSIZE , ORG_Y + y * BLOCKSIZE,
ORG_X + (x+1) * BLOCKSIZE -1 , ORG_Y + (y+1) * BLOCKSIZE -1,
FALSE, 2);
}
enum STATE { EMPTY = 0, ONE = 1, TWO = 2, EIGHT = 8, BOMB = 99 };
struct BLOCK
{
STATE state;
int isOpen;
};
#define CLOSE 0
#define OPEN 1
#define FLAG 2
BLOCK board[9][9];
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_LBUTTONDOWN:
{
int i = 0, j = 0;
// lParam 에서 x, y를 구한다.
// 몇번째 블록인지 구한다. i, j
if( board[i][j].state == BOMB ) { /* 꽝 */ };
if( board[i][j].isOpen == FLAG | OPEN ) return 0;
if( board[i][j].state == EMPTY )
{
// 주변을 열어 준다.
}
else
{
board[i][j].isOpen = OPEN;
// 해당 블록의 크기를 정확히 구해서.. 무효화 한뒤에
// WM_PAINT가 다시 그리게 한다.
}
}
case WM_PAINT:
{
int i, j;
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
Draw3dRect( hdc, 0, 0, 168, 211, FALSE, 3 );
Draw3dRect( hdc, 9, 9, 159, 46, TRUE, 2 );
Draw3dRect( hdc, 15, 14, 57, 40, TRUE, 1 );
Draw3dRect( hdc, 109, 14, 150, 40, TRUE, 1 );
Draw3dRect( hdc, 9, 52, 159, 202, TRUE, 3 );
for ( i = 0; i < 9; ++i )
{
for ( j = 0; j < 9; ++j )
{
if( board[i][j].isOpen == CLOSE )
DrawBlock(hdc, i, j );
else if( FLAG )
{
// 블록을 그리고 깃발을 그린다.
}
else
{
//board[i][j] 가 가지고 있는 숫자를 출력
//비트맵 사용
}
}
}
EndPaint( hwnd, &ps );
}
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
Draw3dRect( hdc, 10, 10, 100, 100, TRUE, 5 );
// 1. GDI Object 생성..
HPEN hPen = CreatePen( PS_DASH, 5, RGB( 255, 0, 0 ) );
// 2. DC에 선택
HPEN old_pen = (HPEN)SelectObject( hdc, hPen );
Rectangle( hdc, 10, 10, 100, 100 );
// 3. DC에서 선택해지 - 이전의 디폴트 펜을 넣는다.
// BLACK_PEN 은 Default Object Pen 이다.!!!
SelectObject( hdc, GetStockObject( BLACK_PEN ) );
//SelectObject( hdc, old_pen );
// 4. GDI Object 파괴
DeleteObject( hPen );
EndPaint( hwnd, &ps );
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hwnd, msg, wParam, lParam);
}