【问题标题】:How to highlight a specific area on a grid of panels如何突出显示面板网格上的特定区域
【发布时间】:2013-04-13 13:04:24
【问题描述】:

制作一个面板填充表单,我希望一个角色(由一个图片框表示)在它上面移动。当我单击角色的图片框时,我希望突出显示一个表示该角色可以移动多远的区域。

下面的图片是我到目前为止所管理的,但这并不是我想要的。红色边框代表图片框,橙色矩形代表高亮区域。每个黑色边框的矩形都是一个面板。

一个角色移动到对角线上的面板应该花费 2 次移动,这样如果一个角色有 2 次移动可用,那么当点击图片框时下面的区域应该被突出显示:

我理解为什么我的代码会突出显示一个正方形而不是我想要的区域,但我不知道如何解决它。任何帮助,将不胜感激;下面是我写的代码。

        foreach (Panel pan in grid)
        {
            if (pan.Left <= (selectedCharacter.PictureBox.Left + (selectedCharacter.Movement * 80)) 
                && pan.Left >= (selectedCharacter.PictureBox.Left - (selectedCharacter.Movement * 80)))
            {
                if (pan.Top <= (selectedCharacter.PictureBox.Top + (selectedCharacter.Movement * 100)) 
                    && pan.Top >= (selectedCharacter.PictureBox.Top - (selectedCharacter.Movement * 100)))
                {
                        pan.BackColor = selectedCharacter.PlayerHighlight;
                }
            }
        }

如果我不够清楚,请随时提出问题

【问题讨论】:

    标签: c# foreach panel picturebox


    【解决方案1】:

    这是我为解决此问题而编写的代码。如果您能看到解决此问题的更简单/不同的方法,请随时发表评论。

    Panel[] highlightedMovement = new Panel[1]; //creates an array of panels to be highlighted
    highlightedMovement[0] = characterBeingHighlighted.CurrentPanel; //adds the panel the character is currently on to the array
    
    int z = 1;
    
    //highlights panels adjecent to those already highlighted for as many iterations as the character's movement stat
    for (int i = 0; i < characterBeingHighlighted.Movement; i++)
    {
        foreach (Panel highlightPanel in highlightedMovement)
        {
            //goes through all panels on the grid and adds them to the array if they are adjacent to any already in the array.
            foreach (Panel gridPanel in grid)
            {
                //checks if the panel is adjacent to any already in the array
                if ((gridPanel.Top == (highlightPanel.Top + 100) && gridPanel.Left == highlightPanel.Left) ||
                    (gridPanel.Top == (highlightPanel.Top - 100) && gridPanel.Left == highlightPanel.Left) ||
                    (gridPanel.Left == (highlightPanel.Left + 80) && gridPanel.Top == highlightPanel.Top) ||
                    (gridPanel.Left == (highlightPanel.Left - 80) && gridPanel.Top == highlightPanel.Top))
                {
                    //adds it to the array but only if it isn't already in the array
                    if (!highlightedMovement.Contains(gridPanel))
                    {
                        Array.Resize(ref highlightedMovement, highlightedMovement.Length + 1);
                        highlightedMovement[z] = gridPanel;
                        z++;
                    }
                }
            }
        }
    }
    
    //highlights all the panels in the array
    foreach (Panel panel in highlightedMovement)
    {
        panel.BackColor = characterBeingHighlighted.PlayerHighlight;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2013-12-18
      相关资源
      最近更新 更多