本篇我们学习圆角矩形的绘制,圆角矩形就是在矩形的四个边角做圆弧处理,使图形不会太生硬,增加柔和度和亲和力,在现代界面中大量运用。
圆角矩形函数如下:
//圆角矩形左上角坐标(left,top),右下角坐标(right, bottom),圆角宽度roundwidth,圆角高度roundheight
//画无填充圆角矩形
void roundrect( int left, int top, int right, int bottom, int roundwidth, int roundheight );
//画无边框填充圆角矩形
void solidroundrect( int left, int top, int right, int bottom, int roundwidth, int roundheight );
//画填充圆角矩形
void fillroundrect( int left, int top, int right, int bottom, int roundwidth, int roundheight );
//清空圆角矩形
void clearroundrect( int left, int top, int right, int bottom, int roundwidth, int roundheight );
其中(left,top)表示圆角矩形左上角坐标,(right, bottom)表示右下角坐标,
这里要加以解释的是圆角宽度roundwidth和圆角高度roundheight,它表示了圆角所在的椭圆的宽度和高度,也就是说圆角矩形的四个角不用直角,而是把一个宽roundwidth和高roundheight的椭圆分成四瓣,四个角分别取自同一个椭圆对应的四个部分。一般roundwidth、roundheight取相等值,表示一个正圆。而且当圆角矩形宽度和高度与圆角所在椭圆相等或者更小时,绘制出来的就是对应的椭圆。所以圆角矩形也可以用来绘制圆或者椭圆。
如代码:
roundrect(50,50,300,200,50,50);
setlinecolor(RGB(255,0,0));
rectangle(50,50,300,200);
setlinecolor(0);
roundrect(350,50,400,100,50,50);
setlinecolor(RGB(255,0,0));
rectangle(349,49,401,101);
结果如图:
一般圆角矩形用来做平面设计或界面设计比较多,那我们就尝试下用圆角矩形函数绘制几个按钮看看吧。
代码如下:
// RoundRect.cpp : 定义控制台应用程序的入口点。
//
#include "../import/include/CGBoard.h"
#include "math.h"
#ifdef _DEBUG
#pragma comment(lib,"../import/lib/SimpleCG_MDd.lib")
#else
#pragma comment(lib,"../import/lib/SimpleCG_MD.lib")
#endif
#define ADDCOLOR(x,y) ((x+y)>255?255:(x+y))
#define SUBCOLOR(x,y) ((x-y)<0?0:(x-y))
int g_nWidth = 500; //画面宽度
int g_nHeight= 400; //画面高度
void DrawButtonType(int nX, int nY, int nWidth, int nHeight,COLORREF nDark,COLORREF nLight)
{
int i=0;
int r=GetRValue(nDark);
int g=GetGValue(nDark);
int b=GetBValue(nDark);
setlinecolor(0);
setfillcolor(nDark);
fillroundrect( nX, nY, nX+nWidth, nY+nHeight, 10, 10 );
setfillcolor(nLight);
int nAdd=110*2/nHeight;
solidrectangle( nX+2, nY+2, nX+nWidth-2, nY+nHeight/2-5 );
for(i=nHeight/2;i<nHeight-5;++i)
{
setlinecolor(RGB(r,g,b));
line( nX+1, nY+i, nX+nWidth-1, nY+i);
r=ADDCOLOR(r,nAdd);
g=ADDCOLOR(g,nAdd);
b=ADDCOLOR(b,nAdd);
}
for(i=nHeight-5;i<nHeight-2;++i)
{
setlinecolor(RGB(r,g,b));
line( nX+2+(i-nHeight+5), nY+i, nX+nWidth-2-(i-nHeight+5), nY+i);
r=ADDCOLOR(r,nAdd);
g=ADDCOLOR(g,nAdd);
b=ADDCOLOR(b,nAdd);
}
}
//绘制按钮四个状态
void DrawButton( int nX, int nY, int nWidth, int nHeight, COLORREF nColor, int nType )
{
int r=GetRValue(nColor);
int g=GetGValue(nColor);
int b=GetBValue(nColor);
switch(nType)
{
case 1://HOVER
r = ADDCOLOR(r,50);
g = ADDCOLOR(g,50);
b = ADDCOLOR(b,50);
nColor = RGB(r,g,b);
break;
case 2://PUSH
r = SUBCOLOR(r,50);
g = SUBCOLOR(g,50);
b = SUBCOLOR(b,50);
nColor = RGB(r,g,b);
break;
case 3://CANCEL
r=150;
r = ADDCOLOR(r,50);
g = r;
b = r;
nColor = RGB(r,r,r);
break;
}
COLORREF nLight = RGB(r,g,b);
r=SUBCOLOR(GetRValue(nColor),60);
g=SUBCOLOR(GetGValue(nColor),60);
b=SUBCOLOR(GetBValue(nColor),60);
COLORREF nDark = RGB(r,g,b);
DrawButtonType(nX, nY, nWidth, nHeight, nDark, nLight);
}
void DrawProcess()
{
int i=0;
int j=0;
srand(GetTickCount());
for( j=0;j<3;++j)
{
COLORREF nColor = RGB(rand()%230,rand()%230,rand()%230);
for( i=0;i<4;++i)
{
DrawButton( j*140+10, 10+i*40, 120, 35, nColor, i);
}
}
for( j=0;j<3;++j)
{
COLORREF nColor = RGB(rand()%230,rand()%230,rand()%230);
for( i=0;i<4;++i)
{
DrawButton( j*140+10, 200+i*40, 120, 35, nColor, i);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//初始化
if( !ShowingBoard(g_nWidth,g_nHeight, DrawProcess))
return 1;
//关闭图库
CloseBoard();
return 0;
}
运行效果如图,我们给每种随机颜色绘制了按钮的四个状态,分别是正常状态、获得鼠标热点、按下、失效状态。
此外,因为圆角矩形的圆润曲线,同样在绘制卡通画方面也有不错的表现。第二个例子就用几只卡通小动物来展现一些圆角矩形在卡通画方面的运用。代码如下:
// RoundRectAnimal.cpp : 定义控制台应用程序的入口点。
//
#include "../import/include/CGBoard.h"
#include "math.h"
#ifdef _DEBUG
#pragma comment(lib,"../import/lib/SimpleCG_MDd.lib")
#else
#pragma comment(lib,"../import/lib/SimpleCG_MD.lib")
#endif
int g_nWidth = 500; //画面宽度
int g_nHeight= 400; //画面高度
void DrawElephant(int nX, int nY)
{
setlinewidth(2);
setfillcolor(RGB(120,200,230));
fillrectangle( nX+20, nY+70, nX+40, nY+85);
fillrectangle( nX+60, nY+70, nX+80, nY+85);
fillroundrect( nX, nY, nX+100, nY+80, 20, 20 );
setfillcolor(0);
solidcircle( nX + 20,nY+40,5);
curveline( nX+30, nY+20, nX+50, nY+10, -4 );
curveline( nX+50, nY+10, nX+70, nY+30, -6 );
curveline( nX+70, nY+30, nX+60, nY+50, -4 );
curveline( nX+60, nY+50, nX+30, nY+50, -6 );
curveline( nX+100, nY+40, nX+120, nY+30, -4 );
curveline( nX+120, nY+30, nX+120, nY+50, -4 );
curveline( nX+120, nY+50, nX+110, nY+40, -4 );
curveline( nX+110, nY+40, nX+140, nY+30, -4 );
curveline( nX, nY+40, nX-30, nY+70, -8 );
curveline( nX-30, nY+70, nX-30, nY+80, 3 );
curveline( nX-30, nY+80, nX, nY+55, 7 );
setfillcolor(RGB(120,200,230));
floodfill( nX - 3, nY+50, 0, 0);
}
void DrawFrog(int nX, int nY)
{
setfillcolor(RGB(160,200,20));
circle( nX+30, nY-5, 15);
circle( nX+70, nY-5, 15);
fillroundrect( nX, nY, nX+100, nY+80, 20, 20 );
curveline( nX+100, nY+50, nX+115, nY+45, -4 );
curveline( nX+115, nY+45, nX+120, nY+55, -3 );
curveline( nX+120, nY+55, nX+103, nY+75, -4 );
curveline( nX+103, nY+75, nX+108, nY+78, -1 );
line( nX+108, nY+80, nX+94, nY+80 );
floodfill( nX+103, nY+60, 0, 0);
curveline( nX, nY+50, nX-15, nY+45, 5 );
curveline( nX-15, nY+45, nX-20, nY+55, 3 );
curveline( nX-20, nY+55, nX-3, nY+75, 4 );
curveline( nX-3, nY+75, nX-8, nY+78, 1 );
line( nX-8, nY+80, nX+6, nY+80 );
floodfill( nX - 3, nY+60, 0, 0);
setfillcolor(0);
solidcircle( nX+38, nY-8, 5);
solidcircle( nX+78, nY-8, 5);
curveline( nX+40, nY+10, nX+60, nY+10, 6 );
setfillcolor(RGB(255,255,255));
solidellipse( nX+30, nY+20, nX+70, nY+80 );
curveline( nX+10, nY+35, nX+30, nY+15, 4 );
curveline( nX+30, nY+15, nX+35, nY+30, 6 );
curveline( nX+35, nY+30, nX+35, nY+45, 6 );
curveline( nX+35, nY+45, nX+10, nY+45, 6 );
curveline( nX+90, nY+35, nX+70, nY+15, -4 );
curveline( nX+70, nY+15, nX+65, nY+30, -6 );
curveline( nX+65, nY+30, nX+65, nY+45, -6 );
curveline( nX+65, nY+45, nX+90, nY+45, -6 );
}
void DrawDog(int nX, int nY)
{
POINT ptBody[]={{nX+70,nY+80}, {nX+75,nY+100}, {nX+85,nY+100}, {nX+85,nY+95}, {nX+130,nY+95}, {nX+130,nY+100}, {nX+140,nY+100}, {nX+140,nY+75}, {nX+110,nY+70}};
int pLenBody[]={ -3, 0, 0, -2, -2, 0, 2, 4 };
POINT ptTail[]={{nX+140,nY+75}, {nX+145,nY+60}, {nX+130,nY+72}};
int pLenTail[]={ 5, -4};
setfillcolor(RGB(230,160,110));
fillroundrect( nX, nY, nX+110, nY+80, 20, 20 );
polycurvelinevar( ptBody, pLenBody, sizeof(ptBody)/sizeof(ptBody[0]));
floodfill( nX+115, nY+75, 0, 0);
polycurvelinevar( ptTail, pLenTail, sizeof(ptTail)/sizeof(ptTail[0]));
floodfill( nX+140, nY+70, 0, 0);
curveline( nX, nY+25, nX+25, nY+20, 4 );
curveline( nX+25, nY+20, nX+30, nY, 4 );
setfillcolor(RGB(130,60,10));
floodfill( nX+3, nY+6, 0, 0);
setfillcolor(RGB(255,255,255));
fillcircle( nX+45, nY+20, 10);
setfillcolor(0);
solidcircle( nX+41, nY+20, 7);
curveline( nX+60, nY+10, nX+80, nY+50, 8 );
curveline( nX+80, nY+50, nX+95, nY+20, 8 );
}
void DrawRabbit(int nX, int nY)
{
setfillcolor(RGB(250,250,250));
fillroundrect( nX+30, nY-30, nX+50, nY+10, 20, 20 );
fillroundrect( nX+60, nY-30, nX+80, nY+10, 20, 20 );
setfillcolor(RGB(255,215,235));
solidroundrect( nX+35, nY-20, nX+45, nY+10, 10, 10 );
solidroundrect( nX+65, nY-20, nX+75, nY+10, 10, 10 );
setfillcolor(RGB(250,250,250));
fillroundrect( nX, nY, nX+110, nY+80, 20, 20 );
setfillcolor(0);
solidcircle( nX+30, nY+20, 7);
solidcircle( nX+80, nY+20, 7);
line(nX+55,nY+20, nX+55,nY+30);
line(nX+55,nY+30, nX+50,nY+35);
line(nX+55,nY+30, nX+60,nY+35);
setfillcolor(RGB(250,165,120));
fillellipse(nX+48,nY+40,nX+62, nY+75);
line(nX+48,nY+55, nX+55,nY+55);
line(nX+48,nY+50, nX+53,nY+50);
setlinecolor(RGB(0,160,80));
setlinewidth(4);
line(nX+45,nY+37, nX+55,nY+45);
line(nX+55,nY+37, nX+55,nY+45);
line(nX+65,nY+37, nX+55,nY+45);
setlinewidth(2);
setlinecolor(0);
setfillcolor(RGB(250,250,250));
fillroundrect( nX+43, nY+60, nX+53, nY+68, 8, 8);
fillroundrect( nX+57, nY+60, nX+67, nY+68, 8, 8);
setlinecolor(RGB(250,250,250));
line(nX+44,nY+58, nX+44,nY+70);
line(nX+66,nY+58, nX+66,nY+70);
}
void DrawProcess()
{
DrawElephant( 100, 40 );
DrawFrog( 300, 40 );
DrawDog( 100, 200 );
DrawRabbit( 300, 200 );
}
int _tmain(int argc, _TCHAR* argv[])
{
//初始化
if( !ShowingBoard(g_nWidth,g_nHeight, DrawProcess))
return 1;
//关闭图库
CloseBoard();
return 0;
}
运行效果如图:
通过以上两个例子,我们可以看到圆角矩形在很多地方都可以发挥作用。尤其在界面设计当中,可以看到有大量应用,而且绘制方法也比较简单,相信大家能够很容易掌握它的使用。下一篇我们可以继续学习另一个强大的绘制函数--圆弧绘制函数。并可以将前一篇的奥运五环绘制完美了。期待大家继续支持。