|
Thank you so much for uploading this code, it helped me a lot.
|
|
|
|
|
Good job, this is exactly what I needed and the best thing is that it works in the latest version of Visual Studio!
Thank you very much!!!
|
|
|
|
|
Ladies and gentlemen,
I am working on a drag and drop application where the user might drag and drop any number of user controls. They all have to be movable and resizable, so I thought I'd copy to routines (mouse down, mouse up, mouse move, mouse leave, the enumeration, and the five variables defined in the class) into the user control class so that it will always be there no matter how many of these controls the user drags and drops. One of these user controls is simply a circle with the following OnPaint event
Private Sub Button1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
Dim myGraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
Dim bX As Integer = Button1.Width
Dim bY As Integer = Button1.Height
myGraphicsPath.AddEllipse(New Rectangle(0, 0, bX, bY))
Button1.Region = New Region(myGraphicsPath)
End Sub
I hope I can explain this clearly, here is what's happening:
When (with the mouse clicked) I move the cursor to the right of the control (the cursor does change and show the fuscia color. As I begin to expand to the circle (to make it an ellipse) and move to the right, the right half of the circle is NOT showing. It is disappearing. Only half of it is showing.
When I investigated the size of the button, Bx did actually change. The size is changing.
Another way to phrase the problem, it seems to only show the control in the region where it originally was, of whatever fits in the original box.
If I try to move it to a completely different location, it also disappears.
I would appreciate any feedback.
|
|
|
|
|
Alright folks, I think I solved the problem. Apparently, when it comes to user controls, there is the user control itself and the control inside it. In my case, a button with its own paint sub. When I was resizing the control, the container (the user control itself) was not being resized. similarly, when I move the button, the user control was not being moved. To overcome this, I changed my Paint subroutine as follows:
Private Sub Button1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
Dim myGraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
Dim bX As Integer = Button1.Width
Dim bY As Integer = Button1.Height
Me.Width = bX
Me.Height = bY
myGraphicsPath.AddEllipse(New Rectangle(0, 0, bX, bY))
Button1.Region = New Region(myGraphicsPath)
End Sub
In addition, I added a locationchanged sub as follows:
Private Sub Button1_LocationChanged(sender As Object, e As System.EventArgs) Handles Button1.LocationChanged
Dim Px As Integer = Me.Location.X
Dim Py As Integer = Me.Location.Y
Me.Location = New Point(Px + Button1.Location.X, Py + Button1.Location.Y)
Button1.Location = New Point(0, 0)
End Sub
The above moves the user control to where the button is.
There might be a more elegant way to do this, but this works.
|
|
|
|
|
Hello, this is amazing work. well done.
I was wandering if anybody could help to implement it to work with 2row and/or 2column TableLayoutPane and to be able to resize the each column or row in TableLayoutPanel?
many thanks in advance.
|
|
|
|
|
I have "translated" the code to C#. I have picked Josh-Mason11's version, I think it was more complete, (although was not standard ).
My congratulations to Jesse Chunn, after 8 years, his code remains being very useful.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CodyShop_MKeyBoardEditor
{
class ResizeableControl
{
private Control mControl;
private bool mMouseDown = false;
private EdgeEnum mEdge = EdgeEnum.None;
private int mWidth = 8;
private bool mOutlineDrawn = false;
int mouseX;
int mouseY;
private enum EdgeEnum
{
None,
Right,
Left,
Top,
Bottom,
Moving,
BottomRight,
BottomLeft,
TopLeft,
TopRight
};
public ResizeableControl(Control Control)
{
mControl = Control;
mControl.MouseDown += new System.Windows.Forms.MouseEventHandler(mControl_MouseDown);
mControl.MouseUp += new System.Windows.Forms.MouseEventHandler(mControl_MouseUp);
mControl.MouseMove += new System.Windows.Forms.MouseEventHandler(mControl_MouseMove);
mControl.MouseLeave += new System.EventHandler(mControl_MouseLeave);
}
private void mControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
mMouseDown = true;
}
}
private void mControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mMouseDown = false;
}
private Control SelControl;
private void mControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Control c = ((Control)sender);
Graphics g = c.CreateGraphics();
switch (mEdge)
{
case EdgeEnum.Moving:
mOutlineDrawn = true;
break;
case EdgeEnum.BottomRight:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, c.Height - mWidth, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.BottomLeft:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, 0, c.Height - mWidth, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.TopRight:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, 0, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.TopLeft:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.Left:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, 0, c.Height / 2 - 4, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.Right:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, c.Height / 2 - 4, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.Top:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, c.Width / 2 - 4, 0, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.Bottom:
DrawOrangeSquares(c, g);
g.FillRectangle(Brushes.Fuchsia, c.Width / 2 - 4, c.Height - mWidth, mWidth, mWidth);
mOutlineDrawn = true;
break;
case EdgeEnum.None:
if (mOutlineDrawn)
{
c.Refresh();
mOutlineDrawn = false;
}
break;
}
if (mMouseDown & mEdge != EdgeEnum.None)
{
c.SuspendLayout();
switch (mEdge)
{
case EdgeEnum.TopRight:
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
break;
case EdgeEnum.TopLeft:
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
break;
case EdgeEnum.BottomRight:
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
break;
case EdgeEnum.BottomLeft:
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
break;
case EdgeEnum.Left:
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
break;
case EdgeEnum.Right:
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
break;
case EdgeEnum.Top:
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
break;
case EdgeEnum.Bottom:
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
break;
case EdgeEnum.Moving:
c.SetBounds(c.Left + e.X - mouseX, c.Top + e.Y - mouseY, c.Width, c.Height);
break;
}
c.ResumeLayout();
}
else
{
if (e.X > c.Width - (mWidth) & e.Y > c.Height - (mWidth))
{
c.Cursor = Cursors.SizeNWSE;
mEdge = EdgeEnum.BottomRight;
}
else if (e.X <= mWidth & e.Y > c.Height - (mWidth))
{
c.Cursor = Cursors.SizeNESW;
mEdge = EdgeEnum.BottomLeft;
}
else if (e.X > c.Width - (mWidth) & e.Y <= mWidth)
{
c.Cursor = Cursors.SizeNESW;
mEdge = EdgeEnum.TopRight;
}
else if (e.X <= mWidth & e.Y <= mWidth)
{
c.Cursor = Cursors.SizeNWSE;
mEdge = EdgeEnum.TopLeft;
}
else if (e.X <= mWidth & e.Y >= c.Height / 2 - 4 & e.Y <= c.Height / 2 + 4)
{
c.Cursor = Cursors.SizeWE;
mEdge = EdgeEnum.Left;
}
else if (e.X >= c.Width - mWidth & e.Y >= c.Height / 2 - 4 & e.Y <= c.Height / 2 + 4)
{
c.Cursor = Cursors.SizeWE;
mEdge = EdgeEnum.Right;
}
else if (e.X >= c.Width / 2 - 4 & e.X <= c.Width / 2 + 4 & e.Y <= mWidth)
{
c.Cursor = Cursors.SizeNS;
mEdge = EdgeEnum.Top;
}
else if (e.X >= c.Width / 2 - 4 & e.X <= c.Width / 2 + 4 & e.Y >= c.Height - mWidth)
{
c.Cursor = Cursors.SizeNS;
mEdge = EdgeEnum.Bottom;
}
else if (e.X <= c.Width & e.Y <= c.Height)
{
c.Cursor = Cursors.SizeAll;
mEdge = EdgeEnum.Moving;
DrawOrangeSquares(c, g);
}
else
{
c.Cursor = Cursors.Default;
mEdge = EdgeEnum.None;
}
}
}
private void mControl_MouseLeave(object sender, System.EventArgs e)
{
Control c = ((Control)sender);
mEdge = EdgeEnum.None;
c.Refresh();
c.Cursor = Cursors.Default;
}
internal void DrawOrangeSquares(Control c, Graphics g)
{
g.FillRectangle(Brushes.White, c.Width - mWidth - 1, c.Height - mWidth - 1, mWidth + 1, mWidth + 1);
g.FillRectangle(Brushes.OrangeRed, c.Width - mWidth, c.Height - mWidth, mWidth, mWidth);
g.FillRectangle(Brushes.White, 0, c.Height - mWidth - 1, mWidth + 1, mWidth + 1);
g.FillRectangle(Brushes.OrangeRed, 0, c.Height - mWidth, mWidth, mWidth);
g.FillRectangle(Brushes.White, 0, c.Height / 2 - 5, mWidth + 1, mWidth + 2);
g.FillRectangle(Brushes.OrangeRed, 0, c.Height / 2 - 4, mWidth, mWidth);
g.FillRectangle(Brushes.White, c.Width - mWidth - 1, c.Height / 2 - 5, mWidth + 1, mWidth + 2);
g.FillRectangle(Brushes.OrangeRed, c.Width - mWidth, c.Height / 2 - 4, mWidth, mWidth);
g.FillRectangle(Brushes.White, c.Width / 2 - 5, 0, mWidth + 2, mWidth + 1);
g.FillRectangle(Brushes.OrangeRed, c.Width / 2 - 4, 0, mWidth, mWidth);
g.FillRectangle(Brushes.White, c.Width / 2 - 5, c.Height - mWidth - 1, mWidth + 2, mWidth + 1);
g.FillRectangle(Brushes.OrangeRed, c.Width / 2 - 4, c.Height - mWidth, mWidth, mWidth);
g.FillRectangle(Brushes.White, c.Width - mWidth - 1, 0, mWidth + 1, mWidth + 1);
g.FillRectangle(Brushes.OrangeRed, c.Width - mWidth, 0, mWidth, mWidth);
g.FillRectangle(Brushes.White, 0, 0, mWidth + 1, mWidth + 1);
g.FillRectangle(Brushes.OrangeRed, 0, 0, mWidth, mWidth);
}
}
}
Regards.
|
|
|
|
|
Sweet. Thanks for the translation, and good job using the updated code.
Standard answer: It can be done in two weeks, provided nothing unexpected happens.
|
|
|
|
|
HOW DO I CHANGE THE FUCHSIA BORDERS WITH SIZING HANDLES?
|
|
|
|
|
Hi folks,
I tried the code here and was very impressed - having just started out on switching to VB.Net using Studio 2013, I found this a great learning experience. Below I have modified the code SLIGHTLY - I modified it because on Studio 2013, the bottom and right resize wasn't working - I believe they have changed things slightly? Either way this code is optimised for STUDIO 2013 - I haven't tested it on any other version. I did try to use the previous reply`s code but it references undeclared objects (Quiz?) and this one does not
The changes
The biggest change is that I changed the names of most of the variables to be descriptive - one of the things I always insisted on as a head programmer was to make everything maximum legible so anyone else can adjust it to suit their needs. I believe with the extra comments and renamed variables I have achieved this
I removed a few unnecessary items in some of the calculations where the math was taking a variable away from the same variable - this was unneccesary because of course the result would always be zero.
I changed the logic ever so slightly so when resizing the edge highlight disappears - its unnecessary because the mouse has already changed to reflect the resize and the user is resizing!
Anyway, I hope this helps someone in some way, the way the original code helped me. Thanks a million to Jesse! You rule
Public Class clsMoveRersize
Private WithEvents ControlToMove As Control
Private IsLeftButtonHeld As Boolean = False
Private ObjectEdge As EdgeEnum = EdgeEnum.None
Private HighlightWidth As Integer = 4
Private IsHighlightDrawn As Boolean = False
Private Enum EdgeEnum
None
Right
Left
Top
Bottom
TopLeft
BottomRight
End Enum
Public Sub New(ByVal SourceControl As Control)
ControlToMove = SourceControl
End Sub
Private Sub ControlToMove_MouseDown(ByVal sender As Object, _
ByVal MouseStatus As System.Windows.Forms.MouseEventArgs) _
Handles ControlToMove.MouseDown
If MouseStatus.Button = Windows.Forms.MouseButtons.Left Then
IsLeftButtonHeld = True
sender.Refresh()
IsHighlightDrawn = False
End If
End Sub
Private Sub ControlToMove_MouseUp(ByVal sender As Object, _
ByVal MouseStatus As System.Windows.Forms.MouseEventArgs) _
Handles ControlToMove.MouseUp
IsLeftButtonHeld = False
End Sub
Private Sub ControlToMove_MouseMove(ByVal sender As Object, _
ByVal MouseStatus As System.Windows.Forms.MouseEventArgs) _
Handles ControlToMove.MouseMove
Dim SourceControl As Control = CType(sender, Control)
Dim GraphicsBuffer As Graphics = SourceControl.CreateGraphics
If Not IsLeftButtonHeld Then
Select Case ObjectEdge
Case EdgeEnum.TopLeft
GraphicsBuffer.FillRectangle(Brushes.Fuchsia, 0, 0, HighlightWidth * 4,_
HighlightWidth * 4)
IsHighlightDrawn = True
Case EdgeEnum.Left
GraphicsBuffer.FillRectangle(Brushes.Fuchsia, 0, 0, HighlightWidth,_
SourceControl.Height)
IsHighlightDrawn = True
Case EdgeEnum.Right
GraphicsBuffer.FillRectangle(Brushes.Fuchsia, SourceControl.Width _
- HighlightWidth - 5, 0, _
SourceControl.Width, SourceControl.Height)
IsHighlightDrawn = True
Case EdgeEnum.Top
GraphicsBuffer.FillRectangle(Brushes.Fuchsia, 0, 0, SourceControl.Width,_
HighlightWidth)
IsHighlightDrawn = True
Case EdgeEnum.Bottom
GraphicsBuffer.FillRectangle(Brushes.Fuchsia, 0, SourceControl.Height - _
HighlightWidth - 5, SourceControl.Width, HighlightWidth)
IsHighlightDrawn = True
Case EdgeEnum.None
If IsHighlightDrawn Then
SourceControl.Refresh()
IsHighlightDrawn = False
End If
End Select
End If
If IsLeftButtonHeld And ObjectEdge <> EdgeEnum.None Then
SourceControl.SuspendLayout()
Select Case ObjectEdge
Case EdgeEnum.TopLeft
SourceControl.SetBounds(SourceControl.Left + MouseStatus.X, SourceControl.Top _
+ MouseStatus.Y, SourceControl.Width, SourceControl.Height)
Case EdgeEnum.Left
SourceControl.SetBounds(SourceControl.Left + MouseStatus.X, SourceControl.Top, _
SourceControl.Width - MouseStatus.X, SourceControl.Height)
Case EdgeEnum.Right
SourceControl.SetBounds(SourceControl.Left, SourceControl.Top, MouseStatus.X,_
SourceControl.Height)
Case EdgeEnum.Top
SourceControl.SetBounds(SourceControl.Left, SourceControl.Top + MouseStatus.Y, _
SourceControl.Width, SourceControl.Height - MouseStatus.Y)
Case EdgeEnum.Bottom
SourceControl.SetBounds(SourceControl.Left, SourceControl.Top, _
SourceControl.Width, MouseStatus.Y + 8)
End Select
SourceControl.ResumeLayout()
Else
Select Case True
Case MouseStatus.X <= (HighlightWidth * 4) And MouseStatus.Y <= (HighlightWidth * 4)
SourceControl.Cursor = Cursors.SizeAll
ObjectEdge = EdgeEnum.TopLeft
Case MouseStatus.X <= HighlightWidth
SourceControl.Cursor = Cursors.VSplit
ObjectEdge = EdgeEnum.Left
Case MouseStatus.X > SourceControl.Width - (HighlightWidth + 5)
SourceControl.Cursor = Cursors.VSplit
ObjectEdge = EdgeEnum.Right
Case MouseStatus.Y <= HighlightWidth
SourceControl.Cursor = Cursors.HSplit
ObjectEdge = EdgeEnum.Top
Case MouseStatus.Y > SourceControl.Height - (HighlightWidth + 5)
SourceControl.Cursor = Cursors.HSplit
ObjectEdge = EdgeEnum.Bottom
Case Else
SourceControl.Cursor = Cursors.Default
ObjectEdge = EdgeEnum.None
End Select
End If
End Sub
Private Sub ControlToMove_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ControlToMove.MouseLeave
Dim SourceControl As Control = CType(sender, Control)
ObjectEdge = EdgeEnum.None
SourceControl.Refresh()
End Sub
End Class
modified 26-Jun-14 8:45am.
|
|
|
|
|
I have added the ability to drag the corners, the mouse doesn't jump to the top left corner OnMouseDown and orange blocks, where drag is possible, show up on mousemove as well as changing colourwhen the muse is on one of them.
Public Class ResizeableControl
Private WithEvents mControl As Control
Private mMouseDown As Boolean = False
Private mEdge As EdgeEnum = EdgeEnum.None
Private mWidth As Integer = 8
Private mOutlineDrawn As Boolean = False
Dim mouseX As Integer
Dim mouseY As Integer
Private Enum EdgeEnum
None
Right
Left
Top
Bottom
Moving
BottomRight
BottomLeft
TopLeft
TopRight
End Enum
Public Sub New(ByVal Control As Control)
mControl = Control
End Sub
Private Sub mControl_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles mControl.MouseDown
CreateQuiz.QuizDesignPanel.Refresh()
mouseX = e.X
mouseY = e.Y
If e.Button = Windows.Forms.MouseButtons.Left Then
mMouseDown = True
CreateQuiz.ControlCreativePanel.Visible = False
CreateQuiz.PicCreativePanel.Visible = False
CreateQuiz.PanelCreativePanel.Visible = False
End If
End Sub
Private Sub mControl_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles mControl.MouseUp
mMouseDown = False
End Sub
Private SelControl As Control
Private Sub mControl_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles mControl.MouseMove
Dim c As Control = CType(sender, Control)
Dim g As Graphics = c.CreateGraphics
Select Case mEdge
Case EdgeEnum.Moving
mOutlineDrawn = True
Case EdgeEnum.BottomRight
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
c.Width - mWidth, c.Height - mWidth, mWidth, mWidth)
mOutlineDrawn = True
Case EdgeEnum.BottomLeft
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
0, c.Height - mWidth, mWidth, mWidth)
mOutlineDrawn = True
Case EdgeEnum.TopRight
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
c.Width - mWidth, 0, mWidth, mWidth) 'top right
mOutlineDrawn = True
Case EdgeEnum.TopLeft
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
0, 0, mWidth, mWidth) 'top left
mOutlineDrawn = True
Case EdgeEnum.Left
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
0, c.Height \ 2 - 4, mWidth, mWidth) 'left
mOutlineDrawn = True
Case EdgeEnum.TopRight
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
c.Width - mWidth, 0, mWidth, mWidth)
Case EdgeEnum.Right
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
c.Width - mWidth, c.Height \ 2 - 4, mWidth, mWidth) 'right
mOutlineDrawn = True
Case EdgeEnum.Top
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
c.Width \ 2 - 4, 0, mWidth, mWidth) 'top
mOutlineDrawn = True
Case EdgeEnum.Bottom
DrawOrangeSquares(c, g)
g.FillRectangle(Brushes.Fuchsia, _
c.Width \ 2 - 4, c.Height - mWidth, mWidth, mWidth) 'bottom
mOutlineDrawn = True
Case EdgeEnum.None
If mOutlineDrawn Then
c.Refresh()
mOutlineDrawn = False
End If
End Select
If mMouseDown And mEdge <> EdgeEnum.None Then
MsgBox(c.ToString)
c.SuspendLayout()
Select Case mEdge
Case EdgeEnum.TopRight
c.SetBounds(c.Left, c.Top, _
c.Width - (c.Width - e.X), c.Height)
c.SetBounds(c.Left, c.Top + e.Y, _
c.Width, c.Height - e.Y)
Case EdgeEnum.TopLeft
c.SetBounds(c.Left + e.X, c.Top, _
c.Width - e.X, c.Height)
c.SetBounds(c.Left, c.Top + e.Y, _
c.Width, c.Height - e.Y)
Case EdgeEnum.BottomRight
c.SetBounds(c.Left, c.Top, _
c.Width - (c.Width - e.X), c.Height)
c.SetBounds(c.Left, c.Top, _
c.Width, c.Height - (c.Height - e.Y))
Case EdgeEnum.BottomLeft
c.SetBounds(c.Left + e.X, c.Top, _
c.Width - e.X, c.Height)
c.SetBounds(c.Left, c.Top, _
c.Width, c.Height - (c.Height - e.Y))
Case EdgeEnum.Left
c.SetBounds(c.Left + e.X, c.Top, _
c.Width - e.X, c.Height)
Case EdgeEnum.Right
c.SetBounds(c.Left, c.Top, _
c.Width - (c.Width - e.X), c.Height)
Case EdgeEnum.Top
c.SetBounds(c.Left, c.Top + e.Y, _
c.Width, c.Height - e.Y)
Case EdgeEnum.Bottom
c.SetBounds(c.Left, c.Top, _
c.Width, c.Height - (c.Height - e.Y))
Case EdgeEnum.Moving
c.SetBounds(c.Left + e.X - mouseX, c.Top + e.Y - mouseY, _
c.Width, c.Height)
End Select
c.ResumeLayout()
Else
Select Case True
Case e.X > c.Width - (mWidth) And _
e.Y > c.Height - (mWidth) 'Bottom right corner
c.Cursor = Cursors.SizeNWSE
mEdge = EdgeEnum.BottomRight
Case e.X <= mWidth And _
e.Y > c.Height - (mWidth) 'Bottom Left corner
c.Cursor = Cursors.SizeNESW
mEdge = EdgeEnum.BottomLeft
Case e.X > c.Width - (mWidth) And _
e.Y <= mWidth 'Top Right corner
c.Cursor = Cursors.SizeNESW
mEdge = EdgeEnum.TopRight
Case e.X <= mWidth And _
e.Y <= mWidth 'Top Left corner
c.Cursor = Cursors.SizeNWSE
mEdge = EdgeEnum.TopLeft
Case e.X <= mWidth And _
e.Y >= c.Height \ 2 - 4 And e.Y _
<= c.Height \ 2 + 4 'left edge
c.Cursor = Cursors.SizeWE
mEdge = EdgeEnum.Left
Case e.X >= c.Width - mWidth And _
e.Y >= c.Height \ 2 - 4 And e.Y _
<= c.Height \ 2 + 4 'right edge
c.Cursor = Cursors.SizeWE
mEdge = EdgeEnum.Right
Case e.X >= c.Width \ 2 - 4 And _
e.X <= c.Width \ 2 + 4 And _
e.Y <= mWidth 'top edge
c.Cursor = Cursors.SizeNS
mEdge = EdgeEnum.Top
Case e.X >= c.Width \ 2 - 4 And _
e.X <= c.Width \ 2 + 4 And _
e.Y >= c.Height - mWidth 'bottom edge
c.Cursor = Cursors.SizeNS
mEdge = EdgeEnum.Bottom
Case e.X <= c.Width And e.Y <= c.Height 'move
c.Cursor = Cursors.SizeAll
mEdge = EdgeEnum.Moving
DrawOrangeSquares(c, g)
Case Else 'no edge
c.Cursor = Cursors.Default
mEdge = EdgeEnum.None
End Select
End If
End Sub
Private Sub mControl_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles mControl.MouseLeave
Dim c As Control = CType(sender, Control)
mEdge = EdgeEnum.None
c.Refresh()
c.Cursor = Cursors.Default
End Sub
Sub DrawOrangeSquares(c As Control, g As Graphics)
g.FillRectangle(Brushes.White, _
c.Width - mWidth - 1, c.Height - mWidth - 1, mWidth + 1, mWidth + 1)
g.FillRectangle(Brushes.OrangeRed, _
c.Width - mWidth, c.Height - mWidth, mWidth, mWidth) 'bottom right
g.FillRectangle(Brushes.White, _
0, c.Height - mWidth - 1, mWidth + 1, mWidth + 1)
g.FillRectangle(Brushes.OrangeRed, _
0, c.Height - mWidth, mWidth, mWidth) 'bottom left
g.FillRectangle(Brushes.White, _
0, c.Height \ 2 - 5, mWidth + 1, mWidth + 2)
g.FillRectangle(Brushes.OrangeRed, _
0, c.Height \ 2 - 4, mWidth, mWidth) 'left
g.FillRectangle(Brushes.White, _
c.Width - mWidth - 1, c.Height \ 2 - 5, mWidth + 1, mWidth + 2)
g.FillRectangle(Brushes.OrangeRed, _
c.Width - mWidth, c.Height \ 2 - 4, mWidth, mWidth) 'right
g.FillRectangle(Brushes.White, _
c.Width \ 2 - 5, 0, mWidth + 2, mWidth + 1)
g.FillRectangle(Brushes.OrangeRed, _
c.Width \ 2 - 4, 0, mWidth, mWidth) 'top
g.FillRectangle(Brushes.White, _
c.Width \ 2 - 5, c.Height - mWidth - 1, mWidth + 2, mWidth + 1)
g.FillRectangle(Brushes.OrangeRed, _
c.Width \ 2 - 4, c.Height - mWidth, mWidth, mWidth) 'bottom
g.FillRectangle(Brushes.White, _
c.Width - mWidth - 1, 0, mWidth + 1, mWidth + 1)
g.FillRectangle(Brushes.OrangeRed, _
c.Width - mWidth, 0, mWidth, mWidth) 'top right
g.FillRectangle(Brushes.White, _
0, 0, mWidth + 1, mWidth + 1)
g.FillRectangle(Brushes.OrangeRed, _
0, 0, mWidth, mWidth) 'top left
End Sub
End Class
|
|
|
|
|
Not rude at all! I posted this in 2007. It is great to see folks still finding it useful, and even better that you have improved it. That is what this site is all about.
Thanks!
Standard answer: It can be done in two weeks, provided nothing unexpected happens.
|
|
|
|
|
Well, it's certainly the best code for it's purpose I could find. It also taught me how to use classes and cases!
A little off topic.
I'm 16 and just starting college. I'm really into code (especially VB.NET at the moment) and would absolutely love a career that involves it. Jut out of interest, if you don't mind saying, how did you get to the position you're at now. Did you take the right courses? Got lucky and recognized by somebody in the business? Or even know the right people? You don't have to say but I would really like to know how to get started (turning a hobby into a profession). Thanks
|
|
|
|
|
Well, I know this is a very, very late response, but I'll answer anyway. I started out by learning to program. I went to a local community college while working full time until I got my associates degree in Computer Science. I got a job as a programmer pretty much immediately, which was a big change from the job I had at that time, loading trucks. I actually started out as a programmer making less than I was making loading trucks. I had (and have) a love for programming, so when I was not working, I was at home writing software for all sorts of purposes. I eventually built up a reputation as a "cracker-jack" programmer, and began picking up side-jobs writing software for companies outside of my day job. I got those jobs through people I worked with that had spouses at other companies that needed short-term contract help. Eventually I started my own company and ran that for about a decade before I sold it. The connections I made while running my company (and since) are the primary source of jobs I have had since then. I also got a BS in Business Management along the way (more than a decade after getting my associates degree), all while working full time (way more than 40 hours per week). I didn't get any help from anyone, and paid my way as I went, and I dropped out of high-school (went back as an adult to get diploma), so if I can do it, anybody can. All you need is passion and determination. Period. Everything else will come. And by the way, don't be afraid to branch out to other programming languages. It may seem hard at first, but they are all pretty much the same once you spend a week or two working at it, and each one has strengths and weaknesses depending on what you are trying to do.
Good luck my friend.
Standard answer: It can be done in two weeks, provided nothing unexpected happens.
|
|
|
|
|
This is a great solution and it helped me a lot but could somebody please help me out with this.
I would like to add the possibility that if you keep the CTRL key pressed it does a drag with the control instead of resize it.
I am trying this but can't get the event to fire.
Private Sub mControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mControl.KeyDown
' Determine whether the key entered is the Ctrl key. If it is, display Help.
If e.KeyCode = Keys.ControlKey Then
ControlEntered = True
dragging = True
MsgBox("true")
End If
End Sub
|
|
|
|
|
Do you still want a solution to this, as I have gotten quite familiar with the class?
|
|
|
|
|
Hello everyone, I am fairly new to vbnet (and to programming altogether) and was working on a project that required the user to move and resize picture boxes, so this was extremely helpful.
However, I do have one question if anyone can please answer it. Can anyone tell me exactly which part of the code does the resizing and which one does the moving? This will be very helpful for those of us coming down the learning curve.
|
|
|
|
|
How to resize a veritcal scroll bar. It doesnt work with this code ?
|
|
|
|
|
Could someone guide me to use this code to make a resizable button when a button is clicked...
I lyk 2 thank u in advance for the help..
|
|
|
|
|
|
|
I love this article. I have modified it little to allow smooth drag.
I am aware that this post is approx. 2 years old...but still I hope it may help someone.
Once again thanks to Jesse
Public Class ResizeableControl
Private WithEvents mControl As Control
Private mMouseDown As Boolean = False
Private mEdge As EdgeEnum = EdgeEnum.None
Private mWidth As Integer = 4
Private mOutlineDrawn As Boolean = False
Private beginX, beginY As Integer
Public Enum EdgeEnum
None = 0
Right = 1
Left = 2
Top = 4
Bottom = 8
TopLeft = 16
'added
All = TopLeft Or Left Or Right Or Top Or Bottom
ResizeAnchorTopLeft = Right Or Bottom
OnlyMove = TopLeft
'end added
End Enum
Public Property AllowEdges() As EdgeEnum
Get
Return _AllowEdges
End Get
Set(ByVal value As EdgeEnum)
_AllowEdges = value
End Set
End Property
Friend _AllowEdges As EdgeEnum = EdgeEnum.All 'Default Behavior
Public Property HighlightColor() As Drawing.Color
Get
Return _HighlightColor
End Get
Set(ByVal value As Drawing.Color)
_HighlightColor = value
End Set
End Property
Private _HighlightColor As Drawing.Color = Color.Fuchsia
Public Sub New(ByVal Control As Control)
mControl = Control
End Sub
'added
Public Sub New(ByVal Control As Control, ByVal AllowedEdges As EdgeEnum)
mControl = Control
_AllowEdges = AllowedEdges
End Sub
'end added
Private Sub mControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then mMouseDown = True
beginX = e.X
beginY = e.Y
End Sub
Private Sub mControl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
mMouseDown = False
End Sub
'added
Private mpLast_Location As Point = New Point(0, 0)
'end added
Private Sub mControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
Dim c As Control = CType(sender, Control)
Dim g As Graphics = c.CreateGraphics
Dim B As New System.Drawing.SolidBrush(_HighlightColor)
'added 'Moved from Select Case mEdge : Case EdgeEnum.None
If mOutlineDrawn Then
c.Refresh()
mOutlineDrawn = False
End If
'end added
Select Case mEdge
Case EdgeEnum.TopLeft
g.FillRectangle(B, 0, 0, mWidth * 4, mWidth * 4)
mOutlineDrawn = True
Case EdgeEnum.Left
g.FillRectangle(B, 0, 0, mWidth, c.Height)
mOutlineDrawn = True
Case EdgeEnum.Right
g.FillRectangle(B, c.Width - mWidth, 0, c.Width, c.Height)
mOutlineDrawn = True
Case EdgeEnum.Top
g.FillRectangle(B, 0, 0, c.Width, mWidth)
mOutlineDrawn = True
Case EdgeEnum.Bottom
g.FillRectangle(B, 0, c.Height - mWidth, c.Width, mWidth)
mOutlineDrawn = True
'Case EdgeEnum.None 'Moved before Select Case
' If mOutlineDrawn Then
' c.Refresh()
' mOutlineDrawn = False
' End If
End Select
If mMouseDown And mEdge <> EdgeEnum.None Then
c.SuspendLayout()
Select Case mEdge
Case EdgeEnum.TopLeft
c.Location = New Point(c.Location.X + e.X - beginX, c.Location.Y + e.Y - beginY)
c.Parent.Refresh()
''added
'Dim iX_Delta As Integer = e.X
'Dim iY_Delta As Integer = e.Y
'If Not (mpLast_Location = New Point(0, 0)) Then
' iX_Delta -= mpLast_Location.X
' iY_Delta -= mpLast_Location.Y
'End If
'c.SetBounds(c.Left + iX_Delta, c.Top + iY_Delta, c.Width, c.Height)
''end added
''c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height) 'Original
'RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Left
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Right
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height)
RaiseEvent ResizeOccurred(c)
'added
mpLast_Location = e.Location
'end added
Case EdgeEnum.Top
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Bottom
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y))
RaiseEvent ResizeOccurred(c)
End Select
c.ResumeLayout()
Else
Select Case True
Case e.X <= (mWidth * 4) And e.Y <= (mWidth * 4) 'top left corner
c.Cursor = Cursors.SizeAll
mEdge = EdgeEnum.TopLeft
Case e.X <= mWidth 'left edge
c.Cursor = Cursors.VSplit
mEdge = EdgeEnum.Left
Case e.X > c.Width - (mWidth + 1) 'right edge
c.Cursor = Cursors.VSplit
mEdge = EdgeEnum.Right
Case e.Y <= mWidth 'top edge
c.Cursor = Cursors.HSplit
mEdge = EdgeEnum.Top
Case e.Y > c.Height - (mWidth + 1) 'bottom edge
c.Cursor = Cursors.HSplit
mEdge = EdgeEnum.Bottom
Case Else 'no edge
c.Cursor = Cursors.Default
mEdge = EdgeEnum.None
End Select
mEdge = mEdge And _AllowEdges
If mEdge = EdgeEnum.None Then c.Cursor = Cursors.Default
End If
End Sub
Private Sub mControl_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
Dim c As Control = CType(sender, Control)
c.Cursor = Cursors.Default
mEdge = EdgeEnum.None
c.Refresh()
End Sub
Public Event ResizeOccurred(ByRef c As System.Windows.Forms.Control)
End Class
|
|
|
|
|
*Fixed the line drawing problem
*Fixed the TopLeft (movement error)
*Added a new Constructor that allow to set the AllowedEdges
*Added Developer-Friendly Allow-Edges Enums: All, ResizeAnchorTopLeft, OnlyMove
Public Class ResizeableControl
Private WithEvents mControl As Control
Private mMouseDown As Boolean = False
Private mEdge As EdgeEnum = EdgeEnum.None
Private mWidth As Integer = 4
Private mOutlineDrawn As Boolean = False
Public Enum EdgeEnum
None = 0
Right = 1
Left = 2
Top = 4
Bottom = 8
TopLeft = 16
'added
All = TopLeft Or Left Or Right Or Top Or Bottom
ResizeAnchorTopLeft = Right Or Bottom
OnlyMove = TopLeft
'end added
End Enum
Public Property AllowEdges() As EdgeEnum
Get
Return _AllowEdges
End Get
Set(ByVal value As EdgeEnum)
_AllowEdges = value
End Set
End Property
Friend _AllowEdges As EdgeEnum = EdgeEnum.All 'Default Behavior
Public Property HighlightColor() As Drawing.Color
Get
Return _HighlightColor
End Get
Set(ByVal value As Drawing.Color)
_HighlightColor = value
End Set
End Property
Private _HighlightColor As Drawing.Color = Color.Fuchsia
Public Sub New(ByVal Control As Control)
mControl = Control
End Sub
'added
Public Sub New(ByVal Control As Control, ByVal AllowedEdges As EdgeEnum)
mControl = Control
_AllowEdges = AllowedEdges
End Sub
'end added
Private Sub mControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then mMouseDown = True
End Sub
Private Sub mControl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
mMouseDown = False
End Sub
'added
Private mpLast_Location As Point = New Point(0, 0)
'end added
Private Sub mControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
Dim c As Control = CType(sender, Control)
Dim g As Graphics = c.CreateGraphics
Dim B As New System.Drawing.SolidBrush(_HighlightColor)
'added 'Moved from Select Case mEdge : Case EdgeEnum.None
If mOutlineDrawn Then
c.Refresh()
mOutlineDrawn = False
End If
'end added
Select Case mEdge
Case EdgeEnum.TopLeft
g.FillRectangle(B, 0, 0, mWidth * 4, mWidth * 4)
mOutlineDrawn = True
Case EdgeEnum.Left
g.FillRectangle(B, 0, 0, mWidth, c.Height)
mOutlineDrawn = True
Case EdgeEnum.Right
g.FillRectangle(B, c.Width - mWidth, 0, c.Width, c.Height)
mOutlineDrawn = True
Case EdgeEnum.Top
g.FillRectangle(B, 0, 0, c.Width, mWidth)
mOutlineDrawn = True
Case EdgeEnum.Bottom
g.FillRectangle(B, 0, c.Height - mWidth, c.Width, mWidth)
mOutlineDrawn = True
'Case EdgeEnum.None 'Moved before Select Case
' If mOutlineDrawn Then
' c.Refresh()
' mOutlineDrawn = False
' End If
End Select
If mMouseDown And mEdge <> EdgeEnum.None Then
c.SuspendLayout()
Select Case mEdge
Case EdgeEnum.TopLeft
'added
Dim iX_Delta As Integer = e.X
Dim iY_Delta As Integer = e.Y
If Not (mpLast_Location = New Point(0, 0)) Then
iX_Delta -= mpLast_Location.X
iY_Delta -= mpLast_Location.Y
End If
c.SetBounds(c.Left + iX_Delta, c.Top + iY_Delta, c.Width, c.Height)
'end added
'c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height) 'Original
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Left
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Right
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height)
RaiseEvent ResizeOccurred(c)
'added
mpLast_Location = e.Location
'end added
Case EdgeEnum.Top
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Bottom
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y))
RaiseEvent ResizeOccurred(c)
End Select
c.ResumeLayout()
Else
Select Case True
Case e.X <= (mWidth * 4) And e.Y <= (mWidth * 4) 'top left corner
c.Cursor = Cursors.SizeAll
mEdge = EdgeEnum.TopLeft
Case e.X <= mWidth 'left edge
c.Cursor = Cursors.VSplit
mEdge = EdgeEnum.Left
Case e.X > c.Width - (mWidth + 1) 'right edge
c.Cursor = Cursors.VSplit
mEdge = EdgeEnum.Right
Case e.Y <= mWidth 'top edge
c.Cursor = Cursors.HSplit
mEdge = EdgeEnum.Top
Case e.Y > c.Height - (mWidth + 1) 'bottom edge
c.Cursor = Cursors.HSplit
mEdge = EdgeEnum.Bottom
Case Else 'no edge
c.Cursor = Cursors.Default
mEdge = EdgeEnum.None
End Select
mEdge = mEdge And _AllowEdges
If mEdge = EdgeEnum.None Then c.Cursor = Cursors.Default
End If
End Sub
Private Sub mControl_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
Dim c As Control = CType(sender, Control)
c.Cursor = Cursors.Default
mEdge = EdgeEnum.None
c.Refresh()
End Sub
Public Event ResizeOccurred(ByRef c As System.Windows.Forms.Control)
End Class
|
|
|
|
|
Been looking for something like this for a while. I code in C++/CLR so I had to do some translating from VB to that language. But I got it working. Thanks
|
|
|
|
|
I've come across what I think is a slight problem/snag... If you try to set a resizable control to a control with child controls, getting the mouse on the border is tricky. For example, a Panel control that contains an embedded TextBox. The TextBox's bottom is at the bottom of the Panel (or it's docked to the bottom). When the mouse is over the bottom of the Panel, it'll need to be on the 1-pixel wide bottom border of the Panel for the highlighting/sizing to work. Otherwise the mouse is over the TextBox, the resizing events don't fire, and the resizing cursor doesn't appear.
Does this make sense? Not sure what, if any, workaround there is for this.
Steve
|
|
|
|
|
This class is way cool. It worked perfectly for what I needed -- a series of Infragistics UltraExpandableGroupBox controls docked TOP one on top of another (basically forming a vertical 'table' of group boxes. I wanted each of the group boxes to be resizable.
I did make a few tweaks (new code below). I added AllowEdges and HighlightColor properties. I also added values to the enumeration. This, along with the AllowEdges property enable you to specify which edges can be resized. I needed the bottom only, so this worked well. The HighlightColor property lets you set what color the edge highlight is -- fuschia just didn't do it for me!
Finally, I added a ResizeOccurred event that is thrown during the resizing. I handle it to make sure the control isn't resized past a minimum threshold.
I realize that my implementation of these "extras" is pretty quick and simplistic. Just wanted to share these as I thought you might want to add them (or something like them) to your original class.
Steve
Public Class Form1
Dim rc As ResizeableControl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
rc = New ResizeableControl(pbDemo)
rc.HighlightColor = Color.Orange
rc.AllowEdges = ResizeableControl.EdgeEnum.Bottom
AddHandler rc.ResizeOccurred, AddressOf HandleResize
End Sub
Private Sub HandleResize(ByRef sender As Windows.Forms.Control)
If sender.Height < 50 Then sender.Height = 50
End Sub
Public Class ResizeableControl
Private WithEvents mControl As Control
Private mMouseDown As Boolean = False
Private mEdge As EdgeEnum = EdgeEnum.None
Private mWidth As Integer = 4
Private mOutlineDrawn As Boolean = False
Public Property AllowEdges() As EdgeEnum
Get
Return _AllowEdges
End Get
Set(ByVal value As EdgeEnum)
_AllowEdges = value
End Set
End Property
Friend _AllowEdges As EdgeEnum = EdgeEnum.Bottom Or EdgeEnum.Left Or EdgeEnum.Right Or EdgeEnum.Top Or EdgeEnum.TopLeft
Public Property HighlightColor() As Drawing.Color
Get
Return _HighlightColor
End Get
Set(ByVal value As Drawing.Color)
_HighlightColor = value
End Set
End Property
Private _HighlightColor As Drawing.Color = Color.Fuchsia
Public Enum EdgeEnum
None = 0
Right = 1
Left = 2
Top = 4
Bottom = 8
TopLeft = 16
End Enum
Public Sub New(ByVal Control As Control)
mControl = Control
End Sub
Private Sub mControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
mMouseDown = True
End If
End Sub
Private Sub mControl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
mMouseDown = False
End Sub
Private Sub mControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
Dim c As Control = CType(sender, Control)
Dim g As Graphics = c.CreateGraphics
Dim B As New System.Drawing.SolidBrush(_HighlightColor)
Select Case mEdge
Case EdgeEnum.TopLeft
g.FillRectangle(B, 0, 0, mWidth * 4, mWidth * 4)
mOutlineDrawn = True
Case EdgeEnum.Left
g.FillRectangle(B, 0, 0, mWidth, c.Height)
mOutlineDrawn = True
Case EdgeEnum.Right
g.FillRectangle(B, c.Width - mWidth, 0, c.Width, c.Height)
mOutlineDrawn = True
Case EdgeEnum.Top
g.FillRectangle(B, 0, 0, c.Width, mWidth)
mOutlineDrawn = True
Case EdgeEnum.Bottom
g.FillRectangle(B, 0, c.Height - mWidth, c.Width, mWidth)
mOutlineDrawn = True
Case EdgeEnum.None
If mOutlineDrawn Then
c.Refresh()
mOutlineDrawn = False
End If
End Select
If mMouseDown And mEdge <> EdgeEnum.None Then
c.SuspendLayout()
Select Case mEdge
Case EdgeEnum.TopLeft
c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Left
c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Right
c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Top
c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y)
RaiseEvent ResizeOccurred(c)
Case EdgeEnum.Bottom
c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y))
RaiseEvent ResizeOccurred(c)
End Select
c.ResumeLayout()
Else
Select Case True
Case e.X <= (mWidth * 4) And e.Y <= (mWidth * 4) 'top left corner
c.Cursor = Cursors.SizeAll
mEdge = EdgeEnum.TopLeft
Case e.X <= mWidth 'left edge
c.Cursor = Cursors.VSplit
mEdge = EdgeEnum.Left
Case e.X > c.Width - (mWidth + 1) 'right edge
c.Cursor = Cursors.VSplit
mEdge = EdgeEnum.Right
Case e.Y <= mWidth 'top edge
c.Cursor = Cursors.HSplit
mEdge = EdgeEnum.Top
Case e.Y > c.Height - (mWidth + 1) 'bottom edge
c.Cursor = Cursors.HSplit
mEdge = EdgeEnum.Bottom
Case Else 'no edge
c.Cursor = Cursors.Default
mEdge = EdgeEnum.None
End Select
mEdge = mEdge And _AllowEdges
If mEdge = EdgeEnum.None Then c.Cursor = Cursors.Default
End If
End Sub
Private Sub mControl_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
Dim c As Control = CType(sender, Control)
mEdge = EdgeEnum.None
c.Refresh()
End Sub
Public Event ResizeOccurred(ByRef c As System.Windows.Forms.Control)
End Class
End Class
|
|
|
|
|