如何用渐变色填充矩形?谢谢!

vcbeginner 2000-10-06 09:11:00
我想用渐变色,比如从红-〉黑填充一窄矩形,使它看起来很有立体感,请问该如何计算RGB,是否有一公式?
...全文
1065 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hboy 2000-10-09
  • 打赏
  • 举报
回复
为了方便,先找现有的,就找到了,现在你用到了程序里了吗,应该可用,我都是这样用的,你觉得好用,就多多给分!说不定下次还可帮你
vcbeginner 2000-10-08
  • 打赏
  • 举报
回复
GradientFill在win2000和win98下可用,NT4.0和win98以下版本不能用,请问hboy,你是从和知道这些新API的。
vcbeginner 2000-10-08
  • 打赏
  • 举报
回复
hboy:
CSDN出问题了,给你的25分好像被CSDN吃了。
vcbeginner 2000-10-08
  • 打赏
  • 举报
回复
hboy:
我先去查一下GradientFill的用法,肯定要给分
hboy 2000-10-08
  • 打赏
  • 举报
回复
不好意思,操作失误
hboy 2000-10-08
  • 打赏
  • 举报
回复
用这个API函数
BOOL GradientFill(
HDC hdc,
CONST PTRIVERTEX pVertex,
DWORD dwNumVertex,
CONST PVOID pMesh,
DWORD dwNumMesh,
DWORD dwMode
);
TRIVERTEX vert[2] ;
GRADIENT_RECT gRect;
vert [0] .x = 0;
vert [0] .y = 0;
vert [0] .Red = 0xff00;
vert [0] .Green = 0x0000;
vert [0] .Blue = 0x0000;
vert [0] .Alpha = 0x0000;

vert [1] .x = 100;
vert [1] .y = 32;
vert [1] .Red = 0x0000;
vert [1] .Green = 0x0000;
vert [1] .Blue = 0x0000;
vert [1] .Alpha = 0x0000;

gRect.UpperLeft = 0;
gRect.LowerRight = 1;
GradientFill(hdc,vert,2,&gRect,1,GRADIENT_FILL_RECT_H);
速度快,不用你去计算,记住在VC的Project Setting的Link项加上msimg32.lib
还可用于三角形,通过三角形的组合,还可用更多的图形,具体用法看MSDN
记住给我分呀!
tbmac 2000-10-08
  • 打赏
  • 举报
回复
不如用OGL,不用耽误工夫
cqjiang 2000-10-08
  • 打赏
  • 举报
回复
伪码:
COLORREF colorOrg(R,G,B);//原始颜色
int n;//渐变颗粒度

//渐亮
for(i=0;i<n;i++)
{
colorNew.R=colorOrg.R + i*(255-colorOrg.R)/n;
colorNew.G=colorOrg.G + i*(255-colorOrg.G)/n;
colorNew.B=colorOrg.B + i*(255-colorOrg.B)/n;
}

//渐黑
for(i=0;i<n;i++)
{
colorNew.R=colorOrg.R - i*colorOrg.R / n;
colorNew.G=colorOrg.G - i*colorOrg.G / n;
colorNew.B=colorOrg.B - i*colorOrg.B / n;
}


bhhjf 2000-10-07
  • 打赏
  • 举报
回复
1.先使用CPalette建立一个新调色板
代码参考如下
if (m_Pal.GetSafeHandle() != NULL)
return FALSE;

BOOL bSucc = FALSE;

const int nNumColors = 236;

LPLOGPALETTE lpPal = (LPLOGPALETTE)new BYTE[sizeof(LOGPALETTE) +
sizeof(PALETTEENTRY) *
nNumColors];

if (lpPal != NULL)
{
lpPal->palVersion = 0x300;
lpPal->palNumEntries = nNumColors;

PALETTEENTRY *ppe = lpPal->palPalEntry;

for (int nIndex = 0; nIndex < nNumColors; nIndex++)
{
const int nColor = ::MulDiv(nIndex, 255, nNumColors);

ppe->peRed = (BYTE)(m_nPaintRGB & GPC_RED ? nColor : 0);
ppe->peGreen = (BYTE)(m_nPaintRGB & GPC_GREEN ? nColor : 0);
ppe->peBlue = (BYTE)(m_nPaintRGB & GPC_BLUE ? nColor : 0);
ppe->peFlags = (BYTE)0;

ppe++;
}

bSucc = m_Pal.CreatePalette(lpPal);

delete [](PBYTE)lpPal;
}
2.在窗口的On_Paint()成员函数中加入下面代码
CPalette *pPalOld = dc.SelectPalette(&m_Pal, FALSE);
dc.RealizePalette();
RECT rect;
this->GetClientRect(&rect);
this->PaintGradiantRect(&dc, rect);
dc.SelectPalette(pPalOld, FALSE);
3.其中的PaintGradiantRect函数如下:
ASSERT(pDC != NULL);
ASSERT_KINDOF(CDC, pDC);

// initialize
RECT rectVar = { rect.left, rect.top, rect.left, rect.top };
int nTotalSize;
if (m_nPaintDir == GPD_TTOB || m_nPaintDir == GPD_BTOT)
{
rectVar.right = rect.right;
nTotalSize = rect.bottom - rect.top;
}
else
{
rectVar.bottom = rect.bottom;
nTotalSize = rect.right - rect.left;
}

// paint nSteps times
for (int nIndex = 0; nIndex < m_nPaintSteps; nIndex++)
{
// calculate the rectangle
if (m_nPaintDir == GPD_TTOB || m_nPaintDir == GPD_BTOT)
{
rectVar.top = rectVar.bottom;
rectVar.bottom = rect.top +
::MulDiv(nIndex + 1, nTotalSize, m_nPaintSteps);
}
else
{
rectVar.left = rectVar.right;
rectVar.right = rect.left +
::MulDiv(nIndex + 1, nTotalSize, m_nPaintSteps);
}

// calculate the color value
int nColor = ::MulDiv(nIndex, 255, m_nPaintSteps);
if (m_nPaintDir == GPD_BTOT || m_nPaintDir == GPD_RTOL)
nColor = 255 - nColor;
const COLORREF clrBr =
PALETTERGB((BYTE)(m_nPaintRGB & GPC_RED ? nColor : 0),
(BYTE)(m_nPaintRGB & GPC_GREEN ? nColor : 0),
(BYTE)(m_nPaintRGB & GPC_BLUE ? nColor : 0));

// paint the rectangle with the brush
CBrush brush(clrBr);
pDC->FillRect(&rectVar, &brush);
}
4.使用调色板还要重载窗口的两个成员函数OnPaletteChanged(CWnd *pFocusWnd);
OnQueryNewPalette()
具体如何重载一般vc书上都有。
NiceFeather 2000-10-06
  • 打赏
  • 举报
回复
这里有两个vb的例子,但我想你看懂了他们这个就没问题了!
请看此页下端的“程序员大本营”的“Visual Basic”的“VB源码集合”的“用户界面”的“在 Windows 95 中,实现渐变的标题栏的例子”和“实现渐变效果的背景”例程

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧