如何使用方向键盘移动面板

时间:2013-02-21 20:12:39

标签: c# winforms controls

在面板控件上的标签停靠中,我使用了以下代码: (这是用鼠标指针移动我的面板)

Point m_pntPosPanel = new Point();

private void lblMove_MouseUp(object sender, MouseEventArgs e)
{
    pnlCost.Location = new Point(e.X - m_pntPosPanel.X + pnlCosto.Location.X, e.Y - m_pntPosPanel.Y + pnlCosto.Location.Y);
    Cursor.Current = Cursors.Default;
    m_blnMouseDown = false;
}

private void lblMove_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        m_pntPosPanel.X = e.X;
        m_pntPosPanel.Y = e.Y;

        Cursor.Current = Cursors.Hand;
        m_blnMouseDown = true;
    }
}

如何使用directional keyboard移动该面板?

1 个答案:

答案 0 :(得分:1)

我将在控件和进程上注册KeyUp事件,如下所示:

    private void lblMove_KeyUp(object server, KeyEventArgs e)
    {
          Point location = button1.Location;
          switch(e.KeyCode)
          {
            case Keys.Up: 
                location.Y = location.Y -1;
                break;
            case Keys.Down:
                location.Y = location.Y + 1;
                break;
            case Keys.Right:
                location.X = location.X + 1;
                break;
            case Keys.Left:
                location.X = location.X - 1;
                break;
          }

          button1.Location = location;
    }