ColorDialog 组件显示调色板,并返回一个属性,其中包含用户选择的颜色。
使用 ColorDialog 组件选择颜色
使用 ShowDialog 方法显示对话框。
使用 DialogResult 属性确定对话框的关闭方式。
使用 Color 组件的 ColorDialog 属性设置所选颜色。
在下面的示例中,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 Subprivate void button1_Click(object sender, System.EventArgs e) { if(colorDialog1.ShowDialog() == DialogResult.OK) { button1.BackColor = colorDialog1.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->Click += gcnew System::EventHandler(this, &Form1::button1_Click);