我創建了一個自定義窗體(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;
}
這是我目前使用的代碼,但它是superbuggy &我仍然有其他的問題,我的customly劃定的邊界。
有人知道我應該如何正確處理這個問題嗎?
我發現了一個體面的解決方案:我加了一個ContainerControl到窗體& I位置&大小此根據以下形式,然後每當增加一個控制到窗體,它應該被添加到一個ContainerControl。仍然不是一個合適的解決方案,但它是迄今爲止最好的解決方案。
如果有人想出另一個解決方案,我還是會很感激。
正如你可以在代碼中看到的那樣,這就是我正在做的事情。它在運行時更改位置和調整表單大小時帶來了很多錯誤 – 2013-04-20 20:04:40
對不起,但我不明白你在說什麼,你的解決方案到底是什麼? – 2013-04-21 19:30:57
添加解決方案詳細信息 – mojtaba 2013-04-22 11:10:40