在VC中用GDI+绘制角度可变的颜色渐变效果
来源:互联网 发布:上瘾网络剧图片 编辑:程序博客网 时间:2024/06/01 08:31
GDI+ 是GDI(Windows 早期版本提供的图形设备接口)的后续版本,是Microsoft Windows XP作系统即后续版本的图形显示技术。它已经集成到了.Net开发环境中,所以不管你的OS是什么版本,只要安装了.Net框架,就有了GDI+(注意:是.Net框架,而不是.Net开发环境,所以Win98中也可以使用GDI+)。
现在,言归正传。
下面以给一个CTestDlg的对话框绘制背景为例子,用GDI+实现角度可变的颜色渐变效果。用到的变量:
iRotation:整型,渐变色的角度
Color1、Color2、Color3:RGB颜色值
两种颜色的比较简单,直接用GDI+提供的LinearGradientBrush刷子就行了:
三种颜色比较复杂,也是用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;
}
{
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;
}
- 在VC中用GDI+绘制角度可变的颜色渐变效果
- 在VC中用GDI+绘制角度可变的颜色渐变效果
- 在VC中用GDI+绘制角度可变的颜色渐变效果
- GDI+学习之------ 绘制角度可变的颜色渐变效果
- VC 绘制颜色渐变区域
- 在MFC中用GDI+实现图片浏览的放大镜效果
- 在VC中用GDI+缩放图片文件
- 在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- 二:在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- 在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- 在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- 在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- GDI+ 图片颜色渐变
- 在VC++中用CMSChart实现实时曲线的绘制(实例)
- j2me绘制渐变颜色
- iOS绘制颜色渐变
- HTML5绘制颜色渐变
- 绘制颜色渐变三角形
- 添加音乐
- 做网站多大宽度合适?---看各大门户页面宽度
- 鼠标拖动层(可任意绑定DIV标签)(实现方法一)
- 全选DataGrid里的复选框
- 50个令人叹为观止的JavaScript应用站点
- 在VC中用GDI+绘制角度可变的颜色渐变效果
- 麻烦的源代码
- 获取内核ntoskrnl.exe句柄地址的一段vc代码
- windows服务安装时,出错:System.ComponentModel.Win32Exception: 帐户名无效或不存在,或者密码对于指定
- Hello Everyone
- Perl语言学习(1)——入门
- ie浏览器下的js脚本调试工具 Microsoft Script Debugger
- 网站标准
- ie浏览器下用VS2008调试Javascript