• 在VC中用GDI+绘制角度可变的颜色渐变效果.NET教程,VB.Net语言[转]


    在VC中用GDI+绘制角度可变的颜色渐变效果-.NET教程,VB.Net语言

    作者:网友供稿 点击:4

        gdi+ 是gdi(windows 早期版本提供的图形设备接口)的后续版本,是microsoft windows xp作系统即后续版本的图形显示技术。它已经集成到了.net开发环境中,所以不管你的os是什么版本,只要安装了.net框架,就有了gdi+(注意:是.net框架,而不是.net开发环境,所以win98中也可以使用gdi+)。

    现在,言归正传。

    在头文件中加入下面的代码:

    #include <gdiplus.h>
    using namespace gdiplus;
    #pragma comment(lib,"gdiplus.lib")

    注意:在使用gdi+函数时必须进行gdi+的初始化,使用完毕要销毁gdi+!
    初始化:

    gdiplusstartupinput gdiplusstartupinput;
    ulong_ptr gdiplustoken;
    gdiplusstartup(&gdiplustoken, &gdiplusstartupinput, null);

    销毁:

    ulong_ptr gdiplustoken = null;
    gdiplusshutdown(gdiplustoken);

    下面以给一个ctestdlg的对话框绘制背景为例子,用gdi+实现角度可变的颜色渐变效果。用到的变量:
    irotation:整型,渐变色的角度
    color1、color2、color3:rgb颜色值

    两种颜色的比较简单,直接用gdi+提供的lineargradientbrush刷子就行了:

    bool ctestdlg::onerasebkgnd(cdc* pdc)
    {
    cdialog::onerasebkgnd(pdc);

    // 取得第一种颜色的r,g,b值
    int r1 = getrvalue(color1);
    int g1 = getgvalue(color1);
    int b1 = getbvalue(color1);

    // 取得第二种颜色的r,g,b值
    int r2 = getrvalue(color2);
    int g2 = getgvalue(color2);
    int b2 = getbvalue(color2);

    // 得到绘制区域
    crect rect;
    getclientrect(&rect);

    // gdi+对象
    gdiplus::graphics graphics(pdc->getsafehdc());
    // 刷子
    gdiplus::lineargradientbrush lingrbrush(gdiplus::rect(0, 0, rect.width(), rect.height()), // 绘制区域
    gdiplus::color(255, r1, g1, b1), // 第一种颜色
    gdiplus::color(255, r2, g2, b2), // 第二种颜色
    (gdiplus::real)(90 - irotation)); // 渐变色的角度

    graphics.fillrectangle(&lingrbrush, gdiplus::rect(0, 0, rect.width(), rect.height()));
    return true;
    }

    三种颜色比较复杂,也是用gdi+提供的lineargradientbrush刷子,不过需要计算绘制区域的对角线长度,并按照对角线平分为三等分。

    具体的看以下代码:

    bool ctestdlg::onerasebkgnd(cdc* pdc)
    {
    cdialog::onerasebkgnd(pdc);

    // 取得第一种颜色的r,g,b值
    int r1 = getrvalue(color1);
    int g1 = getgvalue(color1);
    int b1 = getbvalue(color1);

    // 取得第二种颜色的r,g,b值
    int r2 = getrvalue(color2);
    int g2 = getgvalue(color2);
    int b2 = getbvalue(color2);

    // 取得第三种颜色的r,g,b值
    int r3 = getrvalue(color3);
    int g3 = getgvalue(color3);
    int b3 = getbvalue(color3);

    // 得到绘制区域
    crect rect;
    getclientrect(&rect);

    // 计算对角线长度
    int iheight = rect.height();
    int iwidth = rect.width();
    double dwdiagonal = sqrt((double)(iwidth * iwidth + iheight * iheight));

    // 三块绘制区域
    rect rectdraw(0, 0, (int)dwdiagonal, (int)dwdiagonal);
    rect rectdraw1(0, 0, (int)dwdiagonal, ((int)dwdiagonal)/2);
    rect rectdraw2(0, ((int)dwdiagonal) / 2, (int)dwdiagonal, ((int)dwdiagonal) / 2);

    // gdi+对象
    graphics graphics(pdc->getsafehdc());
    gdiplus::bitmap bmp(rectdraw.width, rectdraw.height);
    graphics grtmp(&bmp);

    // 用刷子填充区域
    gdiplus::lineargradientbrush lingrbrush(rectdraw1, color(r1, g1, b1), color(r2, g2, b2), 90);
    grtmp.fillrectangle(&lingrbrush, rectdraw1);
    gdiplus::lineargradientbrush lingrbrush1(rectdraw2, color(r2, g2, b2),color(r3, g3, b3), 90);
    grtmp.fillrectangle(&lingrbrush1, rectdraw2);

    // 计算
    dwdiagonal *= 0.5;
    double dwangle = irotation * 3.1415926 / 180.0;
    double dwcosangle = cos(dwangle);
    double dwsinangle = sin(dwangle);
    double dwbeta = atan2((double)iheight, (double)iwidth);
    double dwdistance = dwdiagonal * sin(fabs(dwangle) + dwbeta);
    double xc = 0.5 * iwidth - dwdistance * dwsinangle;
    double yc = 0.5 * iheight - dwdistance * dwcosangle;
    double xc1 = 0.5 * iwidth + dwdistance * dwsinangle;
    double yc1 = 0.5 * iheight + dwdistance * dwcosangle;
    double dx = dwdiagonal * dwcosangle;
    double dy = - dwdiagonal * dwsinangle;

    // 绘制
    point ptdestinationpoints[3];
    ptdestinationpoints[0].x = (int)(xc - dx);
    ptdestinationpoints[0].y = (int)(yc - dy);
    ptdestinationpoints[1].x = (int)(xc + dx);
    ptdestinationpoints[1].y = (int)(yc + dy);
    ptdestinationpoints[2].x = (int)(xc1 - dx);
    ptdestinationpoints[2].y = (int)(yc1 - dy);
    graphics.drawimage(&bmp, ptdestinationpoints, 3);

    return true;
    }

    http://www.west263.com/www/info/40780-1.htm

  • 相关阅读:
    BZOJ1841 : 蚂蚁搬家
    BZOJ3068 : 小白树
    BZOJ4449 : [Neerc2015]Distance on Triangulation
    BZOJ3692 : 愚蠢的算法
    BZOJ3145 : [Feyat cup 1.5]Str
    BZOJ4684 : Company Organization
    BZOJ2934 : [Poi1999]祭坛问题
    ML(2)——感知器
    ML(附录1)——梯度下降
    微服务架构
  • 原文地址:https://www.cnblogs.com/saptechnique/p/2331872.html
Copyright © 2020-2023  润新知