如何更改组合框的滚动条的背景色 [英] How to change the combobox's scrollbar's background color

查看:75
本文介绍了如何更改组合框的滚动条的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Winform的C#中更改组合框的滚动条的背景颜色? (不在Webform中)?

麻烦您举个例子吗?谢谢大家!

解决方案

我不认为这可以通过编程实现.

使用Windows API,您可以获得COMBOBOXINFO结构,对于某些部分,例如编辑窗口或列表,但对于滚动条,您将获得handle结构. AFAIK您需要一个手柄才能更改颜色.

我发现有几篇文章/博客显示了如何用不同的控件代替标准的dropdown列表,因此大概可以用自己设计的scrollbar设计自己的控件,并将其替换为ComboBox .

虽然只是更改滚动条的颜色,但仍需要大量工作.并绑定到列表框中选择的索引,依此类推,但不错的开始

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace YourNameHere
{
    public class AdvancedComboBox : Panel
    {
        private System.Windows.Forms.TextBox textBox1 = new TextBox();
        private System.Windows.Forms.Panel panel1 = new Panel();
        private System.Windows.Forms.ListBox listBox1 = new ListBox();

        public AdvancedComboBox()
        {
            InitializeComponent();
            listBox1.Visible = false;

            listBox1.Items.AddRange(new object[] { "Apple", "Pear", "Orange", "Grape", "Lemon", "Lime" });
            panel1.BackgroundImage = setButtonOff();
        }

        private void InitializeComponent()
        {
            this.Controls.AddRange(new Control[] { textBox1, panel1, listBox1 });
            this.panel1.Location = new System.Drawing.Point(128, 59);
            
            this.panel1.Size = new System.Drawing.Size(15, 20);            
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
            this.panel1.MouseLeave += new System.EventHandler(this.panel1_MouseLeave);
            this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(28, 79);
            this.listBox1.Size = new System.Drawing.Size(115, 95);
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            this.listBox1.MouseLeave += new System.EventHandler(this.listBox1_MouseLeave);

            this.textBox1.Location = new System.Drawing.Point(27, 59);            
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            
        }

        private void listBox1_MouseLeave(object sender, EventArgs e)
        {
            listBox1.Visible = false;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = listBox1.SelectedItem.ToString();
            listBox1.Visible = false;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Visible)
            { listBox1.Visible = false; }
            else { listBox1.Visible = true; }
        }
        private void panel1_MouseHover(object sender, EventArgs e)
        {
            panel1.BackgroundImage = setButtonOver();
        }
        private Image setButtonOff()
        {
            string base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAADZrBkAAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAARUlEQVQokWP8//8/A4ng1duPjGRoY2BgYCJDz6g2bIAFqygjIyOmIHJUYbcNMzLRRHA6Elkdpin4/AZRjTUZjSYuamkDAD0zINGCHX3yAAAAAElFTkSuQmCC";

            byte[] base64byte = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(base64byte);
            return Image.FromStream(ms);
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {

            panel1.BackgroundImage = setButtonOff();
        }

        private Image setButtonOver()
        {
            string base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAAGuqymWAAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sDGQA2Npg6IGUAAAB0SURBVCjPY1z54z8DAwMDAwMTAwNDOAcjAwMDI1wMxoJQjCgUHDAxoAIUeSYsKtFtQVeGbgE+y/DJQewhbCZOuQd/cdunwEySW5ABC1YXQhzBhMZHZlArXIgNazKNHDTaWHBJIAc7cgATFSThHNgVDImQBAAVrjj7k1ImuAAAAABJRU5ErkJggg==";

            byte[] base64byte = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(base64byte);
            return Image.FromStream(ms);
        }
    }
}


How to change the combobox''s scrollbar''s background color in C# of winform? (not in webform)?

Cound you give me an example? Thank all in advance!

解决方案

I don''t think that this can be done programatically.

Using the Windows API you can get a COMBOBOXINFO structure which will get you a handle for some parts, the edit window for example or the list, but not for the scrollbar. AFAIK you would need a handle in order to change the colour.

I have found several articles/blogs that show how to substitute a different control for the standard dropdown list so presumably you could design your own control with a scrollbar, also of your own design, and substitute it into the ComboBox.

It would be a lot of work though just to change the scrollbar colours.


Here is a basic control that i put together in a few minutes but you will need to make properties like private int selctedindex and tie into listbox selected index and so forth but a decent start

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace YourNameHere
{
    public class AdvancedComboBox : Panel
    {
        private System.Windows.Forms.TextBox textBox1 = new TextBox();
        private System.Windows.Forms.Panel panel1 = new Panel();
        private System.Windows.Forms.ListBox listBox1 = new ListBox();

        public AdvancedComboBox()
        {
            InitializeComponent();
            listBox1.Visible = false;

            listBox1.Items.AddRange(new object[] { "Apple", "Pear", "Orange", "Grape", "Lemon", "Lime" });
            panel1.BackgroundImage = setButtonOff();
        }

        private void InitializeComponent()
        {
            this.Controls.AddRange(new Control[] { textBox1, panel1, listBox1 });
            this.panel1.Location = new System.Drawing.Point(128, 59);
            
            this.panel1.Size = new System.Drawing.Size(15, 20);            
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
            this.panel1.MouseLeave += new System.EventHandler(this.panel1_MouseLeave);
            this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(28, 79);
            this.listBox1.Size = new System.Drawing.Size(115, 95);
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            this.listBox1.MouseLeave += new System.EventHandler(this.listBox1_MouseLeave);

            this.textBox1.Location = new System.Drawing.Point(27, 59);            
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            
        }

        private void listBox1_MouseLeave(object sender, EventArgs e)
        {
            listBox1.Visible = false;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = listBox1.SelectedItem.ToString();
            listBox1.Visible = false;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Visible)
            { listBox1.Visible = false; }
            else { listBox1.Visible = true; }
        }
        private void panel1_MouseHover(object sender, EventArgs e)
        {
            panel1.BackgroundImage = setButtonOver();
        }
        private Image setButtonOff()
        {
            string base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAADZrBkAAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAARUlEQVQokWP8//8/A4ng1duPjGRoY2BgYCJDz6g2bIAFqygjIyOmIHJUYbcNMzLRRHA6Elkdpin4/AZRjTUZjSYuamkDAD0zINGCHX3yAAAAAElFTkSuQmCC";

            byte[] base64byte = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(base64byte);
            return Image.FromStream(ms);
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {

            panel1.BackgroundImage = setButtonOff();
        }

        private Image setButtonOver()
        {
            string base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAAGuqymWAAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sDGQA2Npg6IGUAAAB0SURBVCjPY1z54z8DAwMDAwMTAwNDOAcjAwMDI1wMxoJQjCgUHDAxoAIUeSYsKtFtQVeGbgE+y/DJQewhbCZOuQd/cdunwEySW5ABC1YXQhzBhMZHZlArXIgNazKNHDTaWHBJIAc7cgATFSThHNgVDImQBAAVrjj7k1ImuAAAAABJRU5ErkJggg==";

            byte[] base64byte = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(base64byte);
            return Image.FromStream(ms);
        }
    }
}


这篇关于如何更改组合框的滚动条的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆