如何从另一个组合框(C#,Winforms)更改形状的颜色

本文关键字:颜色 Winforms 另一个 组合 | 更新日期: 2023-09-27 18:25:28

所以我要做的是使用另一个组合框更改形状的颜色。因此,第一个组合框表示什么样的形状将显示为图像。例如,如果我按下三角形选项,它将显示三角形。好吧,我有另一个组合框,显示形状的颜色。到目前为止,我的代码显示在这里:

  using System;
using System.Drawing;
using System.Windows.Forms;
namespace ComboBoxTest
{
   // Form uses a ComboBox to select different shapes to draw
   public partial class ComboBoxTestForm : Form
   {
      // constructor
      public ComboBoxTestForm()
      {
         InitializeComponent();
      } // end constructor
      Pen myPen;
      SolidBrush mySolidBrush;

      private void imageComboBox_SelectedIndexChanged(
         object sender, EventArgs e )
      {

         // create graphics object, Pen and SolidBrush
         Graphics myGraphics = base.CreateGraphics();
         // create Pen using color DarkRed

         // create SolidBrush using color DarkRed

         // clear drawing area setting it to color white
         myGraphics.Clear( Color.White );
         // find index, draw proper shape
         switch ( imageComboBox.SelectedIndex )
         {
            case 0: // case Circle is selected
               myGraphics.DrawEllipse( myPen, 50, 50, 150, 150 );
               break;
            case 1: // case Rectangle is selected
               myGraphics.DrawRectangle( myPen, 50, 50, 150, 150 );
               break;
            case 2: // case Ellipse is selected
               myGraphics.DrawEllipse( myPen, 50, 85, 150, 115 );
               break;
            case 3: // case Pie is selected
               myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45 );
               break;
             case 4:
                Point point1 = new Point(150,  50);
                Point point2 = new Point(100,  150);
                Point point3 = new Point(200,   150);
                Point[] curvePoints =
                         {
                             point1,
                             point2,
                             point3,
                         };
                myGraphics.DrawPolygon(myPen, curvePoints);
               break;
             case 5: // case Filled Circle is selected
               myGraphics.FillEllipse( mySolidBrush, 50, 50, 150, 150 );
               break;
            case 6: // case Filled Rectangle is selected
               myGraphics.FillRectangle( mySolidBrush, 50, 50, 150, 
                  150 );
               break;
            case 7: // case Filled Ellipse is selected
               myGraphics.FillEllipse( mySolidBrush, 50, 85, 150, 115 );
               break;
            case 8: // case Filled Pie is selected
               myGraphics.FillPie( mySolidBrush, 50, 50, 150, 150, 0, 
                  45 );
               break;
             case 9:
                  Point point4 = new Point(150,  50);
                Point point5 = new Point(100,  150);
                Point point6 = new Point(200,   150);
                Point[] curvePoints2 =
                         {
                             point4,
                             point5,
                             point6,
                         };
               myGraphics.FillPolygon(mySolidBrush, curvePoints2);
               break;
         } // end switch
         myGraphics.Dispose(); // release the Graphics object
      }
      private void ComboBoxTestForm_Load(object sender, EventArgs e)
      {
      }
      private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
          switch (colorComboBox.SelectedIndex)
          {
              case 0:
                  myPen = new Pen(Color.Black);
                   mySolidBrush = new SolidBrush(Color.Black);
                  break;
          }
      }
      // end method imageComboBox_SelectedIndexChanged
   } // end class ComboBoxTestForm
} 

我试着在虚空之外调用myPen,这样它就可以在控制颜色的第二个虚空中调用。Mysolidbrush表示形状的填充。在第二个空白处,我试着将我的钢笔称为黑色,但我没有看到任何变化。我想知道我应该做什么步骤,这样它才能显示出我想要的颜色。

如何从另一个组合框(C#,Winforms)更改形状的颜色

您可以将所有绘制逻辑放入表单的Paint事件中,然后在SelectedIndexChanged事件中,只需调用this.Invalidate();:

private void ComboBoxTestForm_Paint(object sender, PaintEventArgs e)
{
    //detect color based on selected index of colorComboBox
    //Create your brush and your pen
    //detect shape based on selected index of imageComboBox
    //draw shares using e.Graphics.DrawXXXX and e.Graphics.FillXXXX

   /*Suppose Color.Redis detected from selected index*/
    var myColor= Color.Red; 
    using ( var myPen = new Pen(myColor))
    {
        /*Suppose drawing ellipse is detected from selected index*/
        e.Graphics.DrawEllipse( myPen, 50, 50, 150, 150 );
    }
}
private void imageComboBox_SelectedIndexChanged(object sender, EventArgs e )
{
    //Makes the form repaint
    this.Invalidate();
}
private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    //Makes the form repaint 
    this.Invalidate();        
}

此外,在当前代码中,虽然已将myPenmySolidBrush定义为类成员,但从未对它们进行初始化。在colorComboBox_SelectedIndexChanged中,您分配的是局部变量,而不是类成员。

myPen = new Pen(Color.Black);
mySolidBrush = new SolidBrush(Color.Black);

此外,在当前代码中,您已将所有绘制逻辑置于imageComboBox_SelectedIndexChanged中,即使在colorComboBox_SelectedIndexChanged中更正代码并将值指定给类成员myPenmySolidBrush,图形的颜色也将仅针对下一个形状而更改。