首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

GDI+控件绘图如何实现透明背景

2013-09-23 
GDI+控件绘图怎么实现透明背景本帖最后由 BenBenBears 于 2013-09-16 14:33:44 编辑public void PaintCirc

GDI+控件绘图怎么实现透明背景
本帖最后由 BenBenBears 于 2013-09-16 14:33:44 编辑


       public void PaintCircleHand(Graphics g, Pen pen, int point_x, int radius)
        {

            //Graphics g = this.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            //更改坐标系统的原点,新原点即为圆心
            g.TranslateTransform(this.Width / 2, this.Height / 2);

            //定义指针坐标
            Point PPoint = new Point();
            Point PSPoint = new Point();

            //根据鼠标X坐标计算指针Y坐标
            //三种情况
            if (point_x <= -radius && point_x >= -radius - 20)
            {
                PPoint.X = -radius - (point_x + radius);
                PPoint.Y = Convert.ToInt32(Math.Sqrt((radius * radius) - (PPoint.X * PPoint.X)));
            }

            else if (point_x >= radius && point_x <= radius + 20)
            {
                PPoint.X = radius - (point_x - radius);
                PPoint.Y = Convert.ToInt32(Math.Sqrt((radius * radius) - (PPoint.X * PPoint.X)));

            }


            else
            {
                PPoint.X = point_x;
                PPoint.Y = -Convert.ToInt32(Math.Sqrt((radius * radius) - (PPoint.X * PPoint.X))); ;
            }

            PSPoint.X = PPoint.X * (radius - 10) / radius;
            PSPoint.Y = PPoint.Y * (radius - 10) / radius;

            //清屏
            g.Clear(Color.Gray);

            //绘制指针
            g.DrawLine(pen, PSPoint, PPoint);
        }


上面是原创的模拟仪表指针的代码,我想把它封装成控件,但是受到g.Clear(Color.Gray)指定填充颜色的限制,怎么实现背景透明的效果?或者说怎么制作动画时背景保持不变,而不是填充颜色。不过实现的效果不大理想,希望有更好的模拟指针方案。
类似上面的效果,据我观察,旋钮是固定的,旋转的只是旋转的指针,人家是怎么实现的?
旋钮是个底图呗。然后只要在paint里画蓝色的点点就好了
[解决办法]
g.DrawImage(image1,参数);
g.Rotate(参数)
g.DrawImage(image2,参数);
[解决办法]
 
1, this.SetStyle( ControlStyles.SupportsTransparentBackColor, true);

2.设置控件背景色为Transparent
            
3.OnPaint 时 base.OnPaintBackground(e);
[解决办法]
如果不重绘是不能实现透明的。
以前是在别的语言上做过gdi+的图片处理。
因此,才要使用directx和opengl的存在。

如果你使用wpf,是可以实现这个要求的。

热点排行