2
有没有什么方法可以在可调整的拆分器上显示控件(如按钮),该拆分器在.NET的两个面板之间显示?SplitContainer
?将按钮控件添加到SplitContainer Splitter
例子:
我不认为SplitContainer
本身支持这一点,但覆盖的控制得到这个功能似乎无所不在的许多应用似乎有点多,我 - 我觉得就像我过度思考这个或者错过了一些明显的东西。
有没有什么方法可以在可调整的拆分器上显示控件(如按钮),该拆分器在.NET的两个面板之间显示?SplitContainer
?将按钮控件添加到SplitContainer Splitter
例子:
我不认为SplitContainer
本身支持这一点,但覆盖的控制得到这个功能似乎无所不在的许多应用似乎有点多,我 - 我觉得就像我过度思考这个或者错过了一些明显的东西。
下面是一个使用TableLayoutPanel来模拟SplitContainer的示例。
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
TableLayoutPanel rootPanel;
float originalWidth;
int splitPointX;
public Form1()
{
Controls.Add(rootPanel = new TableLayoutPanel
{
ColumnCount = 3,
ColumnStyles =
{
// Notice the use of Absolute here as this will control the 'splitting'
new ColumnStyle(SizeType.Absolute, 120F),
// Size of button panel
new ColumnStyle(SizeType.AutoSize),
// Remaining size
new ColumnStyle(SizeType.Percent, 100F),
},
Dock = DockStyle.Fill,
RowCount = 1,
RowStyles = { new RowStyle(SizeType.Percent, 100F) },
});
Panel buttonPanel;
rootPanel.Controls.Add(buttonPanel = new Panel
{
Anchor = AnchorStyles.Top | AnchorStyles.Bottom,
BackColor = SystemColors.ControlDark,
Margin = new Padding(0),
MinimumSize = new Size(80, 0),
Size = new Size(80, 0),
Controls =
{
// UseVisualStyleBackColor = true only because we altered the container's BackColor
new Button { Text = ">>", Location = new Point(19, 112), Size = new Size(40, 23), UseVisualStyleBackColor = true },
new Button { Text = ">", Location = new Point(19, 83), Size = new Size(40, 23), UseVisualStyleBackColor = true },
new Button { Text = "<", Location = new Point(19, 54), Size = new Size(40, 23), UseVisualStyleBackColor = true },
new Button { Text = "<<", Location = new Point(19, 25), Size = new Size(40, 23), UseVisualStyleBackColor = true },
},
}, 1, 0);
buttonPanel.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
// Capture mouse so that all mouse move messages go to this control
(s as Control).Capture = true;
// Record original column width
originalWidth = rootPanel.ColumnStyles[0].Width;
// Record first clicked point
// Convert to screen coordinates because this window will be a moving target
Point windowPoint = (s as Control).PointToScreen(e.Location);
splitPointX = windowPoint.X;
}
};
buttonPanel.MouseMove += (s, e) =>
{
if ((s as Control).Capture)
{
Point windowPoint = (s as Control).PointToScreen(e.Location);
// Calculate distance of mouse from splitPoint
int offset = windowPoint.X - splitPointX;
// Apply to originalWidth
float newWidth = originalWidth + offset;
// Clamp it.
// The control in the left pane's MinimumSize.Width would be more appropriate than zero
newWidth = Math.Max(0, newWidth);
// Update column width
if (Math.Abs(newWidth - rootPanel.ColumnStyles[0].Width) >= 1)
rootPanel.ColumnStyles[0].Width = newWidth;
}
};
buttonPanel.MouseUp += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
// Release mouse capture
if ((s as Control).Capture)
(s as Control).Capture = false;
}
};
}
}
您使用了大的黄色标签,我仍然不知道在哪里放置光标开始拖动。根本没有明显的线索。 SplitContainer不支持这种布局,它的分隔符不能有宽度。您可以使用位于左侧面板右侧的旧Splitter控制器进行操作。 – 2011-02-17 20:09:23
@Hans - 我同意这可能不是最好的方法,从哲学上讲。实际上,我使用`TableLayoutPanel`,它与`SplitContainer`非常相似,但没有拖动。尽管如此,我还是会争辩说,在分离器上放置一个抓杆指示器,而不是按钮可能是有用的,这也是不可能的。至于宽度,我会引导你到`SplitContainer.SplitterWidth`属性。我知道没有“旧的Splitter控制”(VS 2008) – 2011-02-17 22:54:05