【问题标题】:Change color in paint改变油漆的颜色
【发布时间】:2016-11-17 12:47:54
【问题描述】:

我编写了一个程序,允许用户在表单中用笔绘画。 但有一个问题。

我只能在表单中设置 2 种颜色,例如我将左侧按钮设置为黑色,右侧按钮设置为红色。

我需要的只是如何将此代码更改为用户可以选择自己的颜色的代码。

我尝试了不同的方法,比如颜色对话,但我做不到。

我的代码:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Graphics graphic = this.CreateGraphics();
        graphic.DrawLine(Pens.Black, e.X, e.Y, e.X + 1, e.Y + 1);
    }

    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Graphics graphic = this.CreateGraphics();
        graphic.DrawLine(Pens.Red, e.X, e.Y, e.X + 1, e.Y + 1);
    }
}

【问题讨论】:

  • CreateGraphics 是做什么的?如果您没有绘制到屏幕外缓冲区,那么当窗口的客户区无效时,用户的图稿将丢失。
  • @Dai 我知道我只是想将此代码更改为用户可以选择自己颜色的代码。当我开始项目时,这段代码正是我想要的。只是笔的颜色...
  • Winforms 图形基本规则#1:永远不要使用control.CreateGraphics!永远不要尝试缓存 Graphics 对象!要么使用Graphics g = Graphics.FromImage(bmp) 绘制到Bitmap bmp 中,要么使用e.Graphics 参数在控件的Paint 事件中绘制。系统有时需要绘制所有控件的表面,而您有时无法控制;因此,您要添加到这些表面的所有内容都必须从系统将调用的一个事件创建,即Paint 事件。
  • 它只是似乎起作用。 Askan 已经回答了您的问题,但它似乎仍然有效。用 ColorDialog 设置这两种颜色,试一试,享受,最小化,最大化然后扔掉它
  • @TaW 我相信我的答案中有解决图形问题的方法

标签: c# winforms


【解决方案1】:

使用一些对话框为鼠标左键和右键选择颜色并将其存储在类级别变量中,即

if (_leftPen != null) { _leftPen.Dispose(); }
_leftPen = new Pen(selectedColour, 1f);

注意1fPen的厚度,这个可以根据你的要求改变。

然后在你的绘图方法中使用_leftPen。然后只需对鼠标右键应用类似的逻辑,即_rightPen。然后你有:

private Pen _leftPen = Pens.Black;
private Pen _rightPen = Pens.Red;

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Graphics graphic = this.CreateGraphics();
        graphic.DrawLine(_leftPen, e.X, e.Y, e.X + 1, e.Y + 1);
    }

    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Graphics graphic = this.CreateGraphics();
        graphic.DrawLine(_rightPen, e.X, e.Y, e.X + 1, e.Y + 1);
    }
}

您需要做的就是找到一种方法让用户选择自己的颜色。

还要注意@Taw 的评论:

Winforms 图形基本规则#1:永远不要使用 control.CreateGraphics!永远不要尝试缓存 Graphics 对象!使用 Graphics g = Graphics.FromImage(bmp) 或在控件的 Paint 事件中使用 e.Graphics 参数绘制到位图 bmp 中。系统有时需要绘制所有控件的表面控制;因此,您要添加到这些表面的所有内容都必须从系统将调用的一个事件创建,即 Paint 事件。

您应该在Paint 事件中使用您的代码,在MouseMove 事件中您应该存储要绘制的线条的位置,然后稍后更新。

private Pen _leftPen = Pens.Black;
private Pen _rightPen = Pens.Red;

private List<Point> _leftPoints = new List<Point>();
private List<Point> _rightPoints = new List<Point>();

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        _leftPoints.Add(new Point(e.X, e.Y));
    }

    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        _rightPoints.Add(new Point(e.X, e.Y));
    }

    this.Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    foreach (Point point in _leftPoints)
    {
        e.Graphics.DrawLine(_leftPen, point.X, point.Y, point.X + 1, point.Y + 1);
    }

    //Similar code for _rightPoints here
}

注意对Invalidate 的调用会强制表单重新绘制自身。如果适用,您可以改用 this.Refresh()this.Update()

【讨论】:

    【解决方案2】:
    Color BackColor = Color.Black;
    Color ForeColor = Color.Red;
    

    然后获取用户颜色并设置BackcolorForecolor

          private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Graphics graphic = this.CreateGraphics();
                graphic.DrawLine(new Pen(ForeColor), e.X, e.Y, e.X + 1, e.Y + 1);
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Graphics graphic = this.CreateGraphics();
                graphic.DrawLine(new Pen(BackColor), e.X, e.Y, e.X + 1, e.Y + 1);
            }
        }
    

    【讨论】:

      【解决方案3】:

      你可以这样使用:

      using System;
      using System.Drawing;
      using System.Windows.Forms;
      
      public partial class Form1 : Form
      {
          private readonly Graphics graphics;
      
          public Form1()
          {
              InitializeComponent();
      
              this.graphics = this.CreateGraphics();
      
              this.Load += (s, e) =>
              {
                  foreach (var color in Enum.GetValues(typeof(KnownColor)))
                      this.UserColors.Items.Add(color);
              };
          }
      
          /// <summary>
          /// Painting (left button use changed color, right-white to erase)
          /// </summary>
          private void Form1_MouseMove(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
                  this.graphics.DrawLine(
                      new Pen(
                          Color.FromKnownColor(
                              (KnownColor)Enum.Parse(typeof(KnownColor),
                              this.UserColors.SelectedItem.ToString()))),
                      e.X,
                      e.Y,
                      e.X + 1,
                      e.Y + 1);
      
              if (e.Button == MouseButtons.Right)
                  this.graphics.DrawLine(
                      Pens.White,
                      e.X,
                      e.Y,
                      e.X + 1,
                      e.Y + 1);
          }
      }
      

      this.UserColors 是主窗口上的 ComboBox。

      【讨论】:

      • 老实说,我不知道你的答案是什么
      • 怎么了?我给了他一个带有颜色的好代码示例,可以从用户中选择。
      • 这似乎是一种非常尴尬和迂回的方式来做简单的事情,另请参阅我的回答和 Taw 的评论,了解为什么你不应该使用 CreateGraphics 并且应该进行绘图在您的Paint 活动中工作
      • 这不适用于给定的问题。他使用 CreateGraphics,为什么不呢?他想知道如何自定义笔的颜色,我给了他答案。
      • 查看 Taw 的 cmets 了解原因
      猜你喜欢
      • 2020-11-08
      • 2012-06-27
      • 2013-12-19
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      相关资源
      最近更新 更多