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

如何编写 HatchStyle 下拉列表

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.61/5 (10投票s)

2005年8月29日

1分钟阅读

viewsIcon

88141

downloadIcon

925

在本文中,我们将学习如何编写一个 HatchStyle 下拉菜单。

Sample Image - HatchStyleComboBox1.jpg

引言

在本文中,我们将学习如何编写一个 HatchStyle 下拉菜单。通常,Windows 允许我们使用 DrawItemMeasureItem 事件来自己绘制 ComboBox 中的项目,从而提供覆盖自动绘制的能力。我们使用 Drawmode 属性通过将其设置为 OwnerDrawVariable 来自己绘制项目。双缓冲可以防止控件重绘引起的闪烁。要完全启用双缓冲,还必须将 UserPaintAllPaintingInWmPaint 位设置为 true

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

通过这样做,Windows 将为添加到 ComboBox 中的每个项目向我们发送 DrawItemMeasureItem 事件。

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

Hatch 样式下拉菜单

HatchStyle 枚举指定由 HatchBrush 类型画笔使用的填充图案。填充图案由纯色背景和绘制在背景上的线条组成。下面的迭代将所有填充图案插入到下拉菜单中。

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

我们首先创建一个 Windows 应用程序。将 HatchStyle ComboBox 添加到窗体。将以下行添加到窗体的 Paint 事件中。窗体的 Invalidate 方法使窗体的特定区域失效,并导致向窗体发送 Paint 消息。

    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();
        }
    }

就是这样!

如何编写 HatchStyle 下拉菜单 - CodeProject - 代码之家
© . All rights reserved.