【问题标题】:How would I change the color of a shape from another ComboBox (C#,Winforms)如何从另一个 ComboBox 更改形状的颜色(C#,Winforms)
【发布时间】:2015-12-02 22:43:19
【问题描述】:

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

  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
} 

我试图做的是在 void 之外调用 myPen,因此可以在控制颜色的第二个 void 中调用它。 Mysolidbrush 代表形状的填充。在第二个空白中,我尝试将 myPen 称为 black ,但我没有看到任何变化。我想知道我应该采取哪些步骤才能显示我想要的颜色。

【问题讨论】:

  • 将所有绘制逻辑放入Paint事件中,然后在SelectedIndexChanged事件中,调用this.Invalidate();
  • 同样在当前代码中,虽然您已将 myPenmySolidBrush 定义为类成员,但您从未初始化它们。在colorComboBox_SelectedIndexChanged 中,您分配的是局部变量,而不是类成员。
  • 对不起,我对 GUI 很陌生,你能告诉我这是怎么做的吗?
  • 我分享了一些伪代码,供您按照您的要求描述评论。希望对您有所帮助 :) 不要忘记为事件分配处理程序。

标签: c# winforms combobox


【解决方案1】:

你可以把所有的绘制逻辑放到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,也就是您的绘图颜色只会改变下一个形状。

【讨论】:

  • 我对绘画事件不是很熟悉,所以有没有其他方法可以调用 myPen?我想要为第二个空白做的是让案例 0 识别 myPen,然后更改第一个空白的颜色。我只需要初始化 myPen 吗?
  • 如果你想学习绘画,你应该学会使用Paint事件。无论如何,你应该知道你已经把所有的绘制逻辑放在imageComboBox_SelectedIndexChanged 中,即使你更正了colorComboBox_SelectedIndexChanged 中的代码并将值分配给你的类成员myPenmySolidBrush,你的绘图颜色只会改变下一个形状。
  • 是否需要在 void 之外和第二个 void 中都对其进行初始化?
  • colorComboBox_SelectedIndexChanged中你不应该声明局部变量,你应该只分配值,就像我在答案中所做的那样。
  • 我确实做到了,但颜色没有改变
猜你喜欢
  • 2020-12-09
  • 2019-05-03
  • 2012-10-04
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 2017-09-03
相关资源
最近更新 更多