如何在单词搜索游戏中选择多个矩形?

时间:2013-07-11 19:30:48

标签: c# winforms mouseevent

首先,这是一张图片。 wordsearch

如果按下鼠标左键,如何选择多个矩形?

例如,我想用“man”字写,所以我点击m矩形,将其移动到a,在n处释放,然后返回带有“man”字样的字符串。< / p>

我创建了一个Cube类,它存储一个Rectangle和一个字符串,并且有一个Drawing方法,它需要一些参数:一个Graphic对象,一个Pen和一个Rectangle。我通过以下方式将它们绘制到面板中:

Cube[,] kockak = new Cube[3,3];
//fill cubes with data...

private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;            

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    kockak[j,i].Draw(g, new Pen(Brushes.Black), new Rectangle(i * 100, j * 100, 100, 100));
                }
            }
            Invalidate();
        }

2 个答案:

答案 0 :(得分:1)

只需添加一些方法来选择Cube类中的多维数据集,如下所示:

public class Cube : Panel {
   public Cube(){
      Selected = false;
   }
   protected override void OnPaint(PaintEventArgs e){
      //Draw your background to make it look like selected first before drawing string on top.
      if(Selected) e.Graphics.FillRectangle(Brushes.Green, ClientRectangle);
      //Draw your string normally as you did here
      //.......
   }
   public bool Selected { get; set;}
   public void Select(){
      Selected = true;
      Invalidate();
   }
   public void Deselect(){
      Selected = false;
      Invalidate();
   }
}
//use the code
yourCube.Select();//select a cube
yourCube.Deselect();//deselect a cube

答案 1 :(得分:0)

King King的一些额外想法回答:

我会在表单中添加MouseMove事件并检查MouseButton状态 如果是Left,请将Cude.selceted设置为true。

private void TreeViewTeschd_MouseMove(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (kockak[j,i].rect.Contains(e.Location)) kockak[j,i].selected = true;
            }
        }
    }
}