2016-11-17 219 views
1

我已经编写了一个程序,允许用户在窗体中用笔绘画。 但有一个问题。改变颜色的颜色

我可以只设置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); 
    } 
} 
+0

CreateGraphics是做什么的?如果您没有绘制到屏幕外缓冲区,那么当窗口的客户区域失效时,用户的图片将会丢失。 – Dai

+0

@戴我知道 我只是想改变这段代码,以代码,用户可以选择自己的颜色。 当我开始项目时,这段代码正是我想要的。只是笔的颜色... – VorTex318

+2

Winforms图形基本规则#1: 千万不要使用'control.CreateGraphics'!切勿尝试缓存'Graphics'对象!使用'Graphics g = Graphics.FromImage(bmp)'或者在控件的'Paint'事件中使用'e.Graphics'参数来绘制一个'Bitmap bmp'。系统需要绘制所有的控件'有时你无法控制的表面;因此所有你想要添加到这些表面的东西都必须从系统调用的一个事件中创建,这就是'Paint'事件。 – TaW

回答

1

使用一些对话框来选择左,右鼠标按钮的颜色,并存储在一个类级别的变量即

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

注意1f是厚度Pen,这可以改变,以满足您的要求。

然后在您的绘图方法中使用_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()

1
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); 
     } 
    } 
0

您可以使用这样的事情:

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是你的主窗口上组合框。

+0

我真的不知道你要回答这个问题 – TheLethalCoder

+0

这是怎么回事? 我给了他一个很好的颜色代码示例,可以从用户选择。 – EgoPingvina

+0

这似乎是一个非常尴尬和圆滑的方式来做简单的事情,也看到我的答案和Taw的评论,为什么你不应该使用'CreateGraphics'并且应该在你的'Paint'事件中进行绘图工作 – TheLethalCoder