API _ 선, 박스, 도형 그리기

출처

http://blog.naver.com/PostView.nhn?blogId=eunbear1030&logNo=70033469547&redirect=Dlog&widgetTypeCall=true

]선(라인)그리기

MoveToEx와 LineTo라는 두 개의 함수를 쌍으로 사용

MoveToEx함수는 그리는 시작점으로 그리기 좌표를 이동시키는 기능

LineTo함수는 MoveToEx함수를 실행한 후에 그 위치부터 라인을 그리고자 할때 사용

 

BOOL MoveToEx( 
HDC hdc, // 디바이스 컨텍스트 핸들
int X, // 새로운 위치의 x좌표
int Y, // 새로운 위치의 y자표
LPPOINT lpPoint // 이전 위치 좌표
);

 

BOOL LineTo( 
HDC hdc, // 디바이스 핸들
int nXEnd, // 그리는 좌표 종점 x
int nYEnd // 그리는 좌표 종점 y
);

 

]박스 그리기

박스를 그리는 함수 중 중요한 함수는 2개

  1. Rectangle함수 - 일반적인 박스를 그리는 함수
  2. RoundRect 함수 - 모서리가 둥근 박스를 그리는 함수

BOOL Rectangle( 
HDC hdc, // 디바이스 컨텍스트 핸들 
int nLeftRect, // 좌측 x좌표
int nTopRect, // 상단 y좌표
int nRightRect, // 우측 x좌표
int nBottomRect //하단 y좌표
);

BOOL RoundRect( 
HDC hdc, // 디바이스 컨텍스트 핸들
int nLeftRect, // 좌측 x좌표
int nTopRect, // 상단  y좌표
int nRightRect, // 우측 x좌표
int nBottomRect, // 하단 y좌표
int nWidth, // 모서리의 가로측 지름
int nHeight // 모서리의 세로측 지름
); 

]다양한 원 그리기

  1. Ellipse함수 - 설정한 박스영역에 맞는 원을 그려줌
  2. Arc함수 - 설정된 영역에 그리기 시작좌표 (x,y)에서 종료지점 (x,y)까지 원호를 그려줌
  3. Chord함수 - 현을 그려줌
  4. Pie함수 - 부채꼴을 그려줌

BOOL Ellipse( 
HDC hdc, // handle to device context 
int nLeftRect, // 좌측 x좌표
int nTopRect, // 상단 y좌표
int nRightRect, // 우측 x좌표
int nBottomRect // 하단 y좌표

);

BOOL Arc( 
HDC hdc, // 디바이스 컨텍스 핸들 
int nLeftRect, // 좌측 x좌표
int nTopRect, // 상단 y좌표
int nRightRect, // 우측 x좌표
int nBottomRect, // 하단 y좌표
int nXRadial1, // 그리기를 시작하는 x지점
int nYRadial1, // 그리기를 시작하는 y지점
int nXRadial2, // 그리기를 종료하는 x지점
int nYRadial2 // 그리기를 종료하는 y지점
);

BOOL Chord( 
HDC hdc, // 디비이스 컨텍스 핸들
int nLeftRect, // 좌측 x좌표
int nTopRect, // 상단 y좌표
int nRightRect, // 우측 x좌표
int nBottomRect, //하단 y좌표 
int nXRadial1, // 그리기를 시작하는 x지점
int nYRadial1, // 그리기를 시작하는 y지점
int nXRadial2, // 그리기를 종료하는 x지점
int nYRadial2 // 그리기를 종료하는 y지점

);

BOOL Pie( 
HDC hdc, // 디비이스 컨텍스 핸들
int nLeftRect, // 좌측 x좌표
int nTopRect, // 상단 y좌표
int nRightRect, // 우측 x좌표
int nBottomRect, //하단 y좌표 
int nXRadial1, // 그리기를 시작하는 x지점
int nYRadial1, // 그리기를 시작하는 y지점
int nXRadial2, // 그리기를 종료하는 x지점
int nYRadial2 // 그리기를 종료하는 y지점

);


]다각형과 poly Line

다각형은 여러점을 연결하는 도형을 의미, Poly Line은 여러점에 직선을 연결한 것을 의미

 

Polygon함수 - 다각형을 그릴때 이용

Polyline함수 - Poly Line을 그릴때 이용

 

BOOL Polyline( 
HDC hdc, // 디바이스 컨텍스트 핸들
CONST POINT *lppt, // 그릴 좌표
int cPoints // 좌표의 수
); 
BOOL Polygon( 
HDC hdc, // 디바이스 컨텍스트 핸들
CONST POINT *lppt, // 그릴 좌표
int cPoints // 좌표의 수
); 

]베지어 곡선 그리기

베지어곡선은 설정해준 최소 3개의 점 사이로 곡선이 그려지는 것을 의미, PolyBezier함수를 이용

 

BOOL PolyBezier( 
HDC hdc, // 디바이스 컨텍스트 핸들
CONST POINT *lppt, // 그릴 좌표
int cPoints // 좌표의 수
); 

*` 소스 *

 

#include <windows.h>


LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "DrawEx" ;
HWND        hwnd ;
MSG         msg ;
WNDCLASSEX  wndclass ;

 

wndclass.cbSize        = sizeof (wndclass) ;
wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc   = WndProc ;
wndclass.cbClsExtra    = 0 ;
wndclass.cbWndExtra    = 0 ;
wndclass.hInstance     = hInstance ;
wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName  = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

 

RegisterClassEx (&wndclass) ;

 

hwnd = CreateWindow (szAppName,     
"기본그리기예제:DrawEx",   
WS_OVERLAPPEDWINDOW,  
CW_USEDEFAULT,        
CW_USEDEFAULT,        
CW_USEDEFAULT,        
CW_USEDEFAULT,        
NULL,                 
NULL,                 
hInstance,            
NULL) ;           

 

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

 

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

 

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HDC         hdc ;
PAINTSTRUCT ps ;
RECT rect;
POINT prevpos;
POINT pline[6]={50,133,146,99,246,133,247,212,58,216,50,133};
POINT pgon[4]={308,120,440,118,380,212,308,120};

 

switch (iMsg)
{
case WM_CREATE :
return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect(hwnd,&rect);
//박스 그리기
Rectangle(hdc,rect.left+10,rect.top+10,rect.right-10,rect.bottom-10);
//라운드 박스 그리기    
RoundRect(hdc,rect.left+20,rect.top+20,rect.right-20,rect.bottom-20,20,20);
//원 그리기
Ellipse(hdc,rect.left+30,rect.top+30,rect.right-30,rect.bottom-30);
Arc(hdc,0,0,100,100,50,0,100,50);
//직선 그리기
MoveToEx(hdc,rect.left+10,rect.top+10,&prevpos);
LineTo(hdc,rect.right-10,rect.bottom-10);
//베지어 곡선
PolyBezier(hdc,pline,4);
//폴리 라인
Polyline(hdc,pline,6);
//다각형
Polygon(hdc,pgon,4);
EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}

return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

 

*`출력 결과*