#include <cstdlib>
#include <malloc.h>
#include <iostream>
#define BUFSIZE 20
using namespace std;
int main(int argv, char** argc)
{
char * buf = static_cast<char *> (malloc(BUFSIZE));
size_t bufsize = malloc_usable_size(buf);
cout << "malloc_usable_size (buf) = " << bufsize << endl;
return 0;
}
答案 0 :(得分:1)
使用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);
如果只绘制一个单元格,则只使用一个Point或只清除此处的列表(*)
clickedCells.Clear();
Invalidating
TLP将触发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
不是像素位置,而是单元格坐标!
答案 1 :(得分:1)
所以......下面的代码会起作用,但我并不特别喜欢它......具体来说,我猜测有一种更优雅的方法可以触发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;
}
}
}
注意事项是,当您单击红色单元格时,它会保持红色,因此您需要添加关于哪些单元格为红色/不是红色的检测。同样,其他人可能会有一个比这更优雅的解决方案,所以请留意。