6

有人可以建议替换 WinForms SplitContainer 的任何替代控件吗?我不喜欢 SplitContainer 在被选中和被拖动时如何显示奇怪的虚线带。我想让面板在用户拖动而不是鼠标向上时重新调整大小,并且在拖动拆分器时不显示任何虚线。基本上就像在 vista 上的 Windows 资源管理器中完成所有面板大小调整一样。

这是我正在谈论的虚线:

分离器
(来源:bhslaughter.com

4

5 回答 5

9

编写您自己的拆分容器 UserControl。您基本上只需将两个面板放到控件上(用于左面板和右面板),然后让它们之间的空间成为分隔器。UserControl 本身上的一点 MouseDown、MouseMove 和 MouseUp 逻辑可以让您轻松地左右移动“拆分器”,并且两个面板将在除拆分器之外的任何地方正确阻止它,因此您的逻辑是检查鼠标是否在拆分器尽可能简单。

让控件按照您希望它在设计模式下的行为方式可能需要做一些额外的工作。

于 2010-04-23T20:10:08.610 回答
6

我看到你的问题后发现了这个,所以想我会分享: SplitContainer FAQ

那里的第二个链接准确地说明了您需要做什么。

这是其中的文本,以防万一链接失效。

//1.  Use the custom control defined in the SplitContainerNoFocus sample
//2. Insert the following code in your project, and attach these events to all of the SplitContainers that you don't want stealing focus.

// Temp variable to store a previously focused control
private Control focused = null; 

private void splitContainer_MouseDown(object sender, MouseEventArgs e)
{
   // Get the focused control before the splitter is focused
   focused = getFocused(this.Controls);
}

private Control getFocused(Control.ControlCollection controls)
{
   foreach (Control c in controls)
   {
      if (c.Focused)
      {
         // Return the focused control
         return c;
      }
      else if (c.ContainsFocus)
      {
         // If the focus is contained inside a control's children
         // return the child
         return getFocused(c.Controls);
      }
   }
   // No control on the form has focus
   return null;
}

private void splitContainer_MouseUp(object sender, MouseEventArgs e)
{
   // If a previous control had focus
   if (focused != null)
   {
      // Return focus and clear the temp variable for 
      // garbage collection
      focused.Focus();
      focused = null;
   }
}
于 2010-06-02T08:17:38.460 回答
1

我需要相同的并设置以下属性:

        splitContainer1.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
        splitContainer1.Dock = DockStyle.Fill;
        splitContainer1.IsSplitterFixed = true;

希望这可以帮助。

于 2010-07-23T18:08:43.700 回答
1

您根本无法修补 SplitContainer。如果您仅使用它来调整控件的大小,一种可能性是完全消除它。您可以改为在控件本身上使用鼠标事件。将 TreeView 拖放到窗体上并将其停靠在左侧。订阅 MouseDown/Move/Up 事件并编写如下内容:

    bool mDragging;

    private bool onTreeEdge(Point pos) {
        return pos.X >= treeView1.DisplayRectangle.Right - 3;
    }
    private void treeView1_MouseMove(object sender, MouseEventArgs e) {
        treeView1.Cursor = mDragging || onTreeEdge(e.Location) ? Cursors.VSplit : Cursors.Default;
        if (mDragging) treeView1.Width = e.X;
    }
    private void treeView1_MouseDown(object sender, MouseEventArgs e) {
        mDragging = onTreeEdge(e.Location);
        if (mDragging) treeView1.Capture = true;
    }
    private void treeView1_MouseUp(object sender, MouseEventArgs e) {
        mDragging = false;
    }
于 2010-04-23T20:37:41.723 回答
-1

另外,看看免费的氪星工具包附带的拆分容器组件

于 2011-08-22T14:55:28.373 回答