C#窗体中的图片绘制网格问题
1个回答
展开全部
这么写就行了
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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询