C# 学习写控件时一直没效果。。。

zdczdccccc 2014-03-13 04:31:27

生成控件,可是没效果,连Text都看不见,和设计时候默认的那个UserControl一样,请问哪出了问题


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

namespace _自定义按钮
{
public partial class transparentButton : UserControl
{
public transparentButton()
{
InitializeComponent();
}
#region 公共字段

public static SmoothingMode sMode;
public static bool btClick = true;
public static int pub_degree = 20;//四个角弧度的大小范围

#endregion



#region 属性




private int TcornerDegree = 2;
[Browsable(true), Category("按钮的属性设置"), Description("按钮四个角的弧度")]
public int cornerDegree
{
get { return TcornerDegree; }
set {
TcornerDegree = value;
if (this.Width > this.Height)
pub_degree = (int)(this.Height / 2);
else
pub_degree = (int)(this.Width) / 2;
if (TcornerDegree <= 0)
TcornerDegree = 1;
if (TcornerDegree> pub_degree)
TcornerDegree = pub_degree;
if (TcornerDegree > 0)
this.Invalidate();
}
}


private Color TshineColor = Color.Red;
[Browsable(true), Category("按钮的属性设置"), Description("按钮的光泽度颜色")]
public Color shineColor
{
get { return TshineColor; }
set
{
TshineColor = value;
this.Invalidate();
}
}





private int TtranparentDegree = 2;
[Browsable(true), Category("按钮的属性设置"), Description("按钮的透明度数")]
public int transparentDegree
{
get { return TtranparentDegree; }
set{TtranparentDegree=value;
if(TtranparentDegree>20)
TtranparentDegree=20;
if(TtranparentDegree<0)
TtranparentDegree=0;
if(TtranparentDegree>0)
this.Invalidate();}
}


private string TbtText = "Text";
[Browsable(true), Category("按钮的属性设置"), Description("设置显示的文本")]
public string btText
{
get { return TbtText; }
set
{
TbtText = value;
if (TbtText.Length > 0)
this.Invalidate();
}
}


#endregion



#region 事件

private void transparentButton_Paint(object sender, PaintEventArgs e)
{
this.BackColor=Color.Transparent;
sMode=e.Graphics.SmoothingMode;


Rectangle rect=new Rectangle(0,0,this.Width,this.Height);

if(this.transparentDegree==0)
{
backgroundColorClicked(rect, e.Graphics);
backGroundColorUnclicked(rect,e.Graphics,this.shineColor);

}
else
{
if(this.transparentDegree>0)
{
backgroundColorClicked(rect,e.Graphics);
for (int i = 0; i < transparentDegree; i++)
{
backGroundColorUnclicked(rect,e.Graphics,this.shineColor);
}

}

}
if (btClick == false)
backgroundColorClicked(rect,e.Graphics);
if (this.btText.Length > 0)
{
drawText(e.Graphics);
}





}

private void transparentButton_SizeChanged(object sender,EventArgs e)
{
this.Invalidate();
}
private void transparentButton_MouseDown(object sender,EventArgs e)
{
btClick=false;
this.Invalidate();
}
private void transparentButton_MouseUp(object sender,EventArgs e)
{
btClick=true;
this.Invalidate();
}


#endregion

#region 自定义方法


//未按下时按钮的背景色
private void backGroundColorUnclicked(Rectangle rect,Graphics g,Color fillColor)
{
using(GraphicsPath path=createCircleCornerRectangle(rect,this.cornerDegree))
{
int opacity=255;
opacity=(int)(0.4f*opacity+0.5f);
using(LinearGradientBrush brush=new LinearGradientBrush(rect,Color.FromArgb(opacity/5,fillColor),Color.FromArgb(opacity,fillColor),LinearGradientMode.Vertical))
{
g.FillPath(brush,path);
}
g.SmoothingMode=sMode;
}
}

//按钮按下时背景色
private void backgroundColorClicked(Rectangle rect,Graphics g)
{

int opacity=255;
Color tempColor=Color.Green;
if(btClick==true)
{
opacity=125;
tempColor=Color.Blue;
}
using(GraphicsPath path=createCircleCornerRectangle(rect,this.cornerDegree))
{
using(LinearGradientBrush brush=new LinearGradientBrush(rect,Color.FromArgb(opacity/5,tempColor),Color.FromArgb(opacity,tempColor),LinearGradientMode.Vertical))
{
g.FillPath(brush,path);
}
g.SmoothingMode=sMode;
}


}



private void drawText(Graphics g)
{
Graphics txtG = CreateGraphics();
string text = this.btText;
SizeF size = txtG.MeasureString(text,this.Font);
float txtWidth = size.Width;
float txtHeight = size.Height;
float x = 0;
float y = 0;
if (this.Height > txtHeight)
y = (this.Height - txtHeight) / 2;
else
y = this.cornerDegree;
if (this.Width > txtWidth)
x = (this.Width - txtWidth) / 2;
else
x = this.cornerDegree;
Rectangle rect = new Rectangle((int)Math.Floor(x), (int)Math.Floor(y), (int)Math.Ceiling(txtWidth), (int)Math.Ceiling(txtHeight));
int opacity=255;
opacity =(int)(0.4f*opacity+0.5f);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, Color.Black), Color.FromArgb(opacity, Color.Black), LinearGradientMode.Vertical))
{
g.DrawString(text,this.Font,brush,new PointF(x,y));
}



}



//创建圆角矩形,radius是圆角的度数
private static GraphicsPath createCircleCornerRectangle(Rectangle rect,int radius)
{
GraphicsPath path=new GraphicsPath();
int l = rect.Left;
int t = rect.Top;
int w = rect.Width;
int h = rect.Height;
path.AddArc(l, t, 2 * radius, 2 * radius, 0, 0);
path.AddLine(l, t, l + w, t);
path.AddArc(l + w, t, 2 * radius, 2 * radius, 0, 0);
path.AddLine(l + w, t + radius, l + w, t + h - radius);
path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);
path.AddLine(l + radius, t + h, l + w - radius, t + h);
path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);
path.AddLine(l, t + radius, l, t + h - radius);
return path;

}




#endregion


}
}
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdczdccccc 2014-03-13
  • 打赏
  • 举报
回复
引用 9 楼 caozhy 的回复:
InitializeComponent();的代码没有。 另外,你“透明”了当然看不到。 OnRender那个是asp.net的。属于误导回答,你不用关心了。
版主出马,一个顶两,谢谢啦
threenewbee 2014-03-13
  • 打赏
  • 举报
回复
InitializeComponent();的代码没有。 另外,你“透明”了当然看不到。 OnRender那个是asp.net的。属于误导回答,你不用关心了。
zdczdccccc 2014-03-13
  • 打赏
  • 举报
回复
引用 7 楼 laiyongxin 的回复:
[quote=引用 6 楼 zdczdcc 的回复:] [quote=引用 5 楼 laiyongxin 的回复:] transparentButton_Paint 你这个事件方法 没有被调用到吧
应该什么时候调用呢,我以为是需要重绘时自动调用的。。。[/quote] 好像是onRender , 你重写 onRender 方法 然后在里面调用那个事件[/quote] 好像不对呀,没有OnRender方法可以重写,而且我看书上写的控件好像也没提到OnRender
lyx266 2014-03-13
  • 打赏
  • 举报
回复
引用 6 楼 zdczdcc 的回复:
[quote=引用 5 楼 laiyongxin 的回复:] transparentButton_Paint 你这个事件方法 没有被调用到吧
应该什么时候调用呢,我以为是需要重绘时自动调用的。。。[/quote] 好像是onRender , 你重写 onRender 方法 然后在里面调用那个事件
zdczdccccc 2014-03-13
  • 打赏
  • 举报
回复
引用 5 楼 laiyongxin 的回复:
transparentButton_Paint 你这个事件方法 没有被调用到吧
应该什么时候调用呢,我以为是需要重绘时自动调用的。。。
lyx266 2014-03-13
  • 打赏
  • 举报
回复
transparentButton_Paint 你这个事件方法 没有被调用到吧
zdczdccccc 2014-03-13
  • 打赏
  • 举报
回复
引用 2 楼 a01589 的回复:
public partial class transparentButton : UserControl 这为什么要继承UserControl?
我试了下,不继承有些函数如CreatGraphics(),this.invalidate()等。。。没法用啊
zdczdccccc 2014-03-13
  • 打赏
  • 举报
回复
字打错了。。。目的是画四个角是圆角的button,结果连Text都没出来,而且还是直角的
  • 打赏
  • 举报
回复
public partial class transparentButton : UserControl 这为什么要继承UserControl?
zdczdccccc 2014-03-13
  • 打赏
  • 举报
回复
目的是画四个角是圆角的button,结果出来连Text都没出来,而且还是直接的,默认UserControl状,求指点啊哪出问题了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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