关于gdi

.Net技术 码拜 9年前 (2016-03-04) 933次浏览
关于gdi
private void FormDefind_96_Load(object sender, EventArgs e)
{
int d = 50;//每个方格子长度
int x = 44;//第一个格子的左上角x
int y = 66;//第一个格子的左上角y
isred = new int[96];
rects = new Rectangle[96];
for (var i = 0; i < 12; i++)
{
for (var j = 0; j < 8; j++)
{
rects[j * 12 + i] = new Rectangle(x + i * d, y + j * d, d – 2, d – 2);
isred[j * 12 + i] = 0;
}
}
}
加载96个 Rectangle并且实现了点击单元格变色
private void FormDefind_96_MouseClick_1(object sender, MouseEventArgs e)
{
Point p = PointToClient(MousePosition);
int num = ((p.X – 44) / 50) + ((p.Y – 66) / 50) * 12;
textBox1.Text = p.ToString();
if (44 < p.X & p.X < 644 & 66 < p.Y & p.Y < 466)//&&具有短路功能
{
if (isred[num] == 0)
{
FillByColor(rects[num], Color.Red, this.CreateGraphics());
isred[num] = 1;
}
else
{
FillByColor(rects[num], Color.White, this.CreateGraphics());
isred[num] = 0;
}
}
}
但是鼠标移动到button上面的时候,红色的格子全部消失了,问题出在哪了?求指导
解决方案

30

让格子变色的动作要在 paint 事件中进行,点击格子只是保存一下被点格子的编号

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于gdi
喜欢 (0)
[1034331897@qq.com]
分享 (0)