2009-11-30 21 views
1

我有一些用戶控制面板分離器分隔。 包含面板設置爲AutoScroll。如何調整由分離器分離超出容器面板大小的控件的大小?

由於Splitter控件在調整「拆分」控件的大小時考慮了父級的大小,因此其內部UserControl的大小受面板大小的限制。

當用戶釋放鼠標時,我希望能夠將分離器向下移動到任何位置(甚至超出容器/表單的邊界),並相應地調整容器面板的大小(並在必要時顯示滾動條)。

我已經試過各種組合與不同的面板包裹它,與MINSIZE等..玩這個 是我想出了迄今爲止最好的,但它不是我想要的:

alt text

有沒有人有任何想法?

回答

2

當鼠標按鈕被按下時,您可以設置一個mouse hook,並且釋放鼠標按鈕時將其解除。在鉤子回調中,您可以根據需要觀察鼠標位置並調整控件的大小。

編輯:

你也可以使用一個特殊的控制,用戶可以拖動持有父容器的底部右下角的滾動位置。用戶可以通過拖動控件來增大區域,如果您不使用錨點或停靠點設置,則可以手動調整控件的大小以填充父區域。

我爲此項目執行了一段時間。我做了三角形,看起來像ToolStrip上的「抓地力」。下面是一些代碼片段從ScrollHolder控制:

public ScrollHolder() 
{ 
    this.Size = new Size(21, 21); 
    this.BackColor = SystemColors.Control; 
} 

protected override void OnPaint(PaintEventArgs e) 
{ 
    Point bottomLeft = new Point(0, this.Height); 
    Point topRight = new Point(this.Width, 0); 
    Pen controlDark = SystemPens.ControlDark; 
    Pen controlLightLight = SystemPens.ControlLightLight; 
    Pen controlDark2Px = new Pen(SystemColors.ControlDark, 2); 
    Point bottomRight = new Point(this.Width, this.Height); 
    e.Graphics.DrawLine(
     controlLightLight, 
     bottomLeft.X, 
     bottomLeft.Y - 2, 
     bottomRight.X, 
     bottomRight.Y - 2); 
    e.Graphics.DrawLine(controlDark, bottomLeft, topRight); 
    e.Graphics.DrawLine(
     controlLightLight, 
     bottomLeft.X + 1, 
     bottomLeft.Y, 
     topRight.X, 
     topRight.Y + 1); 
    e.Graphics.DrawLine(controlDark2Px, bottomLeft, bottomRight); 
    e.Graphics.DrawLine(controlDark2Px, bottomRight, topRight); 
    int xNumberOfGripDots = this.Width/4; 
    for (int x = 1; x < xNumberOfGripDots; x++) 
    { 
     for (int y = 1; y < 5 - x; y++) 
     { 
      DrawGripDot(e.Graphics, new Point(
       this.Width - (y * 4), this.Height - (x * 4) - 1)); 
     } 
    } 
} 

private static void DrawGripDot(Graphics g, Point location) 
{ 
    g.FillRectangle(
     SystemBrushes.ControlLightLight, location.X + 1, location.Y + 1, 2, 2); 
    g.FillRectangle(SystemBrushes.ControlDark, location.X, location.Y, 2, 2); 
} 

protected override void OnResize(EventArgs e) 
{ 
    this.SetRegion(); 
    base.OnResize(e); 
} 

private void SetRegion() 
{ 
    GraphicsPath path = new GraphicsPath(); 
    path.AddPolygon(new Point[] 
    { 
     new Point(this.Width, 0), 
     new Point(this.Width, this.Height), 
     new Point(0, this.Height) 
    }); 
    this.Region = new Region(path); 
} 

至於實際行爲實現的話,你可能會想:

  • 滾動到當可見區域之外移動滾動持有人:
  • 滾動到滾動條保持器時,通過在短時間內(例如50毫秒)呼叫Thread.Sleep來減慢它的速度。
+0

我正在考慮做那樣的事情。但是,它與分離器「陰影」的繪製產生了一些不一致。它將受到分配器控制本身的限制(除非我自己實踐)。當你有兩個以上的用戶控件時(這是我所做的),這是非常明顯的。 。 順便說一句,即使你不在表單中,你仍然會得到「SplitterMoving」事件。所以,不需要掛鉤。 – dtroy 2009-11-30 02:27:52

+0

我剛剛編輯了我的答案以提供另一種選擇。 – 2009-11-30 23:36:34