當鼠標按鈕被按下時,您可以設置一個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
來減慢它的速度。
我正在考慮做那樣的事情。但是,它與分離器「陰影」的繪製產生了一些不一致。它將受到分配器控制本身的限制(除非我自己實踐)。當你有兩個以上的用戶控件時(這是我所做的),這是非常明顯的。 。 順便說一句,即使你不在表單中,你仍然會得到「SplitterMoving」事件。所以,不需要掛鉤。 – dtroy 2009-11-30 02:27:52
我剛剛編輯了我的答案以提供另一種選擇。 – 2009-11-30 23:36:34