`

vista风格的按钮控件

阅读更多

vista风格的按钮  vistabutton
下载


Code
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace VistaButton
{
    
/// <summary>
    
/// 重写Windows系统按钮空间 A replacement for the Windows Button Control.
    
/// 技术支持 http://www.softbk.com     http://wuyisky.cnblogs.com  
    
/// wuyisky1@163.com
    
/// </summary>
    [DefaultEvent("Click")]
    
public class VistaButton : System.Windows.Forms.UserControl
    {

        
#region -  Designer  -

        
private System.ComponentModel.Container components = null;

        
/// <summary>
        
/// Initialize the component with it's
        
/// default settings.
        
/// </summary>
        public VistaButton()
        {
            InitializeComponent();

            
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            
this.SetStyle(ControlStyles.DoubleBuffer, true);
            
this.SetStyle(ControlStyles.ResizeRedraw, true);
            
this.SetStyle(ControlStyles.Selectable, true);
            
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            
this.SetStyle(ControlStyles.UserPaint, true);
            
this.BackColor = Color.Transparent;
            mFadeIn.Interval 
= 30;
            mFadeOut.Interval 
= 30;
        }

        
/// <summary>
        
/// Release resources used by the control.
        
/// </summary>
        protected override void Dispose(bool disposing)
        {
            
if (disposing)
            {
                
if (components != null)
                {
                    components.Dispose();
                }
            }
            
base.Dispose(disposing);
        }

        
#region -  Component Designer generated code  -

        
private void InitializeComponent()
        {
            
// 
            
// VistaButton
            
// 
            this.Name = "VistaButton";
            
this.Size = new System.Drawing.Size(10032);
            
this.Paint += new System.Windows.Forms.PaintEventHandler(this.VistaButton_Paint);
            
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.VistaButton_KeyUp);
            
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.VistaButton_KeyDown);
            
this.MouseEnter += new System.EventHandler(this.VistaButton_MouseEnter);
            
this.MouseLeave += new System.EventHandler(this.VistaButton_MouseLeave);
            
this.MouseUp += new MouseEventHandler(VistaButton_MouseUp);
            
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.VistaButton_MouseDown);
            
//this.GotFocus += new EventHandler(VistaButton_MouseEnter);
            
//this.LostFocus += new EventHandler(VistaButton_MouseLeave);
            this.mFadeIn.Tick += new EventHandler(mFadeIn_Tick);
            
this.mFadeOut.Tick += new EventHandler(mFadeOut_Tick);
            
this.Resize += new EventHandler(VistaButton_Resize);
        }

        
#endregion

        
#endregion

        
#region -  Enums  -

        
/// <summary>
        
/// A private enumeration that determines 
        
/// the mouse state in relation to the 
        
/// current instance of the control.
        
/// </summary>
        enum State { None, Hover, Pressed };

        
/// <summary>
        
/// A public enumeration that determines whether
        
/// the button background is painted when the 
        
/// mouse is not inside the ClientArea.
        
/// </summary>
        public enum Style
        {
            
/// <summary>
            
/// Draw the button as normal
            
/// </summary>
            Default,
            
/// <summary>
            
/// Only draw the background on mouse over.
            
/// </summary>
            Flat
        };

        
#endregion

        
#region -  Properties  -

        
#region -  Private Variables  -

        
private bool calledbykey = false;
        
private State mButtonState = State.None;
        
private Timer mFadeIn = new Timer();
        
private Timer mFadeOut = new Timer();
        
private int mGlowAlpha = 0;

        
#endregion

        
#region -  Text  -

        
private string mText;
        
/// <summary>
        
/// The text that is displayed on the button.
        
/// </summary>
        [Category("Text"),
         Description(
"The text that is displayed on the button.")]
        
public string ButtonText
        {
            
get { return mText; }
            
set { mText = value; this.Invalidate(); }
        }

        
private Color mForeColor = Color.White;
        
/// <summary>
        
/// The color with which the text is drawn.
        
/// </summary>
        [Category("Text"),
         Browsable(
true),
         DefaultValue(
typeof(Color), "White"),
         Description(
"The color with which the text is drawn.")]
        
public override Color ForeColor
        {
            
get { return mForeColor; }
            
set { mForeColor = value; this.Invalidate(); }
        }

        
private ContentAlignment mTextAlign = ContentAlignment.MiddleCenter;
        
/// <summary>
        
/// The alignment of the button text
        
/// that is displayed on the control.
        
/// </summary>
        [Category("Text"),
         DefaultValue(
typeof(ContentAlignment), "MiddleCenter"),
         Description(
"The alignment of the button text " +
                     
"that is displayed on the control.")]
        
public ContentAlignment TextAlign
        {
            
get { return mTextAlign; }
            
set { mTextAlign = value; this.Invalidate(); }
        }

        
#endregion

        
#region -  Image  -

        
private Image mImage;
        
/// <summary>
        
/// The image displayed on the button that 
        
/// is used to help the user identify
        
/// it's function if the text is ambiguous.
        
/// </summary>
        [Category("Image"),
         DefaultValue(
null),
         Description(
"The image displayed on the button that " +
                     
"is used to help the user identify" +
                     
"it's function if the text is ambiguous.")]
        
public Image Image
        {
            
get { return mImage; }
            
set { mImage = value; this.Invalidate(); }
        }

        
private ContentAlignment mImageAlign = ContentAlignment.MiddleLeft;
        
/// <summary>
        
/// The alignment of the image 
        
/// in relation to the button.
        
/// </summary>
        [Category("Image"),
         DefaultValue(
typeof(ContentAlignment), "MiddleLeft"),
         Description(
"The alignment of the image " +
                     
"in relation to the button.")]
        
public ContentAlignment ImageAlign
        {
            
get { return mImageAlign; }
            
set { mImageAlign = value; this.Invalidate(); }
        }

        
private Size mImageSize = new Size(2424);
        
/// <summary>
        
/// The size of the image to be displayed on the
        
/// button. This property defaults to 24x24.
        
/// </summary>
        [Category("Image"),
         DefaultValue(
typeof(Size), "24, 24"),
         Description(
"The size of the image to be displayed on the" +
                     
"button. This property defaults to 24x24.")]
        
public Size ImageSize
        {
            
get { return mImageSize; }
            
set { mImageSize = value; this.Invalidate(); }
        }

        
#endregion

        
#region -  Appearance  -

        
private Style mButtonStyle = Style.Default;
        
/// <summary>
        
/// Sets whether the button background is drawn 
        
/// while the mouse is outside of the client area.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
typeof(Style), "Default"),
         Description(
"Sets whether the button background is drawn " +
                     
"while the mouse is outside of the client area.")]
        
public Style ButtonStyle
        {
            
get { return mButtonStyle; }
            
set { mButtonStyle = value; this.Invalidate(); }
        }

        
private int mCornerRadius = 8;
        
/// <summary>
        
/// The radius for the button corners. The 
        
/// greater this value is, the more 'smooth' 
        
/// the corners are. This property should
        
///  not be greater than half of the 
        
///  controls height.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
8),
         Description(
"The radius for the button corners. The " +
                     
"greater this value is, the more 'smooth' " +
                     
"the corners are. This property should " +
                     
"not be greater than half of the " +
                     
"controls height.")]
        
public int CornerRadius
        {
            
get { return mCornerRadius; }
            
set { mCornerRadius = value; this.Invalidate(); }
        }

        
private Color mHighlightColor = Color.White;
        
/// <summary>
        
/// The colour of the highlight on the top of the button.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
typeof(Color), "White"),
         Description(
"The colour of the highlight on the top of the button.")]
        
public Color HighlightColor
        {
            
get { return mHighlightColor; }
            
set { mHighlightColor = value; this.Invalidate(); }
        }

        
private Color mButtonColor = Color.Black;
        
/// <summary>
        
/// The bottom color of the button that 
        
/// will be drawn over the base color.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
typeof(Color), "Black"),
         Description(
"The bottom color of the button that " +
                     
"will be drawn over the base color.")]
        
public Color ButtonColor
        {
            
get { return mButtonColor; }
            
set { mButtonColor = value; this.Invalidate(); }
        }

        
private Color mGlowColor = Color.FromArgb(141189255);
        
/// <summary>
        
/// The colour that the button glows when
        
/// the mouse is inside the client area.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
typeof(Color), "141,189,255"),
         Description(
"The colour that the button glows when " +
                     
"the mouse is inside the client area.")]
        
public Color GlowColor
        {
            
get { return mGlowColor; }
            
set { mGlowColor = value; this.Invalidate(); }
        }

        
private Image mBackImage;
        
/// <summary>
        
/// The background image for the button, 
        
/// this image is drawn over the base 
        
/// color of the button.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
null),
         Description(
"The background image for the button, " +
                     
"this image is drawn over the base " +
                     
"color of the button.")]
        
public Image BackImage
        {
            
get { return mBackImage; }
            
set { mBackImage = value; this.Invalidate(); }
        }

        
private Color mBaseColor = Color.Black;
        
/// <summary>
        
/// The backing color that the rest of 
        
/// the button is drawn. For a glassier 
        
/// effect set this property to Transparent.
        
/// </summary>
        [Category("Appearance"),
         DefaultValue(
typeof(Color), "Black"),
         Description(
"The backing color that the rest of" +
                     
"the button is drawn. For a glassier " +
                     
"effect set this property to Transparent.")]
        
public Color BaseColor
        {
            
get { return mBaseColor; }
            
set { mBaseColor = value; this.Invalidate(); }
        }

        
#endregion

        
#endregion

        
#region -  Functions  -

        
private GraphicsPath RoundRect(RectangleF r, float r1, float r2, float r3, float r4)
        {
            
float x = r.X, y = r.Y, w = r.Width, h = r.Height;
            GraphicsPath rr 
= new GraphicsPath();
            rr.AddBezier(x, y 
+ r1, x, y, x + r1, y, x + r1, y);
            rr.AddLine(x 
+ r1, y, x + w - r2, y);
            rr.AddBezier(x 
+ w - r2, y, x + w, y, x + w, y + r2, x + w, y + r2);
            rr.AddLine(x 
+ w, y + r2, x + w, y + h - r3);
            rr.AddBezier(x 
+ w, y + h - r3, x + w, y + h, x + w - r3, y + h, x + w - r3, y + h);
            rr.AddLine(x 
+ w - r3, y + h, x + r4, y + h);
            rr.AddBezier(x 
+ r4, y + h, x, y + h, x, y + h - r4, x, y + h - r4);
            rr.AddLine(x, y 
+ h - r4, x, y + r1);
            
return rr;
        }



        
#endregion

        
#region -  Drawing  -

        
/// <summary>
        
/// Draws the outer border for the control
        
/// using the ButtonColor property.
        
/// </summary>
        
/// <param name="g">The graphics object used in the paint event.</param>
        private void DrawOuterStroke(Graphics g)
        {
            
if (this.ButtonStyle == Style.Flat && this.mButtonState == State.None) { return; }
            Rectangle r 
= this.ClientRectangle;
            r.Width 
-= 1; r.Height -= 1;
            
using (GraphicsPath rr = RoundRect(r, CornerRadius, CornerRadius, CornerRadius, CornerRadius))
            {
                
using (Pen p = new Pen(this.ButtonColor))
                {
                    g.DrawPath(p, rr);
                }
            }
        }

        
/// <summary>
        
/// Draws the inner border for the control
        
/// using the HighlightColor property.
        
/// </summary>
        
/// <param name="g"><span s
分享到:
评论

相关推荐

    C#制作的Vista风格的按钮控件

    总的来说,创建一个C#的Vista风格按钮控件涉及到多个方面的知识,包括图形绘制、事件处理、动画实现和性能优化。虽然这个过程可能较为复杂,但一旦掌握,你将能够为你的应用程序增添独特的视觉吸引力和用户体验。...

    C# Vista风格日历控件DLL,可直接加入工程

    Vista风格的日历控件是其中一种,它提供了美观的界面和便捷的日历功能。本篇将详细讲解如何利用标题中提及的"C# Vista风格日历控件DLL"进行开发。 首先,这个控件是一个动态链接库(DLL)文件,名为`VistaCalendar....

    漂亮Vista风格按钮示例(C#源码)

    在Windows Forms中,虽然没有直接的Vista风格按钮控件,但我们可以通过自定义控件来模拟这种效果。这通常涉及到重绘按钮的背景和边框,使用GDI+图形库来实现半透明和动态效果。我们可以覆盖`OnPaint`事件,并在其中...

    Vista风格日历控件

    如果源码没有直接使用Aero Glass,那么可能是通过绘制自定义的背景、边框和按钮来模拟Vista风格。 3. **事件处理**:C#中的事件处理机制是控件交互的核心。日历控件会包含多种事件,如日期选择、月份切换等。开发者...

    Vista风格日历控件C#源码

    接下来,通过重绘控件来实现Vista风格的视觉效果,这通常涉及到绘制背景、边框、按钮等元素。使用Graphics类和Pen、Brush对象进行绘制,并利用Windows API调用来获取系统主题颜色,以保持与Vista风格的一致性。 2. ...

    C#Vista风格按钮源码 C#下自定义控件的范例

    漂亮的Vista风格按钮源码全用函数画出,没用图片,是自定义控件最好的范例。 效果图和代码说明:http://www.our-code.com/news/2010920/n4649132.html C#Vista风格按钮源码 C#下自定义控件的范例

    C#Vista风格日历控件2021.rar

    "C# Vista风格日历控件2021.rar"是一个包含C#实现的,模仿Windows Vista界面设计的日历和日期选择组件的压缩包。这个控件不仅提供了基本的日历展示功能,还可能具备了Vista风格的美观界面和增强的用户体验。 Vista...

    VB仿Vista风格真彩色图标按钮控件

    标题中的“VB仿Vista风格真彩色图标按钮控件”是指在Visual Basic(VB)环境中开发的一种用户界面组件,它模仿了微软Windows Vista操作系统中的视觉样式。这种控件允许开发者在他们的应用程序中创建具有真实色彩、高...

    用户自定义控件Vista风格按钮

    总的来说,"用户自定义控件Vista风格按钮"项目展示了C#中自定义控件的创建过程,包括继承已有控件、重绘控件外观、处理用户交互事件以及封装和分发自定义控件。这个项目不仅有助于提升应用程序的视觉效果,也是深入...

    .net逼真的仿Vista效果的按钮控件

    "逼真的仿Vista效果的按钮控件"再次强调了这个控件的主要特性,即其能够高度模拟Vista风格的按钮,包括色彩、阴影、高光等细节,以提供更吸引人的用户界面。 标签“.net”指示了该控件是基于.NET Framework的,这是...

    控件类库Vista风格日历控件C#版,C#源代码

    【标题】:“控件类库Vista风格日历控件C#版”是针对Windows操作系统Vista界面设计的一款日历控件,它采用C#编程语言实现,为开发者提供了更现代、美观的用户界面元素。 【描述】:这个控件库的C#源代码为开发者...

    Vista风格按钮,相似度99.9%(C#代码)

    首先,`VistaButton.cs`文件是C#源代码文件,其中包含了实现Vista风格按钮的核心逻辑。这个类通常会继承自`System.Windows.Forms.Button`基类,并重写或扩展一些方法和属性,以便提供Vista样式的效果。开发者可能会...

    C# 写的Vista按钮的皮肤控件。。

    它是动态链接库,封装了所有与Vista风格按钮相关的功能和样式。开发人员在项目中引用这个DLL后,就可以在代码中实例化VistaButton控件,并可以通过属性设置来调整按钮的外观和行为,例如颜色、大小、文本、图标等。...

    VB仿Vista风格窗体控件源码.7z

    这个"VB仿Vista风格窗体控件源码.7z"压缩包提供了模仿Windows Vista操作系统界面风格的控件集,对于开发者来说,可以方便地在VB项目中创建具有Vista视觉效果的应用程序。 Vista风格的界面设计注重用户体验,引入了...

    C# Vista风格按钮

    在压缩包文件"Vista风格按钮"中,可能包含了一些示例代码或库文件,用于展示如何在C#项目中创建和使用这些风格的按钮。如果能够查看这些文件,将有助于深入理解并实践上述知识。在实际操作时,记得根据项目需求选择...

    VB6功能强大的按钮控件

    能实现java、mac、vista等多种风格的按钮,可自定义添加PNG透明图片,实现不规则图形按钮。

    C# 漂亮vista按钮

    "C# 漂亮vista按钮"这个项目是利用Visual Studio 2008(简称VS2008)来创建一个具有Vista风格的按钮控件。Vista是微软在2007年发布的一款操作系统,它引入了全新的用户界面和设计元素,其中就包括更加现代化和美观的...

    VB6.0 Xp 风格控件

    4. **主题兼容性**:虽然这些控件模仿XP风格,但也要考虑程序在不同操作系统上的表现,确保在Windows XP、Vista、Win7甚至更高版本上都能正常工作。 5. **性能优化**:由于XP风格控件可能增加了额外的图形渲染,...

    mfc 各种控件的使用和布局(透明vista风格)

    在本文中,我们将深入探讨MFC(Microsoft Foundation Classes)中各种控件的使用与布局,特别是在实现透明Vista风格界面的技巧。MFC是微软提供的一套C++库,用于构建Windows应用程序,它极大地简化了Windows API的...

Global site tag (gtag.js) - Google Analytics