2016-11-17 218 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