Vincent's profile异国情调 世界视野 One World On...PhotosBlogListsMore Tools Help

Blog


    November 13

    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类的一些方法还是保留了。

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://vincentibm2007.spaces.live.com/blog/cns!1656C6D42D1E61!1606.trak
    Weblogs that reference this entry
    • None