C语言版GDI+应用例子 -- 画刷

  GDI+提供了SolidBrush(实色刷)、HatchBrush(阴影刷)、TextureBrush(纹理刷)、LinearGradientBrush(渐变刷)和PathGradientBrush(路径刷)等五种画刷,在GDI+的C语言版本中,这些画刷的方法都以在原C++类类名为前缀,其中的LinearGradientBrush和PathGradientBrush分别简化为LineBrush和PathBrush。

    GDI+的画刷是GDI+绘制图形的基础,GDI+画笔也是依靠GDI+画刷形成的,熟练地运用GDI+画刷,可以使各种图像更加多姿多彩,也可以用它们来绘制Windows应用程序的各种控件的界面,MS的Office2003及以后的版本的Office程序界面都是使用GDI+绘制界面的典型例子。

    下面是一个用C语言改写的GDI+画刷的经典例子代码:

view plaincopy to clipboardprint?
// main.c  
 
#include "../../SampleCode/comcode/Application.h"  
 
#pragma hdrstop  
 
PGpBrush backgroundBrush;  
 
void OnCreate(void)  
{  
    // 建立一个全局图像画刷,用来填充窗口背景  
    PGpImage backgroundImage = ImageCreate(L"..//..//Media//colorbars.jpg");  
    backgroundBrush = TextureBrushCreate(backgroundImage);  
    ImageDelete(backgroundImage);  
}  
 
void OnDestroy(void)  
{  
    BrushDelete(backgroundBrush);  
}  
 
void OnPaint(HDC DC)  
{  
    Point points[] = {{40, 140}, {275, 200}, {105, 225},  
                      {190, 300}, {50, 350}, {20, 180}};  
    BYTE types[] = {PathPointTypeStart, PathPointTypeBezier,  
                    PathPointTypeBezier, PathPointTypeBezier,  
                    PathPointTypeLine, PathPointTypeLine};  
    Rect r = {300, 250, 100, 100};  
    ARGB ac[] = {Green, Yellow, Red, Blue, Orange, White};  
    RECT clientRect;  
    PGpHatchBrush hb;  
    PGpLineBrush lb;  
    PGpPathBrush pgb;  
    PGpPath path;  
    PGpSolidBrush sb = SolidBrushCreate(SetAlpha(White, 180));  
    PGpGraphics g = GraphicsCreate(DC);             // GDI+ 画布  
    GraphicsSetSmoothingMode(g, SmoothingModeAntiAlias);  
 
    // 用图像画刷填充背景,同时用白色半透明实色画刷遮照背景图案  
    GetClientRect(Handle, &clientRect);  
    GraphicsFillRectangle(g, backgroundBrush, 0, 0, clientRect.right, clientRect.bottom);  
    GraphicsFillRectangle(g, sb, 0, 0, clientRect.right, clientRect.bottom);  
 
    // 用红色实色画刷画矩形,同时用半透明黄色实色刷错开画同样大小矩形在其上  
    SolidBrushSetColor(sb, Red);  
    GraphicsFillRectangle(g, sb, 20, 20, 50, 50);  
    SolidBrushSetColor(sb, SetAlpha(Yellow, 180));  
    GraphicsFillRectangle(g, sb, 40, 40, 50, 50);  
 
    // 用图案画刷画一个圆,图案前景色为绿色,背景色则为大半透明黄色  
    hb = HatchBrushCreate(HatchStyleForwardDiagonal, Green, SetAlpha(Yellow, 100));  
    GraphicsFillEllipse(g, hb, 250, 10, 100, 100);  
    BrushDelete(hb);  
 
    // 用渐变色画刷从右上至左下画一红、黄色渐变矩形  
    lb = LineBrushFromRect(&r, Red, Yellow, LinearGradientModeBackwardDiagonal);  
    GraphicsFillRectangle(g, lb, r.X, r.Y, r.Width, r.Height);  
    BrushDelete(lb);  
 
    // 用6种颜色路径画刷匹配6坐标点路径画一图形(注意这不是多色填充,  
    // 而是匹配路径坐标点渐变填充,颜色可以少于但不能大于路径坐标点;  
    // 多色路径刷填充是沿路径中心点向边缘按规定的比例填充)。应该很漂亮!  
    // 但遗憾的是,我还没见过有谁画成功过这个图形,只能画单色,也很漂亮的。  
    path = PathFromData(points, types, 6, FillModeAlternate);  
    pgb = PathBrushCreate(path);  
    PathBrushSetSurroundColors(pgb, &ac, 1);  
    GraphicsFillPath(g, pgb, path);  
    BrushDelete(pgb);  
    PathDelete(path);  
 
    // 用紫色实色画刷画一个逆时针旋转30度的矩形  
    SolidBrushSetColor(sb, SlateBlue);  
    GraphicsRotate(g, -30, 0);  
    GraphicsFillRectangle(g, sb, 100, 250, 75, 100);  
    GraphicsResetTransform(g);  
 
    BrushDelete(sb);  
    GraphicsDelete(g);  
}  
 
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)  
{  
    // 这里使用了双缓冲画窗口,可将第3个参数改为FALSE比较一下效果 */  
    InitApplication(hInstance, nCmdShow, TRUE);  
    CreateProc = OnCreate;  
    DestroyProc = OnDestroy;  
    PaintProc = OnPaint;  
    return RunApplication(TEXT("C语言Gdiplus演示例子 -- 画刷"), 450, 400);  
}  
//--------------------------------------------------------------------------- 
// main.c

#include "../../SampleCode/comcode/Application.h"

#pragma hdrstop

PGpBrush backgroundBrush;

void OnCreate(void)
{
 // 建立一个全局图像画刷,用来填充窗口背景
 PGpImage backgroundImage = ImageCreate(L"..//..//Media//colorbars.jpg");
 backgroundBrush = TextureBrushCreate(backgroundImage);
 ImageDelete(backgroundImage);
}

void OnDestroy(void)
{
 BrushDelete(backgroundBrush);
}

void OnPaint(HDC DC)
{
 Point points[] = {{40, 140}, {275, 200}, {105, 225},
       {190, 300}, {50, 350}, {20, 180}};
 BYTE types[] = {PathPointTypeStart, PathPointTypeBezier,
     PathPointTypeBezier, PathPointTypeBezier,
     PathPointTypeLine, PathPointTypeLine};
 Rect r = {300, 250, 100, 100};
 ARGB ac[] = {Green, Yellow, Red, Blue, Orange, White};
 RECT clientRect;
 PGpHatchBrush hb;
 PGpLineBrush lb;
 PGpPathBrush pgb;
 PGpPath path;
 PGpSolidBrush sb = SolidBrushCreate(SetAlpha(White, 180));
 PGpGraphics g = GraphicsCreate(DC);            // GDI+ 画布
 GraphicsSetSmoothingMode(g, SmoothingModeAntiAlias);

 // 用图像画刷填充背景,同时用白色半透明实色画刷遮照背景图案
 GetClientRect(Handle, &clientRect);
 GraphicsFillRectangle(g, backgroundBrush, 0, 0, clientRect.right, clientRect.bottom);
 GraphicsFillRectangle(g, sb, 0, 0, clientRect.right, clientRect.bottom);

 // 用红色实色画刷画矩形,同时用半透明黄色实色刷错开画同样大小矩形在其上
 SolidBrushSetColor(sb, Red);
 GraphicsFillRectangle(g, sb, 20, 20, 50, 50);
 SolidBrushSetColor(sb, SetAlpha(Yellow, 180));
 GraphicsFillRectangle(g, sb, 40, 40, 50, 50);

 // 用图案画刷画一个圆,图案前景色为绿色,背景色则为大半透明黄色
 hb = HatchBrushCreate(HatchStyleForwardDiagonal, Green, SetAlpha(Yellow, 100));
 GraphicsFillEllipse(g, hb, 250, 10, 100, 100);
 BrushDelete(hb);

 // 用渐变色画刷从右上至左下画一红、黄色渐变矩形
 lb = LineBrushFromRect(&r, Red, Yellow, LinearGradientModeBackwardDiagonal);
 GraphicsFillRectangle(g, lb, r.X, r.Y, r.Width, r.Height);
 BrushDelete(lb);

 // 用6种颜色路径画刷匹配6坐标点路径画一图形(注意这不是多色填充,
 // 而是匹配路径坐标点渐变填充,颜色可以少于但不能大于路径坐标点;
 // 多色路径刷填充是沿路径中心点向边缘按规定的比例填充)。应该很漂亮!
 // 但遗憾的是,我还没见过有谁画成功过这个图形,只能画单色,也很漂亮的。
 path = PathFromData(points, types, 6, FillModeAlternate);
 pgb = PathBrushCreate(path);
 PathBrushSetSurroundColors(pgb, &ac, 1);
 GraphicsFillPath(g, pgb, path);
 BrushDelete(pgb);
 PathDelete(path);

 // 用紫色实色画刷画一个逆时针旋转30度的矩形
 SolidBrushSetColor(sb, SlateBlue);
 GraphicsRotate(g, -30, 0);
 GraphicsFillRectangle(g, sb, 100, 250, 75, 100);
 GraphicsResetTransform(g);

 BrushDelete(sb);
 GraphicsDelete(g);
}

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 // 这里使用了双缓冲画窗口,可将第3个参数改为FALSE比较一下效果 */
 InitApplication(hInstance, nCmdShow, TRUE);
 CreateProc = OnCreate;
 DestroyProc = OnDestroy;
 PaintProc = OnPaint;
 return RunApplication(TEXT("C语言Gdiplus演示例子 -- 画刷"), 450, 400);
}
//---------------------------------------------------------------------------
 

    例子代码使用的窗口框架代码和GDI+ C语言版本下载地址见《在C语言Windows应用程序中使用GDI+》。

    由于例子代码中作了详细的注释,而且这也是个GDI+经典例子,所以就不在此啰嗦了。

    顺便说一下,原GDI+ C++版本的Color类型在C版本中取消,需要用到颜色的地方直接使用ARGB类型,原Color类的一些方法还是保留了。

    下面是用VC2005编译运行例子的界面截图:

 

    GDI+初学者要想熟练的掌握GDI+画刷,仅靠这个例子是远远不够的,必须多看看GDI+资料,多参考C++、C#等语言中运用GDI+的代码。关于GDI+画刷,我有几篇文章作了专门介绍,里面有更多使用画刷的例子,虽然是Delphi或者BCB写的,但原理是一样的,可供初学者参考:

《GDI+ for VCL基础 -- 画刷之SolidBrush》,《GDI+ for VCL基础 -- 画刷之HatchBrush》,《GDI+ for VCL基础 -- 画刷之TextureBrush》,《GDI+ for VCL基础 -- 画刷之LinearGradientBrush》,《GDI+ for VCL基础 -- 画刷之PathGradientBrush》。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值