Re-sizing and Re-positioning - Part 2

Download the demo code (Visual Studio.Net Project)
Part 1 | Part 2

Implementation - Handle Class

The Handle class is very simple and records the X orientation of the handle - Left, Middle or Right, and the Y orientation - Top, Middle or Bottom. 8 combinations are possible - the only potential combination that isn't allowable is X = Middle and Y = Middle.

It also records the actual X,Y co-ordinates that specifiy the position of the handle. And it provides a function that allows the HandleManager to determine what cursor should be shown when the mouse is hovering over it.

Implementation - HandleManager Class

The HandleManager is the workhorse. It maintains a collection of the Handles. It processes what to do when the mouse is moving over, or clicking on, the managed area, and the re-sizing or re-positioning as the mouse is dragged. It also calculates the positions and dimensions of the Handles, and draws them.

Implementation - Event Hooks

This is the means by which you connect the HandleManager into your application after you have set a reference to its assembly. You will need to add a boolean flag to track when a re-size/re-position action is occurring. And you will need to create an instance of a HandleManager. Its constructor accepts a Rectangle object that represents the position and dimensions of the object that you want to add the selecting/re-sizing/re-positioning capability to.

MouseDown

If the click is on the object or one of its handles, set the module level boolean variable to flag that a re-size/re-position is underway.

MouseMove

If a re-size/re-position is underway, then process the change according to the latest delta in mouse position. Otherwise, check whether the mouse position is on the object, or one of its handles, and if so, determine which cursor should be displaying.

MouseUp

Tidy up if a re-size/re-position has just finished.

Paint

If there is no re-sizing/re-position underway, then draw the handles and the selection border. This is simply a matter of called the HandleManager's DrawHandles method, passing in e.Graphics - where e is of type PaintEventArgs.

Conclusion

What we have seen is an easy way to add object selecting, re-sizing and re-positioning functionality to your VB.NET WinForms application at runtime.

Part 1 | Part 2