簡體   English   中英

使用組合框更改畫筆顏色

[英]change brush color with combobox

我需要comboBox索引來更改選擇它時畫筆/筆的顏色,我知道它應該是一個簡單的解決方法,但是我似乎無法得到它。

public partial class Form1 : Form
{
    private bool penDown = false;
    private int radius = 5;

    private SolidBrush brush = new SolidBrush(Color.IndianRed);
    private Color[] colors = { Color.IndianRed, Color.Blue, Color.Green, Color.Yellow, Color.Purple};

    public Form1()
    {
        InitializeComponent();
    }

    private void btnClr_Click(object sender, EventArgs e)
    {
        Graphics g = panel1.CreateGraphics();
        g.Clear(panel1.BackColor);

        g.DrawImage(panel1.BackgroundImage, panel1.ClientRectangle, 0, 0, panel1.BackgroundImage.Width,
            panel1.BackgroundImage.Height, GraphicsUnit.Pixel);
        g.Dispose();
    }

    private void btnQuit_Click(object sender, EventArgs e)
    {
        base.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (!penDown)
        {
            return;
        }
        Graphics g = panel1.CreateGraphics();
        g.FillEllipse(brush, new Rectangle(e.X - radius, e.Y - radius, 2 * radius, 2 * radius));

        g.Dispose();
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        penDown = true;
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        penDown = false;
    }

    private void colorCB_SelectedIndexChanged(object sender, EventArgs e)
    {
        //int i = ((color
    }
}

只需使用組合框SelectedIndex屬性即可從colors數組中檢索colors

private void colorCB_SelectedIndexChanged(object sender, EventArgs e)
{
    brush = new SolidBrush(colors[colorCB.SelectedIndex]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM