用于。net紧凑框架的颜色按钮

介绍 我写这篇文章有两个原因。首先,虽然标准的。net框架允许你改变按钮的颜色,但是这个特性在紧凑的框架中是缺失的。按钮在紧凑的框架是无聊的黑色上的灰色。写这篇文章的第二个原因是,虽然编写标准控件相对简单,但是为紧凑框架编写控件就有点困难了,因为在Visual Studio中没有用于紧凑框架控件的项目模板。我可以想象这两个问题都将被微软及时解决,但就目前而言,需要一个用户控件,而且这个控件编写起来并不简单。 创建控制 已经有几篇文章描述了如何为compact框架创建控件,所以我在这里不再重复这些文章。在我看来,在MSDN网站上有一篇比较好的文章:为。net紧凑框架创建自定义控件,作者Chris Kinsman, Vergent Software, 2002年9月。这花了我一段时间来让它工作,但结果是,因为有相当多的步骤要遵循,你必须确保你没有错过任何一个! 的代码 创建了一个空白控件后,我开始将其更改为按钮。我想创建一个与标准。net CF按钮行为相同的按钮,但是颜色更多。我决定给按钮4个新的颜色属性。这些是:隐藏,复制Code

Color m_NormalBtnColour = Color.LightYellow;
Color m_NormalTxtColour = Color.Blue;
Color m_PushedBtnColour = Color.Blue;
Color m_PushedTxtColour = Color.Yellow;

这些属性由控件使用以下代码公开(显示四个属性中的一个,其他属性相同,但有不同的名称和描述):复制Code

#if NETCFDESIGNTIME
    [Category("Appearance")]
    [DefaultValue(3)]
    [Description("The normal colour of the button.")]
#endif
public Color NormalBtnColour
{
    set
    {
        m_NormalBtnColour = value;
        Invalidate();
    }
    get
    {
        return m_NormalBtnColour;
    }
}

注意,我在颜色改变后使控件无效。这样在设计窗体时,当颜色发生变化时,控件就会重新绘制。我还必须删除两个标准的颜色属性:背景色和前景色。我使用以下代码完成了此操作(所示两段代码中的一段):复制Code

#if NETCFDESIGNTIME
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
#endif
public override Color BackColor
{
    set
    {;}
    get
    { return new Color(); }
}

Browsable(false)从属性窗口中删除该项,EditorBrowsable(Never)阻止intellisense显示该属性。最后,对于属性,我添加了按钮状态,可以是正常状态,也可以是按下状态。隐藏,复制Code

public enum States 
{ 
    Normal, 
    Pushed 
}

States m_state;

这在构造函数中设置为normal,在鼠标下推事件中设置为push,在鼠标上推事件中设置为normal。另外,为了在设计模式下正确绘制按钮,当按钮被调整大小时,OnResize被覆盖以使控件无效。隐藏,复制Code

protected override void OnMouseDown(
            System.Windows.Forms.MouseEventArgs e) 
{
    m_state = States.Pushed; 
    // button receives input focus
    Focus();  
    base.OnMouseDown(e); 
    Invalidate(); 
} 

protected override void OnMouseUp(
            System.Windows.Forms.MouseEventArgs e) 
{
    m_state = States.Normal;
    base.OnMouseUp(e);
    Invalidate();
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    Invalidate();
}

剩下唯一要做的是OnPaint方法。如果按钮处于正常状态,则使用两种正常颜色绘制按钮;如果按钮处于按下状态,则使用两种按下颜色绘制按钮。隐藏,收缩,复制Code

protected override void OnPaint(PaintEventArgs e) 
{
    Graphics graphics = e.Graphics;

    Pen pen;
    SolidBrush brush;
    SolidBrush textBrush;

    //Work out the colours that we should be using
    // for the text and background
    if (m_state == States.Normal)
    {
        brush = new SolidBrush(m_NormalBtnColour);
        textBrush = new SolidBrush(m_NormalTxtColour);
        pen = new Pen(m_NormalTxtColour);
    }
    else
    {
        brush = new SolidBrush(m_PushedBtnColour);
        textBrush = new SolidBrush(m_PushedTxtColour);
        pen = new Pen(m_PushedTxtColour);
    }

    //Draw a rectangle and fill the inside
    graphics.FillRectangle(brush, 0, 0, Width, Height);
    graphics.DrawRectangle(pen, 0, 0, Width-1, Height-1);

    //Create a font based on the default font
    int fontHeight = 10;
    Font font = new Font(FontFamily.GenericSerif,
               fontHeight, FontStyle.Bold);

    //Find out the size of the text
    SizeF textSize = new SizeF();
    textSize = e.Graphics.MeasureString(Text, font);

    //Work out how to position the text centrally
    float x=0,y=0;
    if (textSize.Width < Width)
        x = (Width - textSize.Width) /2;
    if (textSize.Height < Height)
        y = (Height - textSize.Height) /2;

    //Draw the text in the centre of the button using
    // the default font
    graphics.DrawString(Text, font, textBrush, x, y);
}

如“构建控件”文章中所述,有两种解决方案,一种用于实际控件,另一种用于设计器中的控件。我编写了一个批处理文件来构建这两个解决方案并将程序集复制到正确的位置,以便Visual Studio能够获取它们。一旦控件被添加到“我的用户控件”中,它们就可以被拖放到表单中,并像操纵其他控件一样进行操作。 历史 2004年1月5日-初始版本。 本文转载于:http://www.diyabc.com/frontweb/news522.html

原文地址:https://www.cnblogs.com/Dincat/p/13451272.html