Click here to Skip to main content
13,406,109 members (46,377 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


64 bookmarked
Posted 25 Oct 2008

A Resizable Graphical Rectangle

, 4 Nov 2008
Rate this:
Please Sign up or sign in to vote.
An object associated with a pictureBox managing callback to manipulate the rectangle area.


This article wants to introduce a Resizable graphical Rectangle (UserRect) which can be resized and moved.  

It's really simple to use.   

This object is built with a Rectangle, and is associated with a paintbox.
It shows 8 handles to be resized. If the user clicks on one of these, then s/he can resize the rectangle.
If the user clicks inside the rectangle, then s/he can move the rectangle into the paintbox.

(Sorry for my English, I'm French, but I want to share this with you, and donate it to this great website.)


In a graphical application, the user needs a rectangular graphical zone for a selection.

The features of this object are as follows: 

  • Shows the handle to resize the rectangle
  • Shows the cursor when the user moves the mouse on the handle
  • Reacts to the move event

It adds callback event to a pictureBox and treats a mouse event.

Using the Code  

To use the object, create a Windows project, add a pictureBox to the main form, and construct the UserRect:

public Form1()
     rect = new UserRect(new Rectangle(10, 10, 100, 100));

If you want to access the new Rectangle value, the UserRect gets a public rect variable:  

public class UserRect 
        public Rectangle rect;

The public functions are:

  • public void Draw(Graphics g); 
  • public void SetPictureBox(PictureBox p); 

The private functions are:

Events adding to PictureBox:

  • private void mPictureBox_MouseDown(object sender, MouseEventArgs e); 
  • private void mPictureBox_MouseUp(object sender, MouseEventArgs e); 
  • private void mPictureBox_MouseMove(object sender, MouseEventArgs e);

Functions treat handles, and modification of cursor:

  • private Rectangle CreateRectSizableNode(int x, int y)
  • private Rectangle GetRect(PosSizableRect p); 
  • private PosSizableRect GetNodeSelectable(Point p)
  • private void ChangeCursor(Point p)
  • private Cursor GetCursor(PosSizableRect p)

Points of Interest

There's one interesting thing. This object associates and treats events of pictureBox alone. You don't need to have all events in your mainform class. There's something missing, if you delete the object then the events are not to be deleted. Object UserRect gets a new variable allowDeformingDuringMovement. If it's true, then when you move the rectangle, you can deform the rect's size on the board.


There's a point I want to add that it's a rotation manipulation. 

I updated the source, and now the rectangle is always in control.  

I add a checkbox to allow or not the deformation when the rectangle goes out of the box.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
France France
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to remove the small Position Size Rectangle in all Corner of Rectangle? Pin
Ryan Ayala5-Mar-16 6:27
memberRyan Ayala5-Mar-16 6:27 
AnswerRe: How to remove the small Position Size Rectangle in all Corner of Rectangle? Pin
zebulon7501817-Sep-16 7:04
memberzebulon7501817-Sep-16 7:04 
GeneralMy vote of 5 Pin
Bruno Alvarez2-Mar-16 16:34
memberBruno Alvarez2-Mar-16 16:34 
QuestionCrop Pin
Member 1118208510-May-15 23:56
memberMember 1118208510-May-15 23:56 
QuestionCan you convert this project to VB.Net Pin
TienVung17-Mar-14 22:07
memberTienVung17-Mar-14 22:07 
AnswerRe: Can you convert this project to VB.Net Pin
N. Henrik Lauridsen29-May-17 9:01
memberN. Henrik Lauridsen29-May-17 9:01 
QuestionAspect Ratio Pin
gaiboten16-Jun-13 2:54
membergaiboten16-Jun-13 2:54 
QuestionDeletion Function Pin
Member 848494525-Apr-13 6:35
memberMember 848494525-Apr-13 6:35 
QuestionHi Pin
ali_motamed27-Mar-13 10:37
memberali_motamed27-Mar-13 10:37 
QuestionHow to cropping image using this article Pin
Ramesha Jogihalli21-Jun-12 3:11
memberRamesha Jogihalli21-Jun-12 3:11 
QuestionA Small Doubt.. Pin
PankajMishra20-Apr-12 9:18
memberPankajMishra20-Apr-12 9:18 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 22:10
membermanoj kumar choubey26-Feb-12 22:10 
QuestionRemove rectangle Pin
shivamshukla22@gmail.com4-Dec-11 22:01
membershivamshukla22@gmail.com4-Dec-11 22:01 
AnswerRe: Remove rectangle Pin
paijopkl29-Oct-14 19:18
memberpaijopkl29-Oct-14 19:18 
Questionresizable rectangle in java Pin
ajay17may2-Aug-10 8:48
memberajay17may2-Aug-10 8:48 
GeneralRemove Rectangles Pin
abhi_261012-May-10 14:11
memberabhi_261012-May-10 14:11 
QuestionCan we add more than one rectangle on the same form? [modified] Pin
wonzery7-Feb-10 22:55
memberwonzery7-Feb-10 22:55 
GeneralExtra functions added, No black area bug and some autoposition methods Pin
Maxxi869-Sep-09 5:14
memberMaxxi869-Sep-09 5:14 
GeneralSuggested changes to code [modified] Pin
Member 460340528-Oct-08 7:55
memberMember 460340528-Oct-08 7:55 
GeneralRe: Suggested changes to code Pin
zebulon7501828-Oct-08 14:47
memberzebulon7501828-Oct-08 14:47 
GeneralRe: Suggested changes to code Pin
Christian Wikander14-Nov-08 3:28
memberChristian Wikander14-Nov-08 3:28 
GeneralRe: Suggested changes to code Pin
scottpj15-Nov-08 18:38
memberscottpj15-Nov-08 18:38 
GeneralRe: Suggested changes to code Pin
Christian Wikander16-Nov-08 21:53
memberChristian Wikander16-Nov-08 21:53 
GeneralBugs Pin
netJP12L28-Oct-08 6:22
membernetJP12L28-Oct-08 6:22 
GeneralRe: Bugs Pin
zebulon7501828-Oct-08 15:03
memberzebulon7501828-Oct-08 15:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180212.1 | Last Updated 4 Nov 2008
Article Copyright 2008 by zebulon75018
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid