CustomControl 棋盘上突出显示的方块不会在初始 MouseDown 事件之后持续存在

发布于 2024-07-12 02:40:32 字数 1482 浏览 7 评论 0原文

我一直在用 C# 编写一个 Windows 应用程序国际象棋游戏,作为磨练技能的练习,而且还因为它很有趣。 我添加了允许玩家选择选项来突出显示棋子在被单击时可以合法移动到的方块的功能。 CustomControl 处理棋盘的渲染,并且还突出显示方块。

一切都按计划进行,直到玩家开始将棋子拖到新的方块上。 鼠标一移动,高光就会消失。 我怀疑引发了 Paint 事件并且板自行重绘。 由于高亮部分不是初始电路板布局的一部分,因此不会绘制它们。

我希望发生的是让方块保持突出显示,直到棋子掉落到其目标方块上。 有可能做到这一点吗? 任何建议将不胜感激。

伪代码:

    void piece_MouseDown(object sender, MouseEventArgs e)
    {
        Piece piece = (Piece)sender;

        legalSquares = CalculateLegalSquares(piece.CurrentSquare);

        if (legalSquares.Count > 0 && this.showLegalMoves)
        {
            chessBoard1.HighlightSquares(legalSquares);
        }

        // I believe a Paint event gets raised either here...
        piece.DoDragDrop(piece, DragDropEffects.Move);
    }

    void piece_DragEnter(object sender, DragEventArgs e)
    {
        // ...or here, that removes the highlights.
        if (e.Data.GetDataPresent("Chess.Piece"))
        {
            e.Effect = DragDropEffects.Move;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    void piece_DragDrop(object sender, DragEventArgs e)
    {
        Piece piece = (Piece)e.Data.GetData("Chess.Piece");

        if (piece.CurrentSquare != dropSquare)
        {
            if (legalSquares.Contains(dropSquare))
            {
                // This is where I’d like the highlights to stop

                // DoStuff()
            }
        }
    }

I've been coding a Windows app chess game in C# as an exercise in honing my skills, and also because it's fun. I have included functionality that allows a player to select the option to highlight the squares a piece can legally move to when it gets clicked. A CustomControl handles the rendering of the chessboard and it also highlights the squares.

It all works as planned until the player begins to drag the piece to a new square. The moment the mouse moves, the highlights go away. I suspect that a Paint event is raised and the board redraws itself. And since the highlights are not part of the initial board layout, they don't get drawn.

What I would like to happen is for the squares to remain highlighted until the piece is dropped on its destination square. Is it possible to accomplish this? Any suggestions will be appreciated.

Psuedo code:

    void piece_MouseDown(object sender, MouseEventArgs e)
    {
        Piece piece = (Piece)sender;

        legalSquares = CalculateLegalSquares(piece.CurrentSquare);

        if (legalSquares.Count > 0 && this.showLegalMoves)
        {
            chessBoard1.HighlightSquares(legalSquares);
        }

        // I believe a Paint event gets raised either here...
        piece.DoDragDrop(piece, DragDropEffects.Move);
    }

    void piece_DragEnter(object sender, DragEventArgs e)
    {
        // ...or here, that removes the highlights.
        if (e.Data.GetDataPresent("Chess.Piece"))
        {
            e.Effect = DragDropEffects.Move;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    void piece_DragDrop(object sender, DragEventArgs e)
    {
        Piece piece = (Piece)e.Data.GetData("Chess.Piece");

        if (piece.CurrentSquare != dropSquare)
        {
            if (legalSquares.Contains(dropSquare))
            {
                // This is where I’d like the highlights to stop

                // DoStuff()
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-07-19 02:40:32

听起来您是通过直接绘制来突出显示有效的方块,但这会在任何重新绘制时被擦除。 如果您的窗口由于其他原因而被重新绘制,例如最小化和恢复它,或者将另一个窗口拖到它上面,您可能会失去亮点。

如果是这种情况,您可能需要重写 OnPaint 方法并在那里进行突出显示。 当您想要更改突出显示的内容时,请在类中设置某些状态来控制 OnPaint 方法中突出显示的内容,然后使窗口无效。

It sounds like you are highlighting the valid squares by drawing directly, but this will get erased on any repaint. You will probably lose the highlights if your window is repainted for other reasons also, such as minimizing and restoring it, or dragging another window on top of it.

If this is the case, you probably need to override the OnPaint method and do your highlighting there. When you want to change what is highlighted, set some state in your class to control what is drawn as highlighted in the OnPaint method, and then Invalidate your window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文