更新:2007 年 11 月
ColorDialog 组件显示调色板并返回包含用户选定颜色的属性。
使用 ColorDialog 组件选择颜色
- 使用 ShowDialog 方法显示对话框。 
- 使用 DialogResult 属性确定对话框是如何关闭的。 
- 使用 ColorDialog 组件的 Color 属性设置选定的颜色。 - 在下面的示例中,Button 控件的 Click 事件处理程序打开一个 ColorDialog 组件。当用户选定颜色并单击 “确定”后,Button 控件的背景色将设置为选定的颜色。本示例假设窗体具有一个 Button 控件和一个 ColorDialog 组件。 - Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If ColorDialog1.ShowDialog() = DialogResult.OK Then Button1.BackColor = ColorDialog1.Color End If End Sub- private void button1_Click(object sender, System.EventArgs e) { if(colorDialog1.ShowDialog() == DialogResult.OK) { button1.BackColor = colorDialog1.Color; } }- private void button1_Click(Object sender, System.EventArgs e) { if (colorDialog1.ShowDialog() == DialogResult.OK) { button1.set_BackColor(colorDialog1.get_Color()); } }- private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { if(colorDialog1->ShowDialog() == DialogResult::OK) { button1->BackColor = colorDialog1->Color; } }- ((Visual C# 和 Visual C++)在窗体的构造函数中放置以下代码,以注册事件处理程序。 - this.button1.Click += new System.EventHandler(this.button1_Click);- this.button1.add_Click(new System.EventHandler(this.button1_Click));- this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);