GDI+实现半透明阴影
2015-09-14 16:44
225 查看
利用 GDI+可以很容易的描画出逼真的半透明效果的阴影。
一、有边框和底部边框实现半透明阴影(转载)。
void DrawShadow(Graphics &g, GraphicsPath ButtonPath)
{
g.SetPageUnit(UnitPixel); //设置Graphics的坐标单位为像素
GraphicsPath &ShadowPath = *(ButtonPath.Clone()); //拷贝一个按钮区域路径的副本,用来生成阴影区域路径
// 获得阴影区域
Matrix ShadowMatrix;
ShadowMatrix.Translate( ShadowSize, ShadowSize );// 平移,ShadowSize即阴影延伸出来的像素数,这里是向右下方移动的,可以根据实际情况修改。
ShadowPath.Transform(&ShadowMatrix); // 应用矩阵
Region ButtonRegion(&ButtonPath); //利用按钮的路径建立按钮区域
Region ShadowRegion(&ShadowPath); //利用阴影路径建立阴影的区域
ShadowRegion.Exclude(&ButtonRegion); // 区域求差,这样就得出了纯粹的阴影区域,排除了阴影区域和按钮区域重合的部分。
// 初始化渐变画刷
PathGradientBrush brush(&ShadowPath);
brush.SetCenterColor(ShadowColor); // 这里利用的是路径渐变画刷
Color colors[] = ...{Color(0, 0, 0, 0)};
int count = 1;
brush.SetSurroundColors(colors, &count);
brush.SetFocusScales(0.75f, 0.75f); //对渐变效果进行调整,使其更加自然。这句的实际作用是对渐变效果进行缩放。参数是横纵两个坐标轴的缩放比例。
g.FillRegion(&brush, &ShadowRegion);
delete &ShadowPath; //别忘了删除Clone出来的副本。
}
二、对话框四边半透明阴影
void DrawShadow(Graphics &g, GraphicsPath ButtonPath)
{
g.SetPageUnit(UnitPixel); //设置Graphics的坐标单位为像素
GraphicsPath &ShadowPath = *(ButtonPath.Clone()); //拷贝一个按钮区域路径的副本,用来生成阴影区域路径
Region ButtonRegion(&ButtonPath); //利用按钮的路径建立按钮区域
RectF rect2{ 50, 50, 110, 110 }, rect3{55,55,100,100};
path.AddRectangle(rect2);
path2.AddRectangle(rect3);
Region ButtonRegion(&path); //利用按钮的路径建立按钮区域
Region region(&path2);
ButtonRegion.Exclude(®ion);//计算抠除矩形中间部分
// 初始化渐变画刷
PathGradientBrush brush(&ShadowPath);
brush.SetCenterColor(ShadowColor); // 这里利用的是路径渐变画刷
Color colors[] = ...{Color(0, 0, 0, 0)};
int count = 1;
brush.SetSurroundColors(colors, &count);
brush.SetFocusScales(0.75f, 0.75f); //对渐变效果进行调整,使其更加自然。这句的实际作用是对渐变效果进行缩放。参数是横纵两个坐标轴的缩放比例。
g.FillRegion(&brush, &ShadowRegion);
delete &ShadowPath; //别忘了删除Clone出来的副本。
}
一、有边框和底部边框实现半透明阴影(转载)。
void DrawShadow(Graphics &g, GraphicsPath ButtonPath)
{
g.SetPageUnit(UnitPixel); //设置Graphics的坐标单位为像素
GraphicsPath &ShadowPath = *(ButtonPath.Clone()); //拷贝一个按钮区域路径的副本,用来生成阴影区域路径
// 获得阴影区域
Matrix ShadowMatrix;
ShadowMatrix.Translate( ShadowSize, ShadowSize );// 平移,ShadowSize即阴影延伸出来的像素数,这里是向右下方移动的,可以根据实际情况修改。
ShadowPath.Transform(&ShadowMatrix); // 应用矩阵
Region ButtonRegion(&ButtonPath); //利用按钮的路径建立按钮区域
Region ShadowRegion(&ShadowPath); //利用阴影路径建立阴影的区域
ShadowRegion.Exclude(&ButtonRegion); // 区域求差,这样就得出了纯粹的阴影区域,排除了阴影区域和按钮区域重合的部分。
// 初始化渐变画刷
PathGradientBrush brush(&ShadowPath);
brush.SetCenterColor(ShadowColor); // 这里利用的是路径渐变画刷
Color colors[] = ...{Color(0, 0, 0, 0)};
int count = 1;
brush.SetSurroundColors(colors, &count);
brush.SetFocusScales(0.75f, 0.75f); //对渐变效果进行调整,使其更加自然。这句的实际作用是对渐变效果进行缩放。参数是横纵两个坐标轴的缩放比例。
g.FillRegion(&brush, &ShadowRegion);
delete &ShadowPath; //别忘了删除Clone出来的副本。
}
二、对话框四边半透明阴影
void DrawShadow(Graphics &g, GraphicsPath ButtonPath)
{
g.SetPageUnit(UnitPixel); //设置Graphics的坐标单位为像素
GraphicsPath &ShadowPath = *(ButtonPath.Clone()); //拷贝一个按钮区域路径的副本,用来生成阴影区域路径
Region ButtonRegion(&ButtonPath); //利用按钮的路径建立按钮区域
RectF rect2{ 50, 50, 110, 110 }, rect3{55,55,100,100};
path.AddRectangle(rect2);
path2.AddRectangle(rect3);
Region ButtonRegion(&path); //利用按钮的路径建立按钮区域
Region region(&path2);
ButtonRegion.Exclude(®ion);//计算抠除矩形中间部分
// 初始化渐变画刷
PathGradientBrush brush(&ShadowPath);
brush.SetCenterColor(ShadowColor); // 这里利用的是路径渐变画刷
Color colors[] = ...{Color(0, 0, 0, 0)};
int count = 1;
brush.SetSurroundColors(colors, &count);
brush.SetFocusScales(0.75f, 0.75f); //对渐变效果进行调整,使其更加自然。这句的实际作用是对渐变效果进行缩放。参数是横纵两个坐标轴的缩放比例。
g.FillRegion(&brush, &ShadowRegion);
delete &ShadowPath; //别忘了删除Clone出来的副本。
}
相关文章推荐
- ./demoCA/newcerts: No such file or directory
- 白盒测试
- Spring IOC介绍
- C++Primer第五版 4.3节练习
- Camera ISP
- *第三周*数据结构实践项目一【顺序表的基本运算】
- 9748:error:02001002:system library:fopen:No such file or directory:.\crypto\bio\bss_file.c:168:fopen
- 第二周项目3-两种排序算法的运行时间
- 第2周项目2 程序的多文件组织
- ios arc下内存管理
- 6个Linux chkconfig命令实例 - 增加,删除,查看和修改services的自动启动选项
- 学习Qt5(6)
- C/C++程序员面试宝典-1
- 二叉树的后序非递归遍历(双栈法)
- iOS webView与APP的交互
- JS设计模式之工厂模式
- eclipse中注释的代码依然会执行的解决方法
- HTML Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用
- js简单运动框架--1 单属性运动
- 第二周——项目2程序的多文件组织