WinForms: Alternative to SplitContainer?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 09:27:31

问题


Are there any alternative controls someone can suggest to replace the WinForms SplitContainer? I don't like how the SplitContainer shows that weird, dotted strip when its selected and when its being dragged. I want to have the panels re-size as the user drags instead of on mouse up and not show any dotted strips when the splitter is being dragged. Basically like how all the re-sizing of panels is done in windows explorer on vista.

This is the dotted thing I'm talking about:


(source: bhslaughter.com)


回答1:


Write your own split container UserControl. You basically just drop two Panels onto the control (for the left and right panels) and then let the space between them be the splitter. A little MouseDown, MouseMove and MouseUp logic on the UserControl itself will let you easily move the "splitter" left and right, and the two panels will properly block this everywhere but over the splitter, so your logic for checking if the mouse if over the splitter is as simple as can be.

It may be a little extra work getting the control to act the way you want it to act in design mode.




回答2:


I found this after I saw your question, so thought I would share: SplitContainer FAQ

The second link on there tells exactly what you need to do.

Here is the text from that just in case the link ever dies.

//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;
   }
}



回答3:


You can't tinker with SplitContainer at all. One possibility is to eliminate it entirely if you are only using it to resize a control. You could use mouse events on the control itself instead. Drop a TreeView on a form and dock it on the left. Subscribe to the MouseDown/Move/Up events and write something like this:

    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;
    }



回答4:


I needed the same and set the following properties:

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

Hope this helps.




回答5:


Also, take a look at the split container component that comes with the free Krypton toolkit



来源:https://stackoverflow.com/questions/2701543/winforms-alternative-to-splitcontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!