钱皇股份 张健健手机:C#下自定义控件的制作 三(全)

来源:百度文库 编辑:九乡新闻网 时间:2024/05/04 19:55:04
C#下自定义控件的制作 三(全) - [C# Programming]
分类:C# Programming | 2009-02-26
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://wynfeeisolate.blogbus.com/logs/35786532.html
还记得上次我们留下的那个悬念么?那么下面就是解答咯...
private void SetClip(Graphics g)
{
Rectangle r = this.ClientRectangle;
r.X++; r.Y++;r.Width -= 3; r.Height -= 3;
//使背景图的绘制范围绘制为图形的左上角XY轴各减小1个像素,将Button的长度和宽度各缩小3个像素用户绘制...数值如此设定的原因很简单,自己体会
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
g.SetClip(rr);//设置操作剪辑...
}
}
绘制一下按钮吧...终于要绘制了,终于要显示出来了!!!
#region 按钮绘制
//在这里我们使用我们定义好的特效函数,这些函数一定要按照一定得顺序进行绘制,否则不能产生预期效果...
private void VistaButton_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
DrawBackground(e.Graphics);
DrawHighlight(e.Graphics);
DrawImage(e.Graphics);
DrawGlow(e.Graphics);
DrawOuterStroke(e.Graphics);
DrawInnerStroke(e.Graphics);
DrawText(e.Graphics);
}
不错...然后添加一些函数吧...嗯,添加一些...
private void VistaButton_Resize(object sender, EventArgs e)
{
Rectangle r = this.ClientRectangle;
r.X -= 1; r.Y -= 1;
r.Width += 2; r.Height += 2;
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
{
this.Region = new Region(rr);
}
//这里我们并没有对文本域进行重新定位,这是本程序的一个小Bug
}
#endregion
#region 鼠标和键盘事件
private void VistaButton_MouseEnter(object sender, EventArgs e)
{
mButtonState = State.Hover;
mFadeOut.Stop();
mFadeIn.Start();
}
private void VistaButton_MouseLeave(object sender, EventArgs e)
{
mButtonState = State.None;
if (this.mButtonStyle == Style.Flat) { mGlowAlpha = 0; }
mFadeIn.Stop();
mFadeOut.Start();
}
private void VistaButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mButtonState = State.Pressed;
if (this.mButtonStyle != Style.Flat) { mGlowAlpha = 255; }
mFadeIn.Stop();
mFadeOut.Stop();
this.Invalidate();
}
}
private void mFadeIn_Tick(object sender, EventArgs e)
{
if (this.ButtonStyle == Style.Flat) { mGlowAlpha = 0; }
if (mGlowAlpha + 30 >= 255)
{
mGlowAlpha = 255;
mFadeIn.Stop();
}
else
{
mGlowAlpha += 30;
}
this.Invalidate();
}
private void mFadeOut_Tick(object sender, EventArgs e)
{
if (this.ButtonStyle == Style.Flat) { mGlowAlpha = 0; }
if (mGlowAlpha - 30 <= 0)
{
mGlowAlpha = 0;
mFadeOut.Stop();
}
else
{
mGlowAlpha -= 30;
}
this.Invalidate();
}
private void VistaButton_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
MouseEventArgs m = new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0);
VistaButton_MouseDown(sender, m);
}
}
private void VistaButton_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
MouseEventArgs m = new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0);
calledbykey = true;
VistaButton_MouseUp(sender, m);
}
}
private void VistaButton_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mButtonState = State.Hover;
mFadeIn.Stop();
mFadeOut.Stop();
this.Invalidate();
if (calledbykey == true) { this.OnClick(EventArgs.Empty); calledbykey = false; }
}
}
还记得这些函数不?是不是我们Designer中声明过的呢?是的...当然是的...
这些代码我都没有写什么注释,原因是代码的含义很简单,大家自己看看就明白了...
然后编译...呼呼,出结果咯....
看看我的效果:
->->->
整个过程是动态的呢...不错吧...图片效果也很好哦,快去试试!!
源文件和dll文件的下载地址:
源文件:http://www.91files.com/?J5TEF2HD7FYRITOZUOH7
dll文件:http://www.91files.com/?6535MWCFYHY5RCIYH6NY
Enjoy~~~!!!!
历史上的今天:
C#下自定义控件的制作 二 2009-02-26
C#下自定义控件的制作 一 2009-02-26
Tag:C#ProgrammingControls
引用地址:
WyNfee 发表于23:19:54 |编辑 |分享 0
评论
能给我发一份源码么?
不胜感激
huzhenqi1@126.com
WyNfee回复胡振奇说:
http://www.91box.net/?j5tef2hd7fyritozuoh7
2010-04-25 17:55:11