'GDI+'에 관한 글 1개

GDI+에서 Round Rectangle 그리기.

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(); 
}
사용 예)
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
태그 : ,
글 걸기 주소 : 이 글에는 트랙백을 보낼 수 없습니다