Click here to Skip to main content
6,822,613 members and growing! (20,288 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics License: The Code Project Open License (CPOL)

A Resizable Graphical Rectangle

By zebulon75018

An object associated with a pictureBox managing callback to manipulate the rectangle area.
C#, Windows, Dev
Posted:25 Oct 2008
Updated:4 Nov 2008
Views:12,514
Bookmarked:40 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 4.93 Rating: 4.42 out of 5
1 vote, 7.7%
1

2

3
4 votes, 30.8%
4
8 votes, 61.5%
5
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:

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:  

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.

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)

About the Author

zebulon75018


Member

Occupation: Software Developer (Senior)
Company: http://www.cmb-soft.com/
Location: France France

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralCan we add more than one rectangle on the same form? [modified] Pinmemberwonzery22:55 7 Feb '10  
GeneralExtra functions added, No black area bug and some autoposition methods PinmemberMaxxi865:14 9 Sep '09  
GeneralSuggested changes to code [modified] PinmemberMember 46034057:55 28 Oct '08  
GeneralRe: Suggested changes to code Pinmemberzebulon7501814:47 28 Oct '08  
GeneralRe: Suggested changes to code PinmemberChristian Wikander3:28 14 Nov '08  
GeneralRe: Suggested changes to code Pinmemberscottpj18:38 15 Nov '08  
GeneralRe: Suggested changes to code PinmemberChristian Wikander21:53 16 Nov '08  
GeneralBugs PinmembernetJP12L6:22 28 Oct '08  
GeneralRe: Bugs Pinmemberzebulon7501815:03 28 Oct '08  
GeneralRe: Bugs PinmembernetJP12L19:03 28 Oct '08  
GeneralRe: Bugs Pinmemberzebulon750186:52 29 Oct '08  
GeneralI can't change the last screenshot , there's a checkbox ( allow deformation ). Pinmemberzebulon7501815:50 27 Oct '08  
GeneralRe: I can't change the last screenshot , there's a checkbox ( allow deformation ). PinmemberIlíon2:16 28 Oct '08  
GeneralRe: I can't change the last screenshot , there's a checkbox ( allow deformation ). Pinmemberzebulon7501814:38 28 Oct '08  
GeneralRe: I can't change the last screenshot , there's a checkbox ( allow deformation ). PinmemberIlíon16:15 28 Oct '08  
GeneralRe: I can't change the last screenshot , there's a checkbox ( allow deformation ). PinmemberIlíon2:29 28 Oct '08  
GeneralRe: I can't change the last screenshot , there's a checkbox ( allow deformation ). Pinmemberzebulon7501814:58 28 Oct '08  
GeneralA suggestion PinmemberIlíon8:29 27 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 4 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by zebulon75018
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project