comboBox_DrawItem事件设置项字体的字形

.Net技术 码拜 9年前 (2016-03-09) 902次浏览
comboBox_DrawItem事件怎么样设置项字体的字形,例如粗体,倾斜等
解决方案

10

comboBox.Font=new Font(comboBox.Font,comboBox.Font.Style|FontStyle.Italic|FontStyle.Bold);
//Font类构造函数中,第一个参数是字体类型(如宋体、隶书等),第二个是字体大小,单位是pt,第三个是字体样式(如粗、斜体、下划线等)

10

    public partial class Form5 : Form
    {
        private System.Windows.Forms.ComboBox comboBox1;
        public Form5()
        {
            InitializeComponent();
            comboBox1 = new ComboBox();
            this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(73, 252);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 22);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
            this.comboBox1.Items.Add("宋体");
            this.comboBox1.Items.Add("黑体");
            this.Controls.Add(this.comboBox1);
        }
        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                e.DrawBackground();
                string text = comboBox1.Items[e.Index].ToString();
                e.Graphics.DrawString(text, new Font(text, e.Font.Size), Brushes.Black, e.Bounds);
            }
        }
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明comboBox_DrawItem事件设置项字体的字形
喜欢 (0)
[1034331897@qq.com]
分享 (0)