Click here to Skip to main content
15,879,096 members
Articles / Programming Languages / C#
Article

A Resizable Graphical Rectangle

Rate me:
Please Sign up or sign in to vote.
4.83/5 (30 votes)
4 Nov 2008CPOL2 min read 145.1K   11.9K   69   37
An object associated with a pictureBox managing callback to manipulate the rectangle area.
IMG102.jpg

Introduction 

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.)

Background 

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:

C#
public Form1()
{
     InitializeComponent();
     rect = new UserRect(new Rectangle(10, 10, 100, 100));
     rect.SetPictureBox(this.pictureBox1);
}

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

C#
public class UserRect 
    {        
        public Rectangle rect;

The public functions are:

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

The private functions are:

Events adding to PictureBox:

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

Functions treat handles, and modification of cursor:

  • C#
    private Rectangle CreateRectSizableNode(int x, int y)
  • C#
    private Rectangle GetRect(PosSizableRect p); 
  • C#
    private PosSizableRect GetNodeSelectable(Point p)
  • C#
    private void ChangeCursor(Point p)
  • C#
    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.

History

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.

License

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


Written By
Software Developer (Senior) http://www.cmb-soft.com/
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to deform? Pin
Southmountain31-May-22 10:43
Southmountain31-May-22 10:43 
QuestionRegarding draw multiple rectangles Pin
Member 1359441419-Mar-18 0:52
Member 1359441419-Mar-18 0:52 
QuestionHow to remove the small Position Size Rectangle in all Corner of Rectangle? Pin
Ryan Ayala5-Mar-16 5:27
Ryan Ayala5-Mar-16 5:27 
AnswerRe: How to remove the small Position Size Rectangle in all Corner of Rectangle? Pin
zebulon7501817-Sep-16 6:04
zebulon7501817-Sep-16 6:04 
GeneralMy vote of 5 Pin
Bruno Alvarez2-Mar-16 15:34
Bruno Alvarez2-Mar-16 15:34 
QuestionCrop Pin
Member 1118208510-May-15 22:56
Member 1118208510-May-15 22:56 
QuestionCan you convert this project to VB.Net Pin
TienVung17-Mar-14 21:07
TienVung17-Mar-14 21:07 
AnswerRe: Can you convert this project to VB.Net Pin
N. Henrik Lauridsen29-May-17 8:01
N. Henrik Lauridsen29-May-17 8:01 
QuestionAspect Ratio Pin
gaiboten16-Jun-13 1:54
gaiboten16-Jun-13 1:54 
how to make AspectRatio? thanks!
QuestionDeletion Function Pin
Member 848494525-Apr-13 5:35
Member 848494525-Apr-13 5:35 
QuestionHi Pin
ali_motamed27-Mar-13 9:37
ali_motamed27-Mar-13 9:37 
QuestionHow to cropping image using this article Pin
Ramesha Jogihalli21-Jun-12 2:11
Ramesha Jogihalli21-Jun-12 2:11 
QuestionA Small Doubt.. Pin
PankajMishra20-Apr-12 8:18
PankajMishra20-Apr-12 8:18 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:10
professionalManoj Kumar Choubey26-Feb-12 21:10 
QuestionRemove rectangle Pin
shivamshukla22@gmail.com4-Dec-11 21:01
shivamshukla22@gmail.com4-Dec-11 21:01 
AnswerRe: Remove rectangle Pin
paijopkl29-Oct-14 18:18
paijopkl29-Oct-14 18:18 
QuestionRe: Remove rectangle Pin
Ali Alshihry19-Feb-20 23:57
Ali Alshihry19-Feb-20 23:57 
Questionresizable rectangle in java Pin
ajay17may2-Aug-10 7:48
ajay17may2-Aug-10 7:48 
GeneralRemove Rectangles Pin
abhi_261012-May-10 13:11
abhi_261012-May-10 13:11 
QuestionCan we add more than one rectangle on the same form? [modified] Pin
wonzery7-Feb-10 21:55
wonzery7-Feb-10 21:55 
GeneralExtra functions added, No black area bug and some autoposition methods Pin
Job V9-Sep-09 4:14
Job V9-Sep-09 4:14 
GeneralSuggested changes to code [modified] Pin
scottpj28-Oct-08 6:55
scottpj28-Oct-08 6:55 
GeneralRe: Suggested changes to code Pin
zebulon7501828-Oct-08 13:47
zebulon7501828-Oct-08 13:47 
GeneralRe: Suggested changes to code Pin
Christian Wikander14-Nov-08 2:28
Christian Wikander14-Nov-08 2:28 
GeneralRe: Suggested changes to code Pin
scottpj15-Nov-08 17:38
scottpj15-Nov-08 17:38 

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.