簡體   English   中英

自定義UserControl上的KeyDown和KeyUp事件

[英]KeyDown and KeyUp events on a custom UserControl

我有一個UserControl ,它具有一些其他控件,然后還有一個Panel ,用於在鼠標事件上繪制自定義形狀。 我想添加一些其他功能,但是為此,我需要在鼠標在面板上繪制形狀時檢測KeyDownKeyUp (在這種情況下, UserControl.KeyDown/KeyUp永遠不會觸發)。 如何實現呢?

編輯

這是可以看到問題的UserControl的最低版本:

public partial class UserControl1 : UserControl
{
    TextBox textBox1;
    Panel panel1;
    public UserControl1()
    {
        InitializeComponent();

        textBox1 = new System.Windows.Forms.TextBox();
        textBox1.Location = new System.Drawing.Point(0, 0);
        this.Controls.Add(this.textBox1);

        panel1 = new System.Windows.Forms.Panel();
        panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        panel1.Location = new System.Drawing.Point(0, 25);
        panel1.Size = new System.Drawing.Size(300, 150);
        panel1.MouseEnter += new System.EventHandler(this.panel1_MouseEnter);
        this.Controls.Add(this.panel1);

        this.Size = new System.Drawing.Size(300, 200);
        this.KeyDown += UserControl1_KeyDown;
    }

    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        panel1.Focus();
    }

    void UserControl1_KeyDown(object sender, KeyEventArgs e)
    {
        MessageBox.Show("Key down");
    }

}

當此控件位於窗體內時,將永遠不會顯示“按下按鍵”消息。

我在這里從Hans Passant 那里獲取了代碼(很好的提示,非常感謝!),並根據自己的需要對其進行了修改,如下所示:

class SelectablePanel : Panel
{
    public SelectablePanel()
    {
        this.SetStyle(ControlStyles.Selectable, true);
        this.TabStop = true;
    }
    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Up || keyData == Keys.Down) return true;
        if (keyData == Keys.Left || keyData == Keys.Right) return true;
        return base.IsInputKey(keyData);
    }
    protected override void OnKeyDown(KeyEventArgs e)
    {
        var handler = KeyDown;
        if (handler != null) handler(this, e);
    }
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        var handler = KeyPress;
        if (handler != null) handler(this, e);
    }
    protected override void OnKeyUp(KeyEventArgs e)
    {
        var handler = KeyUp;
        if (handler != null) handler(this, e);
    }
    protected override void OnMouseEnter(EventArgs e)
    {
        this.Focus();
        base.OnMouseEnter(e);
    }
    protected override void OnEnter(EventArgs e)
    {
        this.Invalidate();
        base.OnEnter(e);
    }
    protected override void OnLeave(EventArgs e)
    {
        this.Invalidate();
        base.OnLeave(e);
    }
    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        if (this.Focused)
        {
            var rc = this.ClientRectangle;
            rc.Inflate(-2, -2);
            ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
        }
    }

    [Browsable(true)]
    public new event EventHandler<KeyEventArgs> KeyDown;
    [Browsable(true)]
    public new event EventHandler<KeyEventArgs> KeyUp;
    [Browsable(true)]
    public new event EventHandler<KeyPressEventArgs> KeyPress;
}

最后,我修改了原始代碼,如下所示:

public partial class UserControl1 : UserControl
{
    TextBox textBox1;
    SelectablePanel selectablePanel;
    public UserControl1()
    {
        InitializeComponent();

        textBox1 = new System.Windows.Forms.TextBox();
        textBox1.Location = new System.Drawing.Point(0, 0);
        this.Controls.Add(this.textBox1);

        selectablePanel = new SelectablePanel();
        selectablePanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        selectablePanel.Location = new System.Drawing.Point(0, 25);
        selectablePanel.Size = new System.Drawing.Size(300, 150);
        selectablePanel.KeyDown += panel1_KeyDown;
        this.Controls.Add(this.selectablePanel);

        this.Size = new System.Drawing.Size(300, 200);
    }

    void panel1_KeyDown(object sender, KeyEventArgs e)
    {
        MessageBox.Show("Key down");
    }
}

現在它可以滿足我的需求。

暫無
暫無

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

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