65.9K
CodeProject 正在变化。 阅读更多。
Home

适用于 .NET Compact Framework 的颜色按钮

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (9投票s)

2004年1月5日

3分钟阅读

viewsIcon

218987

downloadIcon

1044

演示如何为 .NET Compact Framework 编写颜色按钮控件。

Sample Image - ColourButtons.gif

引言

我写这篇文章有两个原因。第一个原因是,虽然标准的 .NET 框架允许您更改按钮的颜色,但 Compact Framework 缺少此功能。Compact Framework 中的按钮是无聊的黑色和灰色。写这篇文章的第二个原因是,虽然编写标准控件相对简单,但为 Compact Framework 编写控件更具挑战性,因为 Visual Studio 中没有 Compact Framework 控件的项目模板。我想这两个问题都会被微软及时解决,但目前,需要一个用户控件,而这个控件的编写并非易事。

创建控件

已经有几篇文章描述了如何为 Compact Framework 创建一个控件,所以这里我不会重复这些文章。在我看来,一篇较好的文章是在 MSDN 网站上:为 .NET Compact Framework 创建自定义控件,作者是 Chris Kinsman,Vergent Software,2002 年 9 月。我花了一段时间才让它工作,但事实证明是因为需要遵循很多步骤,而且您必须确保您没有遗漏任何步骤!

代码

创建了一个空白控件后,我开始更改它以使其像一个按钮一样工作。我想创建一个按钮,它的行为与标准的 .NET CF 按钮相同,但有更多的颜色。我决定给按钮添加 4 个新的颜色属性。这些是

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

这些属性由控件使用以下代码公开(其中显示了四个中的一个 - 其他属性具有相同的名称和描述)

#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;
    }
}

请注意,在颜色更改后,我使控件无效。这样,当在设计窗体时更改颜色时,控件会被重绘。我还必须删除两个标准的颜色属性:BackColorForeColor。我使用以下代码执行此操作(显示两个中的一个)

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

Browsable(false) 从属性窗口中删除该项目,而 EditorBrowsable(Never) 阻止 Intellisense 显示该属性。最后,对于这些属性,我添加了按钮状态,该状态是 normalpushed

public enum States 
{ 
    Normal, 
    Pushed 
}

States m_state;

这在构造函数中被设置为 normal,并且会在鼠标按下事件中被设置为 pushed,在鼠标释放事件中被设置为 normal。此外,为了使按钮在设计模式下正确绘制,重写了 OnResize 以在调整按钮大小时使控件无效。

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 方法。如果按钮处于 normal 状态,则使用两种正常颜色绘制按钮,如果按钮处于 pushed 状态,则使用两种按下颜色绘制按钮。

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 日 - 初始版本。
© . All rights reserved.