void CGdiPlusDemoView::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 Graphics graphics(dc);

 Pen BluePen(Color(255,0,0,255), 20.0f);  //Color의 첫번째 인자는 알파채널이고 뒤부터는 rgb값.
 Pen BlackPen(Color(128,0,0,0), 20.0f);   //뒤의 20.0f 값인 REAL 형은 float형을 다른 이름으로 재정의한 것이며,
// 부동 소수점단위로 펜의 두께를 명시하는 역활을 한다.

 graphics.DrawLine(&BluePen, Point(10,50), Point(210,50));
 graphics.DrawLine(&BlackPen, Point(10,250), Point(210,250));
}


Graphics 클래스는 CDC 클래스처럼 그리기의 대상을 추상화한 GDI+ 클래스이며, 대부분의 그리기 코드는 이 클래스의 메서드에 집중된다. 그러므로 SelectObject() 함수를 호출하지 않아도 된다.





꺽임 처리

다음은 만일 여러 직선을 이어서 그리거나 다각형을 그릴 때 각각의 직선이 만나는 부분이 발생하면 펜의 설정에 따라 적절히 렌더링을 하게 된다.

void CGdiPlusDemoView::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 Graphics graphics(dc);

 Pen BluePen(Color(255,0,0,255), 20.0f);

 Point pt1(30,10);
 Point pt2(30,110);
 Point pt3(230,20);
 Point pt4(230,120);
 Point points[4] = {pt1,pt2,pt3,pt4};

 BluePen.SetLineJoin(LineJoinRound);//꺽인 부분을 어떻게 렌더링할지 설정.
 //GdiplusEnums.h 파일에 열거형으로 정의되어 있다.
 BluePen.SetDashStyle(DashStyleDot); // 점선으로 변환.
 graphics.DrawLines(&BluePen, points, 4);
}


끝부분 처리

일반 GDI 프로그래밍에서는 펜의 시작 부분(start cap)과 끝 부분(end cap)을 렌더링하는 방법이 세 종류였으나, GDI+ 에서는 다양한 모양을 구현할 수 있다. 다음은 펜의 시작과 끝 부분을 둥근 원과 화살표로 렌더링하는 예다.

void CGdiPlusDemoView::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 Graphics graphics(dc);

 Pen BluePen(Color(255,0,0,255), 20.0f);

 Point pt1(30,10);
 Point pt2(30,110);
 Point pt3(230,20);
 Point pt4(230,120);
 Point points[4] = {pt1,pt2,pt3,pt4};

 BluePen.SetLineJoin(LineJoinRound);

 BluePen.SetStartCap(LineCapRoundAnchor); //둥근 원으로 시작
 BluePen.SetEndCap(LineCapArrowAnchor); //화살표로 끝

 graphics.DrawLines(&BluePen, points, 4);
}


끝부분 처리와 안티 에일리어싱

그래프를 개발할 때 각각의 선을 잇는 곡선으로 그래프를 만드는데, 이때 Graphics 클래스의 DrawCurve() 메서드를 활용하면 구현이 쉽다. 다음은 안티 에일리어싱까지 적용한 코드다. 안티 에일리어싱은 단 한줄로 끝난다.

 void CGdiPlusDemoView::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 Graphics graphics(dc);
 graphics.SetSmoothingMode(SmoothingModeHighQuality); //안티 에일리어싱 적용

 Pen RedPen(Color(255,255,0,0), 2.0f);
 Pen GreenPen(Color(255,0,255,0), 2.0f);
 Pen BluePen(Color(255,0,0,255), 2.0f);
 
 Point pts[6] = { 
      Point(10,150),
      Point(110,10),
      Point(170,250),
      Point(220,120),
      Point(270,150),
      Point(350,150)
    };

 graphics.DrawCurve(&RedPen, pts, 6, 0.0f); //마지막인자는 tension 값으로 곡선을 그릴 때 끝을 강제로 늘려준다.
 graphics.DrawCurve(&GreenPen, pts, 6, 0.5f);
 graphics.DrawCurve(&BluePen, pts, 6, 1.0f);

//곡선을 그리는 함수로는 DrawBezier() 라는 함수도 있으며, 4개의 좌표를 이용하여 베지어 곡선을 그린다.

}






'Windows > MFC' 카테고리의 다른 글

GDI+ , 브러쉬(Brush)  (0) 2011.12.13
GDI+ , 도형 그리기  (0) 2011.12.13
데이터베이스 :: OLE-DB  (0) 2011.11.23
MFC 소켓 프로그래밍 ( TCP/IP 이용 )  (0) 2011.11.20
데이터베이스 ( Database ) - DBMS,ODBC  (0) 2011.11.20

+ Recent posts