我在我的WinForms桌面应用程序中使用微软的WinForms控件。
我希望在拆分器控件的面板之间有一个小按钮(或任何好的UI元素)来折叠其中一个面板。例如,有两个部分的“按钮”,如果我单击一个部件,右侧面板崩溃,如果我单击另一个部分,则左侧面板崩溃。
这个是可能的吗?如何实现这一目标?
发布于 2011-06-17 00:17:45
发布于 2012-01-24 15:55:03
我已经在我的实现中使用了这个解决方案,对您来说可能为时已晚,但可能会帮助其他人。
在我的实现中,我还将控件从一个面板移动到另一个面板,这就是为什么我只将面板折叠状态更改为最后一个操作的原因。
由于我不能发布任何图像,所以只需根据下面的图表(<和>是按钮)来计算:
╔════════════╤═════════════╗
║ [<]│[>] ║
║ │ ║
║ │ ║
║ │ ║
║ │ ║
║ │ ║
╚════════════╧═════════════╝
下面是左面板(panel1)的实现,右侧面板也使用类似的函数。
private void setSplitterLeftPanelCollapsedState(bool collapse)
{
splitContainer1.SuspendLayout();
// Collapse the left panel
if (collapse)
{
if (!splitContainer1.Panel1Collapsed)
{
// restoring the panel in the end to apply layout changes
buttonOpenPanel1.Text = ">";
splitContainer1.Panel1Collapsed = true;
}
}
// Open the left panel
else
{
if (splitContainer1.Panel1Collapsed)
{
// collapsing the panel in the end to apply layout changes
buttonOpenPanel1.Text = "<";
splitContainer1.Panel1Collapsed = false;
}
}
splitContainer1.ResumeLayout();
comboBoxSearchText.Focus();
}
发布于 2013-10-24 17:50:31
受Lotus布局的启发,我设计了一些我认为在这种情况下有用的东西。它只包含面板之间的一个按钮,切换单个面板的展开/折叠状态,但可以很容易地修改为使用两个按钮来控制左右面板。它使用两个拆分容器(一个停靠在另一个容器中)和“中间”面板的mouseMove事件来模拟拖动拆分器(通过在C#中用鼠标拖动控件来移动控件)。此外,我使用容器的ClientSizedChanged事件来处理切换按钮图像的逻辑,而不是折叠/展开面板(检测SplitContainer折叠更改的时间)的方法。
设计:
splitContainer1
╔════════════╤═════════════════════════════════╗
║ │ splitContainer2 (docked fill) ║
║ │ ╔════════════╤════════════════╗ ║
║ │ ║ │ ║ ║
║ │ ║ Button(s) │ ║ ║
║ │ ║ [<>] │ ║ ║
║ │ ║ │ ║ ║
║ │ ╚════════════╧════════════════╝ ║
╚════════════╧═════════════════════════════════╝
splitContainer2.Dock = DockStyle.Fill;
splitContainer1 = splitContainer2.IsSplitterFixed = true;
splitContainer2.Panel1.Cursor = Cursors.VSplit;
向左或向右定位按钮(或在tableLayout控件中停靠多个按钮)。只需确保面板中仍有可单击/拖动的部分。
将中间面板的最大值设置为低数字。大小取决于你需要的按钮有多宽。
代码:
面板将切换到相反的状态。
如果您真的需要一个有两个部分的按钮,而不是两个按钮或一个切换按钮,您需要点击鼠标坐标,并根据点击发生的位置有不同的逻辑。
private void btnExpand_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
}
与展开/折叠相关联的Handel逻辑。我选择使用此事件是因为我的程序中有几种方法,用户可以折叠/展开面板。
private void splitContainer1_Panel2_ClientSizeChanged(object sender, EventArgs e)
{
if (splitContainer1.Panel1Collapsed)
{
splitContainer2.Panel1.Cursor = Cursors.Default;
this.btnExpand.Image = imageExpand;
}
Else
{
splitContainer2.Panel1.Cursor = Cursors.VSplit;
this.btnExpand.Image = imageCollapse;
}
}
由于移动faux拆分器而调整面板的大小
private void splitContainer2_Panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
/* All you really need is this:
splitContainer1.SplitterDistance += e.X;
Note: Splitter distance must be a positive integer and e.X will be negitive when dragging to the left of the splitContainer. You could handel this check here or on the splitterMoving event.
The code I have below shows how to “snap” a panel closed if the splitter is moved close enough to the edge
Or prevent a panel from being hidden from view (which could also be accomplished by setting the minimum size of a panel).
*/
if (e.X + splitContainer1.SplitterDistance < 40)
{
while (splitContainer1.SplitterDistance > 1)
splitContainer1.SplitterDistance--;
splitContainer1.Panel1Collapsed = true;
}
else if ((e.X + splitContainer1.SplitterDistance) * 1.00 / this.Width * 1.00 < .75)
splitContainer1.SplitterDistance += e.X;
else
Cursor.Current = Cursors.No;
}
}
https://stackoverflow.com/questions/6379858
复制相似问题