请问如何实现此渐变效果

mxmkeep 2010-05-05 02:32:44
下面的程序是我用BCB6做的,切换特效只是BCB里边的一个三方控件,
但是,现在我想自己写代码实现这个效果,请问该用什么相关的算法么?

里边的exe文件是release版,可以直接执行看效果
http://download.csdn.net/source/2317864
...全文
92 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanran_hill 2010-05-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mxmkeep 的回复:]
引用 3 楼 yanran_hill 的回复:

VS2008里有处理渐变的类,不过如果楼主熟悉GDI,可以自己做一个渐变画线的类,这样就可以在PC Windows、Wince等不同的操作系统,以及C++、C#中都可以使用了

难道我又要学GDI+?
可不可以自己写算法?
DirectShow、OpenCV?
[/Quote]
这是我在Mobile上做得画渐变区域的C#类,希望能对你有所帮助

public sealed class 渐变颜色填充
{
public enum FillDirection
{
//
// The fill goes horizontally
//
LeftToRight = GRADIENT_FILL_RECT_H,
//
// The fill goes vertically
//
TopToBottom = GRADIENT_FILL_RECT_V
}

public static bool Fill(
Graphics gr, Rectangle rc, Color[] ColorArray, FillDirection fillDir,
float[] PositionArray)
{
int nSize = PositionArray.Length;
int i;
if (nSize <= 0)
return false;
Rectangle[] rects = new Rectangle[nSize];
for (i = 0; i < nSize - 1; i++)
{
rects[i] = rc;
if (fillDir == FillDirection.LeftToRight)
{
rects[i].X = (int)(PositionArray[i] * rc.Width);
rects[i].Width = (int)((PositionArray[i + 1] - PositionArray[i]) * rc.Width) + 1;
}
else //if (fillDir == FillDirection.TopToBottom)
{
rects[i].Y = (int)(PositionArray[i] * rc.Height);
rects[i].Height = (int)((PositionArray[i + 1] - PositionArray[i]) * rc.Height) + 1;
}
Fill(gr, rects[i], ColorArray[i], ColorArray[i + 1], fillDir);
}
return true;
}
// This method wraps the PInvoke to GradientFill.
// Parmeters:
// gr - The Graphics object we are filling
// rc - The rectangle to fill
// startColor - The starting color for the fill
// endColor - The ending color for the fill
// fillDir - The direction to fill
//
// Returns true if the call to GradientFill succeeded; false
// otherwise.
public static bool Fill(
Graphics gr,
Rectangle rc,
Color startColor, Color endColor,
FillDirection fillDir)
{

// Initialize the data to be used in the call to GradientFill.
渐变颜色填充.TRIVERTEX[] tva = new 渐变颜色填充.TRIVERTEX[2];
tva[0] = new 渐变颜色填充.TRIVERTEX(rc.X, rc.Y, startColor);
tva[1] = new 渐变颜色填充.TRIVERTEX(rc.Right, rc.Bottom, endColor);
渐变颜色填充.GRADIENT_RECT[] gra = new 渐变颜色填充.GRADIENT_RECT[] {
new 渐变颜色填充.GRADIENT_RECT(0, 1)};

// Get the hDC from the Graphics object.
IntPtr hdc = gr.GetHdc();

// PInvoke to GradientFill.
bool b;

b = GradientFill(
hdc,
tva,
(uint)tva.Length,
gra,
(uint)gra.Length,
(uint)fillDir);
//System.Diagnostics.Debug.Assert(b, string.Format(
// "GradientFill failed: {0}",
// System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

// Release the hDC from the Graphics object.
gr.ReleaseHdc(hdc);

return b;
}

public struct TRIVERTEX
{
public int x;
public int y;
public ushort Red;
public ushort Green;
public ushort Blue;
public ushort Alpha;
public TRIVERTEX(int x, int y, Color color)
: this(x, y, color.R, color.G, color.B, color.A)
{
}
public TRIVERTEX(
int x, int y,
ushort red, ushort green, ushort blue,
ushort alpha)
{
this.x = x;
this.y = y;
this.Red = (ushort)(red << 8);
this.Green = (ushort)(green << 8);
this.Blue = (ushort)(blue << 8);
this.Alpha = (ushort)(alpha << 8);
}
}
public struct GRADIENT_RECT
{
public uint UpperLeft;
public uint LowerRight;
public GRADIENT_RECT(uint ul, uint lr)
{
this.UpperLeft = ul;
this.LowerRight = lr;
}
}


[DllImport("coredll.dll", SetLastError = true, EntryPoint = "GradientFill")]
public extern static bool GradientFill(
IntPtr hdc,
TRIVERTEX[] pVertex,
uint dwNumVertex,
GRADIENT_RECT[] pMesh,
uint dwNumMesh,
uint dwMode);

public const int GRADIENT_FILL_RECT_H = 0x00000000;
public const int GRADIENT_FILL_RECT_V = 0x00000001;
}
mxmkeep 2010-05-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xianglitian 的回复:]

给你一个VC的函数做参考吧
GDI的
C/C++ code
void DrawGradientRect(CDC* pDC, CRect rectGradient, COLORREF clrBegin, COLORREF clrEnd)
{
int nRBegin = GetRValue(clrBegin);
int nGBegin = GetGVal……
[/Quote]
非常感谢,我尽量看看
向立天 2010-05-05
  • 打赏
  • 举报
回复
给你一个VC的函数做参考吧
GDI的
void DrawGradientRect(CDC* pDC, CRect rectGradient, COLORREF clrBegin, COLORREF clrEnd)
{
int nRBegin = GetRValue(clrBegin);
int nGBegin = GetGValue(clrBegin);
int nBBegin = GetBValue(clrBegin);

int nREnd = GetRValue(clrEnd);
int nGEnd = GetGValue(clrEnd);
int nBEnd = GetBValue(clrEnd);

int nWidth = rectGradient.Width();
int nHeight = rectGradient.Height();

CRect rectSingle;

for(int i=0 ; i<=nHeight-1; i++)
{
COLORREF clrFillColor;

clrFillColor = RGB(nRBegin+(nREnd-nRBegin)*i/nHeight, nGBegin+(nGEnd-nGBegin)*i/nHeight, nBBegin+(nBEnd-nBBegin)*i/nHeight);
rectSingle.SetRect(rectGradient.left, rectGradient.top+i, rectGradient.right, rectGradient.top+i+1);
pDC->FillSolidRect(&rectSingle, clrFillColor);
}
}
mxmkeep 2010-05-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yanran_hill 的回复:]

VS2008里有处理渐变的类,不过如果楼主熟悉GDI,可以自己做一个渐变画线的类,这样就可以在PC Windows、Wince等不同的操作系统,以及C++、C#中都可以使用了
[/Quote]
难道我又要学GDI+?
可不可以自己写算法?
DirectShow、OpenCV?
yanran_hill 2010-05-05
  • 打赏
  • 举报
回复
VS2008里有处理渐变的类,不过如果楼主熟悉GDI,可以自己做一个渐变画线的类,这样就可以在PC Windows、Wince等不同的操作系统,以及C++、C#中都可以使用了
向立天 2010-05-05
  • 打赏
  • 举报
回复
BCB没有过
倒是经常VC做渐变
cattycat 2010-05-05
  • 打赏
  • 举报
回复
自己写,你看BCB6里有源码吗,可以参考,没有的话,一般就是图像处理的算法了,VC图像渐变效果,可以搜一下方法。

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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