[英]Collapse 'button' for splitcontainer control
我在WinForms桌面應用程序中使用 Microsoft 的SplitContainer控件。
我想在拆分器控件的面板之間有一個小按鈕(或任何漂亮的 UI 元素)來折疊其中一個面板。 例如,一個有兩部分的“按鈕”,如果我點擊一個部分,右側面板會折疊,如果我點擊另一部分,左側面板會折疊。
這可能嗎? 如何實施?
我在我的實現中使用了這個解決方案,對你來說可能為時已晚,但可能會幫助其他人。
在我的實現中,我還將控件從一個面板移動到另一個面板,這就是為什么我只將面板折疊狀態更改為最后一個操作。
由於我發不了圖片,就按照下圖( [<] 和 [>] 是按鈕)試着算一下:
╔════════════╤═════════════╗
║ [<]│[>] ║
║ │ ║
║ │ ║
║ │ ║
║ │ ║
║ │ ║
╚════════════╧═════════════╝
以下是左側面板(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();
}
受 Lotus Notes 布局的啟發,我設計了一些我認為在這種情況下會很有用的東西。 它在面板之間僅包含一個按鈕,用於切換單個面板的展開/折疊狀態,但可以輕松修改為使用兩個按鈕來控制左右面板。 它使用兩個拆分容器,一個停靠在另一個內部,以及“中間”面板的 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;
}
}
由於移動人造分離器,處理面板的大小調整
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.