我正在研究WinForm项目,并“尝试”创建一个TableLayoutPanel,用户可以在运行时调整大小,例如SplitContainer的行为。我发现一些代码可以部分执行此操作,但是它不完整。有人可以帮我吗?
在此先感谢-DA
到目前为止,这是我在CodeProject上找到的线程中提供的代码。我自己所做的唯一不同是创建一个从TableLayoutPanel继承的customTableLayoutPanel。
public partial class Form1 : Form
{
bool resizing = false;
TableLayoutRowStyleCollection rowStyles;
TableLayoutColumnStyleCollection columnStyles;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rowStyles = tableLayoutPanel1.RowStyles;
columnStyles = tableLayoutPanel1.ColumnStyles;
}
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
resizing = true;
}
}
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (resizing)
{
columnStyles[0].SizeType = SizeType.Absolute;
rowStyles[0].SizeType = SizeType.Absolute;
rowStyles[0].Height = e.Y;
columnStyles[0].Width = e.X;
}
}
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
resizing = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
将 Column SizeType 和 Row SizeType 属性设置为 Absolute 并将 CellBorderStyle 设置为您想要的,而不是之后在代码中编写如下
public partial class Form1 : Form
{
bool resizing = false;
TableLayoutRowStyleCollection rowStyles;
TableLayoutColumnStyleCollection columnStyles;
int colindex = -1;
int rowindex = -1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rowStyles = tableLayoutPanel1.RowStyles;
columnStyles = tableLayoutPanel1.ColumnStyles;
}
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
rowStyles = tableLayoutPanel1.RowStyles;
columnStyles = tableLayoutPanel1.ColumnStyles;
resizing = true;
}
}
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (!resizing)
{
float width = 0;
float height = 0;
//for rows
for (int i = 0; i < rowStyles.Count; i++)
{
height += rowStyles[i].Height;
if (e.Y > height - 3 && e.Y < height + 3)
{
rowindex = i;
tableLayoutPanel1.Cursor = Cursors.HSplit;
break;
}
else
{
rowindex = -1;
tableLayoutPanel1.Cursor = Cursors.Default;
}
}
//for columns
for (int i = 0; i < columnStyles.Count; i++)
{
width += columnStyles[i].Width;
if (e.X > width - 3 && e.X < width + 3)
{
colindex = i;
if (rowindex > -1)
tableLayoutPanel1.Cursor = Cursors.Cross;
else
tableLayoutPanel1.Cursor = Cursors.VSplit;
break;
}
else
{
colindex = -1;
if (rowindex == -1)
tableLayoutPanel1.Cursor = Cursors.Default;
}
}
}
if (resizing && (colindex>-1 || rowindex > -1))
{
float width = e.X;
float height = e.Y;
if (colindex > -1)
{
for (int i = 0; i < colindex; i++)
{
width -= columnStyles[i].Width;
}
columnStyles[colindex].SizeType = SizeType.Absolute;
columnStyles[colindex].Width = width;
}
if (rowindex > -1)
{
for (int i = 0; i < rowindex; i++)
{
height -= rowStyles[i].Height;
}
rowStyles[rowindex].SizeType = SizeType.Absolute;
rowStyles[rowindex].Height = height;
}
}
}
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
resizing = false;
tableLayoutPanel1.Cursor = Cursors.Default;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14877 次 |
| 最近记录: |