C#窗体如何绘制网格图,并且每个方格里面都可以传值
答案:1 悬赏:30
解决时间 2021-03-08 15:38
- 提问者网友:护她一生,唯爱
- 2021-03-08 01:05
最佳答案
- 二级知识专家网友:万千宠爱
- 2021-03-08 01:12
这么写就行了
public Form1()
{
InitializeComponent();
bit = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(bit);
g.Clear(this.BackColor);
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 10, 10));
g.Dispose();
textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效减少窗体拉伸时的闪烁
}
TextureBrush textureBrush;
Bitmap bit;
int x = 0;
int y = 0;
bool showRec = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(textureBrush,this.ClientRectangle);
if (showRec)
{
e.Graphics.DrawRectangle(Pens.White, new Rectangle(x * 10, y * 10, 10, 10));
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)//鼠标右键
{
x = e.X / 10;
y = e.Y / 10;
showRec = true;
this.Invalidate();
MessageBox.Show("第" + x + "×" + y + "个矩形 " + "坐标为:(" + e.X + "," + e.Y + ")");
showRec = false;
this.Invalidate();
}
}
public Form1()
{
InitializeComponent();
bit = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(bit);
g.Clear(this.BackColor);
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 10, 10));
g.Dispose();
textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效减少窗体拉伸时的闪烁
}
TextureBrush textureBrush;
Bitmap bit;
int x = 0;
int y = 0;
bool showRec = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(textureBrush,this.ClientRectangle);
if (showRec)
{
e.Graphics.DrawRectangle(Pens.White, new Rectangle(x * 10, y * 10, 10, 10));
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)//鼠标右键
{
x = e.X / 10;
y = e.Y / 10;
showRec = true;
this.Invalidate();
MessageBox.Show("第" + x + "×" + y + "个矩形 " + "坐标为:(" + e.X + "," + e.Y + ")");
showRec = false;
this.Invalidate();
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯