zoukankan      html  css  js  c++  java
  • 实现一个所有者绘制的组合框

    介绍 嗨,我已经等待 了很久很久,有人好心张贴代码为一个所有者画的组合框,但可惜,没有效果。是的,我是你的普通的,卑贱的蚂蚱,我一直在偷代码 代码项目已经存在很长时间了。看来这次我得自己写点东西了。我猜这个问题对你们大多数人来说太简单了。但请继续为相对初学者写文章,因为我们大多数人必须先从简单的事情开始。幸运的是,编写代码的结果是 childs使用。net框架。 好吧,让我们开始。首先要做的是将一个组合框放到窗体上,并将其DrawMode属性更改为OwnerDrawFixed。这将意味着出现在组合框中的所有项目将具有相同的维度。如果你要在ComboBox中绘制大小可变的项目,那么你会将属性更改为OwnerDrawVariable并被迫处理一个MeasureItem事件。这里我就不讲了。接下来,为ComboBox创建一个数据源,以及相应的图像集合。在我的例子中,我只使用了两个数组,下面的代码现在应该很容易理解了 的代码 隐藏,收缩,复制Code

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace WindowsApplication7
    {    
        public class Form1 : System.Windows.Forms.Form
        {        
            private System.ComponentModel.Container components = null;
            private String[] arr;
            private Image[] imageArr;
            private System.Windows.Forms.ComboBox comboBox1;        
            public Font myFont;        
            
            public Form1()
            {            
                InitializeComponent();
                myFont = new System.Drawing.Font("Comic Sans", 11);
                
                arr = new String[4];
                arr[0] = "Smiley Red Face";
                arr[1] = "Smiley Cry";
                arr[2] = "Smiley Big Grin";
                arr[3]  = "Smiley Suss";
                
                this.comboBox1.DataSource = arr; //set the combo's data source to out array
    
                imageArr = new Image[4];
    
                /* I stole these images off CP. I specifically chose these images because they 
                are all little 16x16 sized gifs. If you're going to use your own images make 
                sure they are all the same size, otherwise your code won't work as advertised. 
                In that case you will have to set the DrawMode property of your combobox to 
                OwnnerDrawVariable and catch the MeasureItem event*/
    
                imageArr[0] = new Bitmap("C:\smiley_redface.gif");
                imageArr[1] = new Bitmap("C:\smiley_cry.gif");
                imageArr[2] = new Bitmap("C:\smiley_biggrin.gif");
                imageArr[3] = new Bitmap("C:\smiley_suss.gif");
            }        
            
            //You have your windows forms designer generated code here
    
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }
    
            private void comboBox1_DrawItem(object sender, 
                                            System.Windows.Forms.DrawItemEventArgs e)
            {    
                // Let's highlight the currently selected item like any well 
                // behaved combo box should
                e.Graphics.FillRectangle(Brushes.Bisque, e.Bounds);                
                e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, 
                                      new Point(imageArr[e.Index].Width*2,e.Bounds.Y));       
                e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y));   
                
                //is the mouse hovering over a combobox item??            
                if((e.State & DrawItemState.Focus)==0)
                {                                                    
                    //this code keeps the last item drawn from having a Bisque background. 
                    e.Graphics.FillRectangle(Brushes.White, e.Bounds);                    
                    e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, 
                                          new Point(imageArr[e.Index].Width*2,e.Bounds.Y));
                    e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y)); 
                }    
            }                                                                
        }
    }

    结论 好了,这就是我的第一篇CodeProject文章,请大家放心。代码一开始相当冗长乏味,但随着我对DrawItemEventArgs的理解越来越清晰,我减少了它。我不太确定将ComboBox的默认背景色设置为白色以外的任何颜色是否很酷。但这似乎更像是品味问题。 问候,Senkwe 本文转载于:http://www.diyabc.com/frontweb/news384.html

  • 相关阅读:
    pytorch lstm crf 代码理解
    python sys.argv是什么?
    如何用简单易懂的例子解释条件随机场(CRF)模型?它和HMM有什么区别?
    jieba分词工具的使用方法
    手把手教你用Python实现自动特征工程
    命名实体识别视频51cto
    命名实体识别入门教程(必看)
    零基础入门--中文命名实体识别(BiLSTM+CRF模型,含代码)
    自然语言处理深度学习篇-BiLSTM文本挖掘实践 命名实体识别
    导航栏颜色
  • 原文地址:https://www.cnblogs.com/Dincat/p/13443965.html
Copyright © 2011-2022 走看看