65.9K
CodeProject 正在变化。 阅读更多。
Home

一个渐变静态控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.30/5 (19投票s)

2002年9月27日

2分钟阅读

viewsIcon

154332

downloadIcon

4768

静态控件渐变

引言

我一直在浏览这个网站(以及其他一些网站),希望能找到一个简单易用,外观比普通的 CStatic 更好的控件。不幸的是,我没有找到满意的 - 网上有很多很棒的控件,但我需要一些不同的东西。我读过的一篇文章,名为 CTitleMenu,其中包含一个类,可以创建一个带有漂亮的渐变标题的菜单 - 这正是我想要的!我决定将部分代码整合到创建一个漂亮的静态控件中....这就是它。

如何将此控件添加到您的项目中

  1. 将文件 GradientStatic.hGradientStatic.cpp 复制到您的项目目录中,并将这些文件添加到您的项目中。
  2. 在计划使用该类的文件中,包含 #include "GradientStatic.h"
  3. CStatic 定义更改为 CGradientStatic
  4. 使用下面描述的 API。

CGradientStatic API

此控件继承自 CStatic,仅添加了几个新函数。请注意,这个类并不完美。如果您需要其他功能 - 请随意修改它!

默认情况下,CGradientStatic 使用以下颜色

  • 左侧使用活动应用程序的标题颜色 (COLOR_ACTIVECAPTION)
  • 右侧使用按钮面颜色 (COLOR_BTNFACE)
  • 文本使用应用程序标题上的文本颜色绘制。
void SetColor(COLORREF cl);
    // Use this function to change left color of gradient.

    //For example this will set a RED color.
    SetColor(RGB(255,0,0)); 
    //this will use one of system defined colors
    SetColor(GetSysColor(COLOR_ACTIVECAPTION)); 

void SetGradientColor(COLORREF cl);
    //Use this function to changes right color of gradient.

void SetTextColor(COLORREF cl);
    //Use this function to changes color of text.

void SetLeftSpacing(int iNoOfPixels);
    //If text isn't centered you can use this function to set a point where 
    //CGradientStatic should start painting text.
    //On default control leaves 10 pixels from the right side.

void SetTextAlign(int iAlign ) 
    //Use this function to change text aligment.
    //Acceptable values:  0 - left, 1 - center, 2 -right

void void SetVerticalGradient(BOOL a_bVertical = TRUE)
    //Use this function to set vertical gradient

使用示例

    m_pBoldFont = new CFont; //delete it in destructor
    m_pBoldFont->CreateFont(25,0,0,0,900,0,0,0,0,0,0,
        ANTIALIASED_QUALITY,0,"Arial");

    //Use big font and standard colors
    m_cExample.SetFont(m_pBoldFont);
    m_cExample.SetWindowText("This is CGradientStatic example :)");

    //Use standard font & centered text
    m_cExample2.SetWindowText("This text is in the center !");

    m_cExample2.SetColor(RGB(255,0,0));
    m_cExample2.SetGradientColor(RGB(0,0,0));

注释

此控件动态地从 msimg32.dll 加载 GradientFill 函数。如果由于任何原因加载失败,将不会执行渐变填充,但也不会发生崩溃。此控件可免费使用 :)

更新

2004年4月26日

添加了对垂直渐变的支持,以及新的改进的演示应用程序。

2003年5月6日

删除了先前版本的一些限制。现在代码更小、更简单,并且运行得更好一些。

2002年10月6日

Paolo Adami 建议对该控件进行一个小小的改进。感谢函数 DrawGradRect(CDC *pDC, CRect r, COLORREF cLeft, COLORREF cRight) - 控件将在没有安装 msimg32.dll 库的系统上绘制渐变背景。这段代码的算法不如 msimg32.dll 库那么精妙,产生的效果也较差 - 但它能正常工作。

© . All rights reserved.