Program generation of crystal buttons in C #.

Source: Internet
Author: User
Tags bool drawtext bf4
Button | The program makes a control so you can say goodbye to the picture button:

Effect:

Program Source code:



Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;
Using System.Drawing.Drawing2D;
Using System.Drawing.Text;

Namespace WindowsControlLibrary1
{
<summary>
Summary description of the UserControl1.
</summary>
public class Usercontrol1:button
{
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
private bool Xiacen=false;
private bool Mousemove=false;
Private Color BackColor;
Public UserControl1 ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the initcomponent call
Backcolor=this.backcolor;
This. Text=this. Showfocuscues.tostring ();
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
Components. Dispose ();
}
Base. Dispose (disposing);
}

Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
//
UserControl1
//
This. MouseUp + = new System.Windows.Forms.MouseEventHandler (this. Usercontrol1_mouseup);
This. Paint + = new System.Windows.Forms.PaintEventHandler (this. Usercontrol1_paint);
This. MouseEnter + = new System.EventHandler (this. Usercontrol1_mouseenter);
This. KeyUp + = new System.Windows.Forms.KeyEventHandler (this. Usercontrol1_keyup);
This. KeyDown + = new System.Windows.Forms.KeyEventHandler (this. Usercontrol1_keydown);
This. Backcolorchanged + = new System.EventHandler (this. usercontrol1_backcolorchanged);
This. MouseLeave + = new System.EventHandler (this. Usercontrol1_mouseleave);
This. MouseDown + = new System.Windows.Forms.MouseEventHandler (this. Usercontrol1_mousedown);

}
#endregion



Protected GraphicsPath Getgraphicspath (Rectangle rect)
{
GraphicsPath Clientpath = new System.Drawing.Drawing2D.GraphicsPath ();
if (rect. width<=0)
{
Rect. width=1;
}
if (rect. height<=0)
{
Rect. Height=1;
}

Clientpath.addarc (rect. Left,rect. Top,rect. Height,rect. HEIGHT,90F,180F);
Clientpath.addarc (rect. Right-rect.height,rect. Top,rect. Height,rect. HEIGHT,270F,180F);
Clientpath.closefigure ();
return clientpath;
}

Protected GraphicsPath GetGraphicsPath1 (Rectangle rect)
{
GraphicsPath Clientpath = new System.Drawing.Drawing2D.GraphicsPath ();
if (rect. width<=0)
{
Rect. width=1;
}
if (rect. height<=0)
{
Rect. Height=1;
}

Clientpath.addarc (rect. Left,rect. Top,rect. Height,rect. HEIGHT,190F,80F);
Clientpath.addarc (rect. Right-rect.height,rect. Top,rect. Height,rect. HEIGHT,270F,80F);
Clientpath.closefigure ();
return clientpath;
}

private void Drawyinying (Graphics gr,bool Xiacen)
{
Rectangle rect= this. ClientRectangle;
Rect. Inflate (-(rect). WIDTH/10),-(rect. Height)/4);
Float Bf1=rect. width/100f;
Float Bf2=rect. height/100f;

Rect. Y=rect. Y+this. CLIENTRECTANGLE.HEIGHT/4;
if (Xiacen)
{
Rect. Y=rect. y+4;
}
GraphicsPath path;

for (int a=1;a<33;a++)
{
float bf3=bf1*a;
float bf4=bf2*a;
Rectangle Rect1=rect;
Rect1. Inflate (-(int) bf3,-(int) bf4);
Path=getgraphicspath (RECT1);

int R=BACKCOLOR.R;
int g=backcolor.g;
int b=backcolor.b;
R=r+3*a;
G=g+3*a;
B=b+3*a;
if (r>255) r=255;
if (g>255) g=255;
if (b>255) b=255;
Gr. FillPath (New SolidBrush (Color.FromArgb (r,g,b)), path);
}
}

private void Drawgaoliang (Graphics g,bool Xiacen)
{
Rectangle rect= this. ClientRectangle;
Rect. Inflate ( -4,-4);

if (Xiacen)
{
Rect. Y=rect. y+4;
}
GraphicsPath path=getgraphicspath1 (rect);
RectangleF Rect1=path. GetBounds ();
Rect1. Height=rect1. height+1;
G.fillpath (New LinearGradientBrush (Rect1,
Color.FromArgb (0xff,0xff,0xff,0xff),
Color.FromArgb (0xff,backcolor), lineargradientmode.vertical), path);
}

private void DrawText (Graphics g,bool Xiacen)
{
Rectangle rect= this. ClientRectangle;
Rectangle rect1= this. ClientRectangle;
StringFormat stringformat=new StringFormat ();
Stringformat.alignment=stringalignment.center;
Stringformat.linealignment=stringalignment.center;
Rect. Y=this. CLIENTRECTANGLE.HEIGHT/5;
if (Xiacen)
{
Rect. Y=rect. y+4;
Rect1. Y=rect1. y+4;
}

Font Font=this. Font;


if (MouseMove)
{
Font=new Font (this. Font,fontstyle.underline);
}

g.DrawString (this. Text,font,
New SolidBrush (Color.FromArgb (0x66,backcolor)), Rect,stringformat);
g.DrawString (this. Text,font,new SolidBrush (this. ForeColor), Rect1,stringformat);
}

private void Usercontrol1_mousedown (object sender, System.Windows.Forms.MouseEventArgs e)
{
if (Xiacen==false)
{
Xiacen=true;
This. Refresh ();
}
}

private void Usercontrol1_mouseup (object sender, System.Windows.Forms.MouseEventArgs e)
{
if (xiacen==true)
{
Xiacen=false;
This. Refresh ();
}
}

private void Usercontrol1_paint (object sender, System.Windows.Forms.PaintEventArgs e)
{


}

protected override void OnPaint (PaintEventArgs e)
{

Base. OnPaint (e);
E.graphics.fillrectangle (New SolidBrush (BackColor), 0,0,this. Width,this. Height);
e.graphics.smoothingmode=smoothingmode.highquality;
E.graphics.textrenderinghint=textrenderinghint.antialiasgridfit;
Rectangle rect=new Rectangle (0,0,this. Width,this. Height);
GraphicsPath Clientpath=getgraphicspath (rect);
E.graphics.fillpath (New SolidBrush (BackColor), Clientpath);
This. Region=new System.Drawing.Region (Clientpath);
Drawyinying (E.graphics,xiacen);
Drawgaoliang (E.graphics,xiacen);
DrawText (E.graphics,xiacen);

if (this. Focused)
{
E.graphics.drawpath (New Pen (Color.FromArgb (0x22,0xff,0xff,0xff), 3), Clientpath);
}

}





private void Usercontrol1_backcolorchanged (object sender, System.EventArgs e)
{
int R=BACKCOLOR.R;
int g=backcolor.g;
int b=backcolor.b;
r=r+0x22;
g=g+0x22;
b=b+0x22;
if (r>255) r=255;
if (g>255) g=255;
if (b>255) b=255;
Backcolor=color.fromargb (R,G,B);
}

private void Usercontrol1_keydown (object sender, System.Windows.Forms.KeyEventArgs e)
{
if (Xiacen==false && e.keycode==keys.space)
{
Xiacen=true;
This. Refresh ();
}
}

private void Usercontrol1_keyup (object sender, System.Windows.Forms.KeyEventArgs e)
{
if (xiacen==true && e.keycode==keys.space)
{
Xiacen=false;
This. Refresh ();
}
}

private void Usercontrol1_mouseenter (object sender, System.EventArgs e)
{
if (Mousemove==false)
{
Mousemove=true;
This. Refresh ();
}
}

private void Usercontrol1_mouseleave (object sender, System.EventArgs e)
{
if (mousemove==true)
{
Mousemove=false;
This. Refresh ();
}
}

}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.