I have own control and I need to resize runtime by dragging. To resize bottom and right borders I use this:
protected override void OnMouseDown(MouseEventArgs e)
{
SL = new System.Drawing.Point(Location.X + e.Location.X, Location.Y + e.Location.Y);
SP = new System.Drawing.Point(Location.X, Location.Y);
if (e.X <= m)
_OnLeft = true;
if (e.X >= Width - m)
_OnRight = true;
if (e.Y <= m)
_OnTop = true;
if (e.Y >= Height - m)
_OnBottom = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
// Change Width - right
if (_OnRight && (!_OnTop && !_OnBottom))
{
if (e.X <= 1)
return;
Width = e.X;
return;
}
// Change Height - bottom
if (_OnBottom && (!_OnLeft && !_OnRight))
{
if (e.Y <= 1)
return;
Height = e.Y;
return;
}
}
All works fine. But I have problems with Top and Left resizing:
// Change Width - left
if (_OnLeft && (!_OnTop && !_OnBottom))
{
// Problem part - I don't know condition to return
if (Width + Left - e.X <= 1)
return;
Left += e.X - SL.X + SP.X;
// How to get right width
Width += Left - e.X;
return;
}
// Change Height - top
if (_OnTop && (!_OnLeft && !_OnRight))
{
// Problem part - I don't know condition to return
if (Height + Top - e.Y <= 1)
return;
Top += e.Y - SL.Y + SP.Y;
// How to get right height
Height += Top - e.Y;
return;
}
Something like that. Have ideas?
Pretty much the only way to cleanly allow .NET control resizing is to use P/Invoke. This exact code is not tested, but I have used this resizing method many times so it should work:
First, the P/Invoke external declarations:
Next, call the P/Invoke functions to have the operating system handle the resize:
Finally, override OnMouseMove to change the cursor based on where it is on the control. I’ll leave that part to you, because it’s almost the same code as the previous snippet.