如何写HatchStyle下拉列表

 

介绍 在本文中,我们

  

 

将看到如何编写HatchStyle下拉列表。通常,Windows允许我们自己在组合框中绘制项目。我们可以使用DrawItem和MeasureItem事件来提供覆盖自动绘制的能力,并且通过将Drawmode属性设置为OwnerDrawVariable,我们使用Drawmode属性来自己绘制项目。双缓冲防止因重绘控件而引起的闪烁。要完全启用双缓冲,还必须将UserPaint和AllPaintingInWmPaint位设置为true。隐藏,复制Code

public HSComboBox(): base()
{
    this.DrawMode = DrawMode.OwnerDrawVariable;
    this.SetStyle(ControlStyles.DoubleBuffer, true);
    this.InitializeDropDown();
}

通过这样做,Windows将为添加到组合框中的每个项发送DrawItem和MeasureItem事件。隐藏,收缩,复制Code

protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
    // The following method should generally be called before drawing.
    // It is actually superfluous here, since the subsequent drawing
    // will completely cover the area of interest.
    e.DrawBackground();
    //The system provides the context
    //into which the owner custom-draws the required graphics.
    //The context into which to draw is e.graphics.
    //The index of the item to be painted is e.Index.
    //The painting should be done into the area described by e.Bounds.

    if (e.Index != -1)
    {
        Graphics g = e.Graphics;
        Rectangle r = e.Bounds;

        Rectangle rd = r;
        rd.Width = rd.Left + 25;
        Rectangle rt = r;
        r.X = rd.Right;
        string displayText = this.Items[e.Index].ToString();

        HatchStyle hs = (HatchStyle)
          Enum.Parse(typeof(HatchStyle),displayText, true);;
        // TODO add user selected foreground
        // and background colors here

        HatchBrush b = new HatchBrush(hs, Color.Black, e.BackColor);
        g.DrawRectangle(new Pen(Color.Black, 2), rd.X + 3,
                   rd.Y + 2, rd.Width - 4, rd.Height - 4);
         g.FillRectangle(b, new Rectangle(rd.X + 3, rd.Y + 2,
                               rd.Width - 4, rd.Height - 4));

        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Near;
        //If the current item has focus.
        if((e.State & DrawItemState.Focus)==0)
        {
            e.Graphics.FillRectangle(new
              SolidBrush(SystemColors.Window), r);
            e.Graphics.DrawString(displayText, this.Font,
                    new SolidBrush(SystemColors.WindowText), r, sf);
        }
        else
        {
            e.Graphics.FillRectangle(new
              SolidBrush(SystemColors.Highlight), r);
            e.Graphics.DrawString(displayText, this.Font,
              new SolidBrush(SystemColors.HighlightText), r, sf);
        }
    }
    //Draws a focus rectangle on the specified graphics
    //surface and within the specified bounds.
    e.DrawFocusRectangle();
}

protected override void
  OnMeasureItem(System.Windows.Forms.MeasureItemEventArgs e)
{
    //Work out what the text will be
    string displayText = this.Items[e.Index].ToString();

    //Get width & height of string
    SizeF stringSize=e.Graphics.MeasureString(displayText, this.Font);

    //Account for top margin
    stringSize.Height += 5;

    // set hight to text height
    e.ItemHeight = (int)stringSize.Height;

    // set width to text width
    e.ItemWidth = (int)stringSize.Width;
}

舱口样式下拉 HatchStyle枚举指定HatchBrush类型的画笔使用的阴影模式。阴影模式由实线背景色和绘制在背景上的线组成。下面的迭代将所有的阴影模式插入到下拉菜单中:复制Code

protected void InitializeDropDown()
{
    foreach (string styleName in Enum.GetNames(typeof(HatchStyle)))
    {
        this.Items.Add(styleName);
    }
}

我们从创建一个Windows应用程序开始。在表单中添加HatchStyle组合框。将以下代码行添加到窗体的Paint事件中。窗体的Invalidate方法使窗体的特定区域无效,并导致将绘制消息发送到窗体。隐藏,复制Code

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    if (hsComboBox1.SelectedItem != null)
    {
        Graphics g = e.Graphics;

        HatchStyle hs = (HatchStyle)Enum.Parse(typeof(HatchStyle),
                         hsComboBox1.SelectedItem.ToString(), true);
        HatchBrush b = new HatchBrush(hs, Color.Black, this.BackColor);
        g.FillRectangle(b, new Rectangle (0,0,this.Width,this.Height));
    }
}

private void hsComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (hsComboBox1.SelectedItem != null)
    {
        this.Invalidate();
    }
}

就是这样! 本文转载于:http://www.diyabc.com/frontweb/news241.html

posted @ 2020-08-04 09:36  Dincat  阅读(229)  评论(0编辑  收藏  举报