【发布时间】:2013-05-19 04:50:59
【问题描述】:
我创建了一个自定义表单 (FormBorderStyle = FormBorderStyle.None)。
我使用自己的自定义标题按钮(关闭、最大化...)在顶部绘制自己的标题栏。
现在我唯一的问题是向该表单添加普通用户控件。如果我给这些控件一个位置,这些位置是相对于窗体顶部的(包括标题栏)。
我使用“new”关键字覆盖了默认的 ClientSize 和 ClientRectangle,这使我可以对其进行调整(从而从中删除标题栏)。
这似乎不起作用,我无法弄清楚如何在不“破解” ControlAdded 事件的情况下正确执行此操作(这仍然是错误的)。
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control.GetType() != typeof(VlufiCaptionButton /* Caption buttons: close, minimize & maximize, should not be included */))
{
e.Control.Location = new Point(e.Control.Location.X + ClientRectangle.X, e.Control.Location.Y + ClientRectangle.Y);
e.Control.LocationChanged += Control_LocationChanged;
}
}
private void Control_LocationChanged(object sender, EventArgs e)
{
if (!childControlLocationChangedHandled)
{
System.Diagnostics.Debug.WriteLine("changing");
Control cControl = (Control)sender;
childControlLocationChangedHandled = true;
cControl.Location = new Point(cControl.Location.X + ClientRectangle.X, cControl.Location.Y + ClientRectangle.Y);
}
else
childControlLocationChangedHandled = false;
}
这是我目前使用的代码,但它超级笨拙,而且我的自定义绘制边框仍然存在其他问题。
有人知道我应该如何正确处理吗?
我找到了一个不错的解决方案:我在表单中添加了一个 ContainerControl,并根据表单对其进行定位和调整大小,然后每当向表单添加控件时,都应该将其添加到 ContainerControl .仍然不是一个合适的解决方案,但它是迄今为止最好的解决方案。
如果有人提出其他解决方案,我仍然会很感激。
【问题讨论】:
标签: c# winforms custom-controls