读书人

相关控件透明度的控制

发布时间: 2012-12-14 10:33:08 作者: rapoo

有关控件透明度的控制!

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace WindowsFormsApplication2
{
public class ExButton : Button
{

public ExButton()
{
this.Cursor = Cursors.Hand;
}

private bool mouseover = false;
private Color _topColor = Color.FromArgb(166, 222, 255);
private Color _bottomColor = Color.FromArgb(234, 247, 254);
[DefaultValue(typeof(Color), "166, 222, 255")]
public Color TopColor
{
get
{
return _topColor;
}
set
{
_topColor = value;
base.Invalidate(true);
}
}
[DefaultValue(typeof(Color), "234, 247, 254")]
public Color BottomColor
{
get
{
return _bottomColor;
}
set
{
_bottomColor = value;
base.Invalidate(true);
}
}


[TypeConverter(typeof(OpacityConverter))]
public double Opacity
{
get;
set;
}


protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{

Color c5 = _topColor;//按钮表面上部颜色

Color c2 = _bottomColor;//按钮表面下部颜色

if (mouseover)//鼠标移入的话重新改变
{

c5 = Color.Gainsboro;

c2 = Color.Gray;

}



Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c5, c2, LinearGradientMode.Vertical);

int offsetwidth = this.Width / 50;

Point[] points = new Point[8];

points[0].X = offsetwidth;

points[0].Y = 0;

points[1].X = this.Width - offsetwidth;

points[1].Y = 0;

points[2].X = this.Width;

points[2].Y = offsetwidth;

points[3].X = this.Width;

points[3].Y = this.Height - offsetwidth;

points[4].X = this.Width - offsetwidth;

points[4].Y = this.Height;

points[5].X = offsetwidth;

points[5].Y = this.Height;

points[6].X = 0;

points[6].Y = this.Height - offsetwidth;

points[7].X = 0;

points[7].Y = offsetwidth;

e.Graphics.FillPolygon(b, points, FillMode.Winding);



int offsetwidth1 = (this.Width - 5) / 50 + 2;

Point[] points1 = new Point[8];

points1[0].X = offsetwidth1;

points1[0].Y = 2;

points1[1].X = this.Width - offsetwidth1;

points1[1].Y = 2;

points1[2].X = this.Width - 1;

points1[2].Y = offsetwidth1;

points1[3].X = this.Width - 1;

points1[3].Y = this.Height - offsetwidth1;

points1[4].X = this.Width - offsetwidth1;

points1[4].Y = this.Height - 1;



points1[5].X = 1;

points1[5].Y = this.Height - 1;

points1[6].X = 2;

points1[6].Y = this.Height - offsetwidth1;

points1[7].X = 2;

points1[7].Y = offsetwidth1;

Pen p = new Pen(Color.Orange, 2);

Pen p1 = new Pen(Color.Wheat, 2);

e.Graphics.DrawLine(p1, points1[0], points1[1]);



e.Graphics.DrawLine(p, points1[1], points1[2]);

e.Graphics.DrawLine(p, points1[2], points1[3]);

e.Graphics.DrawLine(p, points1[3], points1[4]);

e.Graphics.DrawLine(p, points1[4], points1[5]);

e.Graphics.DrawLine(p, points1[5], points1[6]);

e.Graphics.DrawLine(p1, points1[6], points1[7]);

e.Graphics.DrawLine(p1, points1[7], points1[0]);

e.Graphics.DrawPolygon(new Pen(Color.DarkBlue, 2), points);



StringFormat drawFormat = new StringFormat();

drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;

drawFormat.LineAlignment = StringAlignment.Center;

drawFormat.Alignment = System.Drawing.StringAlignment.Center;



e.Graphics.DrawString(this.Text, this.Font, new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.Black, LinearGradientMode.Vertical), this.ClientRectangle, drawFormat);

b.Dispose();

}

protected override void OnMouseEnter(EventArgs e)
{
mouseover = true;
this.Invalidate(false);
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{

mouseover = false;

this.Invalidate(false);

base.OnMouseLeave(e);

}


}
}


我把别人写的控件重新定义了一个Opacity的属性,添加在界面的时候,还是不能让他有效的成为透明状态,请问有什么其他的方法定义这个BUTTON是透明的吗??有人说Form里面的添加的控件必须要Form透明,里面才能成透明状,可是我只想单独控件透明,谁有好的方法!!
[解决办法]
坐等高手回答这个问题!对于高手,这个问题应该不难吧。。。。。
[解决办法]
几天了,既然没一个人回答这个问题!
[解决办法]
好吧,我开始失去信心了,再等几天看看!!
[解决办法]
给你一点给人意见:给按钮做一张背景图片。
一个馊主意,希望馊的有道理。
[解决办法]
背景图片不能达到透明效果。。所以我也试过!

读书人网 >.NET

热点推荐