簡體   English   中英

顯示子控件時C#WinForms按鍵事件不起作用

[英]C# WinForms key events not working when showing child control

我有一個名為“ MainView”的表單,其中包含一個名為“ GameState”的UserControl(我使用狀態機)。 在“ GameState” UserControl中,我繪制了游戲世界。 我在“ GameState”中還有一個KeyUp事件,當按下Escape鍵時,它將在“ GameState” UserControl(InGameMenu.visible = true)中顯示UserControl“ InGameMenu”。

當我按Escape鍵時,顯示菜單。 如果再次按Escape鍵,菜單將再次隱藏。 單擊菜單后,我無法從“ GameState”和“ InGameMenu”再次獲得按鍵事件。 首先,我認為這是一個簡單的焦點問題,因此我嘗試單擊“ GameState”和“ InGameMenu”以檢查是否使關鍵事件起作用,但是沒有成功。 我還嘗試將鍵事件從“ GameState”添加到“ InGameMenu”事件處理程序中,或在“ InGameMenu”中處理鍵事件,但這都不起作用(均未觸發)。 我在“ InGameMenu”上有一個關閉按鈕。 當我單擊該菜單時,菜單將隱藏,並且來自“ GameState”的按鍵事件再次起作用。

為什么當“ InGameMenu”打開時,“ GameState”和“ InGameMenu”的關鍵事件沒有觸發?

提前致謝。

游戲狀態碼

public partial class GameState : UserControl, IState<MainView>
{
    private bool ready = false;
    private Font drawFont = new Font("Arial", 16);
    private SolidBrush drawBrush = new SolidBrush(Color.Black);

    private InGameMenu menu;

    public GameState()
    {
        InitializeComponent();
        this.menu = new InGameMenu();
        this.menu.Location = new Point(this.Width / 2 - menu.Width / 2, this.Height / 2 - menu.Height / 2);
        this.menu.Visible = false;
        this.addControl(this.menu);      
    }

    public void addControl(Control control)
    {
        control.KeyUp += this.GameState_KeyUp;
        this.Controls.Add(control);
    }

    public void enter(MainView owner)
    {
        this.Width = owner.Width;
        this.Height = owner.Height;
        this.Location = new Point(0, 0);
        owner.Controls.Add(this);
        World.getInstance().createWorld();
        this.Focus();    

        this.ready = true;
    }

    public void update(MainView owner)
    {
        //game stuff
        if (this.menu.Visible == false)
        {
            //game stuff
        }
        else
        {
            switch (this.menu.action)
            {
                case action.stop:
                    owner.stateMachine.setCurrentState(new MainMenuState());
                    break;
                case action.close:
                    this.menu.Visible = false;
                    this.Focus();
                    break;
                default:
                    break;
            }
            this.menu.action = action.nothing;
        }      

    }

    private void GameState_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            if (this.menu.Visible == false)
            {
                this.menu.Visible = true;
            }
            else
            {
                this.menu.Visible = true;
            }
        }
    }

}

InGameMenu代碼

namespace LordsAndLands.States.GameStateStuff
{
public enum action
{
    nothing,
    stop,
    close
}

public partial class InGameMenu : UserControl
{
    public action action = action.nothing;

    public InGameMenu()
    {
        InitializeComponent();
    }

    private void buttonExit_Click(object sender, EventArgs e)
    {   
        Environment.Exit(0);
    }

    private void buttonExitToMainMenu_Click(object sender, EventArgs e)
    {
        this.action = action.stop;
    }

    private void buttonClose_Click(object sender, EventArgs e)
    {
        this.action = action.close;
    }

}
}

您可以使用Form.KeyPreview=true; 窗體的屬性,並檢查窗體事件中的哪個鍵被按下以及其他控件應該做什么。

KeyPreview屬性:

獲取或設置一個值,該值指示在事件傳遞給具有焦點的控件之前,表單是否將接收鍵事件。

有關MSDN的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM