2013-04-20 25 views
0

我創建了一個自定義窗體(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。仍然不是一個合適的解決方案,但它是迄今爲止最好的解決方案。

如果有人想出另一個解決方案,我還是會很感激。

回答

-1

閱讀評論查看詳細:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     int dy = 0; 
     public Form1() 
     { 
      InitializeComponent(); 
      //i add a panel to top form 
      //(for simulating your caption bar) and get its height 
      dy = panel1.Height; //for yours its your caption bar height 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      //adding button control between form top and panel end area 
      //(simulate in your caption bar) 
      Button btn = new Button(); 
      btn.Location = new Point(panel1.Location.X+40,panel1.Location.Y+10); 
      btn.Text = "Salam"; 
      this.Controls.Add(btn); 
     } 
     //in control added event i add dy (height of ignored area) to control Location 
     private void Form1_ControlAdded(object sender, ControlEventArgs e) 
     { 
      e.Control.Location = new Point(e.Control.Location.X, e.Control.Location.Y + dy); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 
} 
+0

正如你可以在代碼中看到的那樣,這就是我正在做的事情。它在運行時更改位置和調整表單大小時帶來了很多錯誤 – 2013-04-20 20:04:40

+0

對不起,但我不明白你在說什麼,你的解決方案到底是什麼? – 2013-04-21 19:30:57

+0

添加解決方案詳細信息 – mojtaba 2013-04-22 11:10:40

0

好吧畢竟,我終於找到工作和相當不錯的解決方案。

我所做的是使用我自己的自定義ControlCollection覆蓋自定義窗體的Controls屬性。

所以這是我在我的自定義表單有:

public Control.ControlCollection RealControls 
{ 
    get 
    { 
     return base.Controls; 
    } 
} 
public new CustomControlCollection Controls { get; private set; } 

public ContainerControl ControlContainer { get; set; } 

這是自定義的ControlCollection:

public class CustomControlCollection 
{ 
    public VlufiForm Owner { get; private set; } 
    public CustomControlCollection (VlufiForm pOwner) 
    { 
     Owner = pOwner; 
    } 
    public void Add(Control c) 
    { 
     Add(c, false); 
    } 
    public int Count 
    { 
     get 
     { 
      return Owner.ControlContainer.Controls.Count; 
     } 
    } 
    public Control this[int index] 
    { 
     get 
     { 
      return Owner.ControlContainer.Controls[index]; 
     } 
    } 
    public void Add(Control c, bool isUsable) 
    { 
     if (isUsable) 
      Owner.RealControls.Add(c); 
     else 
      Owner.ControlContainer.Controls.Add(c); 
    } 
    public void SetChildIndex(Control c, int nIndex) 
    { 
     Owner.ControlContainer.Controls.SetChildIndex(c, nIndex); 
    } 
} 

這只是一個例子定製控件集合,你可以添加更多的方法(因此更多地繼承了ControlCollection)。 我還沒有在這個系統中發現任何錯誤,它現在完美的工作。

編輯:發現一個錯誤,如果你在Visual Studio的設計器模式下停靠一個控件,它會停靠在整個窗體中,但是在運行時不會出現。

相關問題