0

我想要忽略光标键并且只能用鼠标控制的 ac# 拆分容器。我怎样才能做到这一点?这样我就可以在其中一个侧面板中使用键盘输入,而无需同时移动拆分。

4

2 回答 2

1

使用 e.Handled = true 或 e.SuppressKeyPress = true 来防止键调整拆分器的大小对我不起作用。

我可以通过在 KeyDown 上设置 IsSplitterFixed = true 和在 MouseDown/MouseMove 上设置 IsSplitterFixed = false 来做到这一点(允许通过鼠标调整大小)。

例如

    public Form1()
    {
        InitializeComponent();

        splitContainer1.MouseMove += splitContainer1_MouseMove;
        splitContainer1.KeyDown += splitContainer1_KeyDown;
        splitContainer1.MouseDown += splitContainer1_MouseDown;
    }

    void splitContainer1_MouseDown(object sender, MouseEventArgs e)
    {
        splitContainer1.IsSplitterFixed = false;
    }

    void splitContainer1_MouseMove(object sender, MouseEventArgs e)
    {
        splitContainer1.IsSplitterFixed = false;
    }

    void splitContainer1_KeyDown(object sender, KeyEventArgs e)
    {
        splitContainer1.IsSplitterFixed = true;
    }
于 2013-10-24T16:40:40.320 回答
0

您可以禁用处理KeyDown控件事件的键盘输入,如果需要,您可以在输入与特定键匹配时处理该事件。

例子

splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown

private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
  //  if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
  //  {

          e.Handled = true; //Handle the event
  //  }
}

此外,您可以根据收集到的事件取消控件的SplitterMoving事件来停止拆分器移动SplitContainerKeyCodeKeyDown

例子

Keys KeyCode; //This is the variable we will use to store the KeyCode gathered from the KeyDown event into. Then, check if it matches any of the arrow keys under SplitterMoving event canceling the movement if the result was true
splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown
splitContainer1.SplitterMoving += new SplitterCancelEventHandler(splitContainer1_SplitterMoving); //Link the SplitterMoving event of splitContainer1 to splitContainer1_SplitterMoving

private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
    if (KeyCode == Keys.Up || KeyCode == Keys.Down || KeyCode == Keys.Left || KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
    {
        KeyCode = Keys.A; //Reset the KeyCode
        e.Cancel = true; //Cancel the splitter movement
    }
}
private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
    KeyCode = e.KeyCode; //Set KeyCode to the KeyCode of the event
 // e.Handled = true; //Handle the event
}

谢谢,
我希望你觉得这有帮助:)

于 2012-12-22T05:54:20.783 回答