简体   繁体   English

Windows Forms 带按钮的隐藏面板仍处于活动状态

[英]Windows Forms Hidden Panel with buttons is still active

First, I have to use windows forms I know it is bad for making games, but its for school.首先,我必须使用 windows forms 我知道它不适合制作游戏,但它适合学校。

I have designed a pause menu for my game using a panel with buttons on it.我使用带有按钮的面板为我的游戏设计了一个暂停菜单。 Here is the code for When I press escape to open the pause menu.这是当我按下退出键打开暂停菜单时的代码。

private void Game_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        if (paused == true)
        {
            paused = false;
            pnlPaused.Visible = false;
            tmrAnimations.Start();
            tmrGame.Start();
            tmrJump.Start();
            tmrPlayerMovement.Start();
            tmrLeftMovement.Start();
            tmrRightMovement.Start();
        }
        else
        {
            pnlPaused.BringToFront();
            pnlPaused.Visible = true;
            pnlPaused.BackColor = Color.FromArgb(80, 79, 249, 249);
            paused = true;
            tmrAnimations.Stop();
            tmrGame.Stop();
            tmrJump.Stop();
            tmrPlayerMovement.Stop();
            tmrLeftMovement.Stop();
            tmrRightMovement.Stop();
        }
    }
}

This code works fine until I click one of the buttons.在我单击其中一个按钮之前,此代码可以正常工作。 When any button is clicked, even the ones with no code, it is impossible to press escape to unpause again.当任何一个按钮被点击时,即使是没有代码的,也不可能再次按下escape来取消暂停。 With this resume button the timers all start and the game starts working again but the form doesn't register any key clicks.使用此恢复按钮,计时器全部启动,游戏重新开始工作,但表单未注册任何按键点击。

private void btnResume_Click(object sender, EventArgs e)
{
    paused = false;
    pnlPaused.Visible = false;
    tmrAnimations.Start();
    tmrGame.Start();
    tmrJump.Start();
    tmrPlayerMovement.Start();
    tmrLeftMovement.Start();
    tmrRightMovement.Start();
}

I have tried adding this code into the button click code我尝试将此代码添加到按钮单击代码中

pnlPaused.Enabled = false;

This code works in re-enabling user input but every time a key is pressed the windows error sound plays, the one when you have to dismiss an alert etc but try clicking off (hopefully that makes sense).此代码可用于重新启用用户输入,但每次按下某个键时都会播放 windows 错误声音,当您必须关闭警报等但尝试单击关闭时(希望这是有道理的)。 Does anyone know another way to fix the panel still being active after being hidden or preventing the windows sound?有谁知道在隐藏或阻止 windows 声音后修复面板仍然处于活动状态的另一种方法?

I have realized all I needed to do was unfocus the button with我已经意识到我需要做的就是用

this.Focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM