簡體   English   中英

單擊鼠標后如何用顏色填充單元格?

[英]How can I fill a cell with color after mouse click?

我有個問題。 我有一個帶有2行和2列的tableLayoutPanel。 第一列是空的,第二列是空的。 我知道檢測到單擊的單元格,但是我不知道如何用紅色科洛爾填充此單擊的單元格。 有什么建議嗎? 這是我的代碼:

private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
    {
        int row = 0;
        int verticalOffset = 0;
        foreach (int h in tableLayoutPanel1.GetRowHeights())
        {
            int column = 0;
            int horizontalOffset = 0;
            foreach (int w in tableLayoutPanel1.GetColumnWidths())
            {
                Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
                if (rectangle.Contains(e.Location))
                {
                    MessageBox.Show(String.Format("row {0}, column {1} was clicked", row, column));

                    return;
                }
                horizontalOffset += w;
                column++;
            }
            verticalOffset += h;
            row++;
        }
    }

使用List<T>跟蹤已單擊的單元格:

   List<Point> clickedCells = new List<Point>();

..您可以將找到的一個添加到列表中:

    if (rectangle.Contains(e.Location))
    {
        // *
        Point cell = new Point(column, row);
        if (!clickedCells.Contains(cell)) clickedCells.Add(cell);
        // **
        tableLayoutPanel1.Invalidate();
        return;
    }

要在再次單擊時關閉顏色然后重新打開,可以在此處添加一個else子句(**):

         else clickedCells.Remove(cell);

如果只繪制一個單元格,則僅使用一個點或僅清除此處的列表(*)

        clickedCells.Clear();

使TLP Invalidating將觸發CellPaint事件,在該事件中處理列表:

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if (clickedCells.Contains(new Point(e.Column,e.Row)))
        e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
}

請注意,我使用的Points不是像素位置,而是像元坐標!

因此,下面的代碼可以工作,但是我不是特別喜歡它。特別是,我猜測還有一種更為優美的方法來觸發paint事件,並將參數傳遞給該事件。 有一些基於CellPaint事件的帖子,但我不知道如何立即觸發它。 所以..

public partial class Form1 : Form
{
    Rectangle clicked = new Rectangle();
    bool wasClick = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
    {
        int row = 0;
        int verticalOffset = 0;
        int clickx = e.X;
        int clicky = e.Y;
        foreach (int h in tableLayoutPanel1.GetRowHeights())
        {
            int column = 0;
            int horizontalOffset = 0;
            foreach (int w in tableLayoutPanel1.GetColumnWidths())
            {
                Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
                if (rectangle.Contains(e.Location))
                {
                    MessageBox.Show(String.Format("row {0}, column {1} was clicked", row, column));
                    clicked = rectangle;
                    wasClick = true;
                    tableLayoutPanel1.Invalidate(rectangle);
                    return;
                }
                horizontalOffset += w;
                column++;
            }
            verticalOffset += h;
            row++;
        }
    }

    private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
    {
        if (wasClick)
        {
            Graphics g = e.Graphics;
            Rectangle r = clicked;
            g.FillRectangle(Brushes.Red, r);
            wasClick = false;
        }

    }
}

需要注意的是,當您單擊一個紅色單元格時,它會保持紅色,因此您需要添加關於哪些單元格是紅色/不是紅色的檢測。 同樣,其他人可能會比這有更好的解決方案,因此請注意。

暫無
暫無

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

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