GDI+에서 Round Rectangle 그리기.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void GetRoundedRect(GraphicsPath* pPath, RectF baseRect, float radius){
    if( radius<=0.0F ){
        pPath->AddRectangle(baseRect);
        pPath->CloseFigure();
        return;
    }
    if( radius>=(min(baseRect.Width, baseRect.Height))/2.0){
        GetCapsule(pPath, baseRect);
        return;
    }
    float diameter = radius * 2.0F;
    RectF arc   ( baseRect.X, baseRect.Y, diameter, diameter);
    pPath->AddArc( arc, 180, 90 );
    arc.X = baseRect.GetRight() -diameter;
    pPath->AddArc( arc, 270, 90 );
    arc.Y = baseRect.GetBottom() -diameter;
    pPath->AddArc( arc, 0, 90 );
    arc.X = baseRect.GetLeft();
    pPath->AddArc( arc, 90, 90 );    
    pPath->CloseFigure();
}
 
void GetCapsule(GraphicsPath* pPath, RectF baseRect){
    float diameter;
    
    TRY{
        if( baseRect.Width > baseRect.Height){
            diameter = baseRect.Height;           
            RectF arc( baseRect.X, baseRect.Y, diameter, diameter);
            pPath->AddArc( arc, 90, 180);
            arc.X = baseRect.GetRight()-diameter;
            pPath->AddArc( arc, 270, 180);
        }else if( baseRect.Width < baseRect.Height ){
            diameter = baseRect.Width;
             
            RectF arc( baseRect.X, baseRect.Y, diameter, diameter );
            pPath->AddArc( arc, 180, 180 );
            arc.Y = baseRect.GetBottom() -diameter;
            pPath->AddArc( arc, 0, 180 );
        }else{
            pPath->AddEllipse( baseRect );
        }
    }CATCH(CException, ex){
        pPath->AddEllipse( baseRect );
    }
    END_CATCH
    pPath->CloseFigure();
}
사용 예)
1
2
3
4
5
6
7
8
9
10
RectF PaintRect(WndRect.Width()/2-200, WndRect.Height()/2-100, 400, 200);
 
LinearGradientBrush BackBrush(PaintRect , Color(255,255,255), Color(57,109,194), LinearGradientModeVertical);
Pen     BackPen  (Color(57,109,194), 3);
 
GraphicsPath RoundPath;
GetRoundedRect(&RoundPath, PaintRect, 10);
 
pMemG->FillPath(&BackBrush, &RoundPath);
pMemG->DrawPath(&BackPen,   &RoundPath);

자료 참고: codeproject.com

2008/03/14 19:00 2008/03/14 19:00
태그 : ,
글 걸기 주소 : 이 글에는 트랙백을 보낼 수 없습니다