共用方式為


HOW TO:使用 ColorDialog 元件顯示色板

更新:2007 年 11 月

ColorDialog 元件會顯示色板,並傳回包含使用者所選取的色彩屬性。

若要使用 ColorDialog 元件選擇色彩

  1. 使用 ShowDialog 方法顯示對話方塊。

  2. 使用 DialogResult 屬性決定對話方塊關閉的方式。

  3. 使用 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);
    

請參閱

參考

ColorDialog

其他資源

ColorDialog 元件 (Windows Form)