C#五子棋开发问题:下棋不见棋子,但是把窗体拖出屏幕外又拖回来,来回几次,棋子有在棋盘上(刚才鼠标点击的地方)。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Gobang
{
public partial class GobangFrm : Form
{
int[,] arr = new int[15, 15];
private PictureBox[,] GobangpictureBox = new PictureBox[15, 15];
public GobangFrm()
{
InitializeComponent();
GobangBox.Paint += new PaintEventHandler(GobangBox_Paint);
GobangBox.MouseClick += new MouseEventHandler(GobangBox_MouseClick);
}
private void GobangBox_Paint(object sender, PaintEventArgs e)//画棋盘和重绘棋子
{
Graphics paper = e.Graphics;
Pen pen = new Pen(Color.Black, 2);
SolidBrush Brush = new SolidBrush(Color.White);
for (int l = 0; l < 600; l += 40)
{
paper.DrawLine(pen, 20, l + 20, 580, l + 20);//画横线
paper.DrawLine(pen, 20 + l, 20, l + 20, 580);//画竖线
}
paper.FillEllipse(Brush, 297, 297, 5, 5);
//下面是重绘代码
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
{
if (arr[i, j] == 1)
{
int m = i * 40 + 4;
int n = j * 40 + 4;
Graphics Repaper=e.Graphics;
Repaper = GobangBox.CreateGraphics();
Image pic = Image.FromFile("white.gif");
Repaper.DrawImage(pic, m, n, 30, 30);
}
}
}
private void GobangBox_MouseClick(object sender, MouseEventArgs e)//下棋
{
if (e.Button == MouseButtons.Left)//判断是否点的是鼠标左键
{
double h = (e.X) / 40;
double t = (e.Y) / 40;
int i = (int)Math.Floor(h);
int j = (int)Math.Floor(t);
int m = i * 40 + 4;
int n = j * 40 + 4;
GobangpictureBox[i,j] = new PictureBox();
GobangpictureBox[i, j].Image = global::Gobang.Properties.Resources.white;
arr[i, j] = 1;
} }
}
}想要解决的问题:1。当鼠标点击时把棋子显示出来、2。当把窗体最小化还原后棋子不消失或者把窗体拖出屏幕外在托回来后棋子不消失。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Gobang
{
public partial class GobangFrm : Form
{
int[,] arr = new int[15, 15];
private PictureBox[,] GobangpictureBox = new PictureBox[15, 15];
public GobangFrm()
{
InitializeComponent();
GobangBox.Paint += new PaintEventHandler(GobangBox_Paint);
GobangBox.MouseClick += new MouseEventHandler(GobangBox_MouseClick);
}
private void GobangBox_Paint(object sender, PaintEventArgs e)//画棋盘和重绘棋子
{
Graphics paper = e.Graphics;
Pen pen = new Pen(Color.Black, 2);
SolidBrush Brush = new SolidBrush(Color.White);
for (int l = 0; l < 600; l += 40)
{
paper.DrawLine(pen, 20, l + 20, 580, l + 20);//画横线
paper.DrawLine(pen, 20 + l, 20, l + 20, 580);//画竖线
}
paper.FillEllipse(Brush, 297, 297, 5, 5);
//下面是重绘代码
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
{
if (arr[i, j] == 1)
{
int m = i * 40 + 4;
int n = j * 40 + 4;
Graphics Repaper=e.Graphics;
Repaper = GobangBox.CreateGraphics();
Image pic = Image.FromFile("white.gif");
Repaper.DrawImage(pic, m, n, 30, 30);
}
}
}
private void GobangBox_MouseClick(object sender, MouseEventArgs e)//下棋
{
if (e.Button == MouseButtons.Left)//判断是否点的是鼠标左键
{
double h = (e.X) / 40;
double t = (e.Y) / 40;
int i = (int)Math.Floor(h);
int j = (int)Math.Floor(t);
int m = i * 40 + 4;
int n = j * 40 + 4;
GobangpictureBox[i,j] = new PictureBox();
GobangpictureBox[i, j].Image = global::Gobang.Properties.Resources.white;
arr[i, j] = 1;
} }
}
}想要解决的问题:1。当鼠标点击时把棋子显示出来、2。当把窗体最小化还原后棋子不消失或者把窗体拖出屏幕外在托回来后棋子不消失。
解决方案 »
- 求教C#写COM组件给VC调用时的事件响应问题!
- 升双星散分,搭车提问。
- 怎样屏蔽菜单的单击事件?内详.
- 各位高人请赐教,关于C#调用MSN API函数问题!!!
- 头大,头大。。。头真的很大(牛哥进来啊)
- winform中的A窗体中从库中读张照片(单击按钮显示),然后在B窗体中显示它的放大照片,请教照片是怎么传递的.
- “CopyTo”方法没有采用“2”个参数的重载——怎么改正?
- 用C#根据用户名和密码获取我邮箱里面的好友
- C#编程求帮助
- windowform中怎么获得DataGrid中的第五行第四列的值?并且这个单元没有被选中(用C#编程)
- 怎么修改继承窗体中的DataGridView控件的属性
- 为什么数据会自动清除?
{
paper.DrawImage(imgBlackStone, m * 40, n * 40, imgBlackStone.Width, imgBlackStone.Height);
}
else
{
paper.DrawImage(imgWhiteStone, m * 40, n * 40, imgWhiteStone.Width, imgWhiteStone.Height);
}楼主可以参考参考这,,
http://www.cnblogs.com/esshs/archive/2005/04/01/129824.html
{
paper.DrawImage(imgBlackStone, m * 40, n * 40, imgBlackStone.Width, imgBlackStone.Height);
}
else
{
paper.DrawImage(imgWhiteStone, m * 40, n * 40, imgWhiteStone.Width, imgWhiteStone.Height);
}楼主可以参考参考这,,
http://www.cnblogs.com/esshs/archive/2005/04/01/129824.html
http://download.csdn.net/detail/linxi729778865/3056350
内附下载地址http://files.cnblogs.com/esshs/FiveStones(v1.0.0).rar
下面加上GobangBox.Refresh();