Click here to Skip to main content
12,622,675 members (29,050 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked


, 26 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Using a class derived from System::Windows::Forms::Form to enable region selection of a form.

SelectionForm in Mandelbrot program


While writing a program that displays Mandelbrot images, I wrote some code to make it possible to select a region on a window form for zooming into the Mandelbrot image.

While doing so, I realized that this might be useful in other programs, and I split the region selection code off and put it into a class called SelectionForm which is derived from System::Windows::Forms::Form. The new class adds region selection functionality to the Form class, and is pretty easy to use.

Using the code

Add the SelectionForm .cpp and .h files into your C++ Windows Forms CLR project. Include SelectionForm.h in your Form1.h file.

#include "SelectionForm.h"

Derive your form class from SelectionForm instead of System::Windows::Forms::Form and do some initialization in the constructor of your form class.

//public ref class Form1 : 
//     public System::Windows::Forms::Form
public ref class Form1 : public SelectionForm
    //TODO: Add the constructor code here
    this->Paint += gcnew 
    // skip for no fixed aspectratio
    SelectionAspectRatio = 1.0f;
    // avoid flicker
    this->DoubleBuffered = true;

Users can use the mouse to create, resize, or move a region selection on your form. Your derived class can use the functions hasSelection, getSelection, and clearSelection to get information about the region selection, as in the following example of a button click handler function:

private: System::Void button1_Click(System::Object^  sender, 
                                    System::EventArgs^  e) {
  if (this->hasSelection()) {
    System::Drawing::Rectangle^rect = this->getSelection();
  } else {
    System::Windows::Forms::MessageBox::Show("No Selection");

How does it work?

The derived class SelectionForm has handlers for the mouse up, down, and move events. It also has a handler for the paint event. The mouse events are added to the form's eventshandler list in the constructor of SelectionForm. The Paint eventhandler must be added to the eventhandlerlist after the derived class' own Paint handler, otherwise the painting occurs in the wrong order.

The class has some logic to keep track of the selection and the appearance of the mouse pointer.

Points of Interest

Annoying: Sometimes the IDE get confused when showing the [Design] view of your form because it is derived from something else then the standard form. Rebuilding the solution solves this problem.

Note: the demo application zooms to the selection by hitting the Enter-key, and zooms out again by hitting the Escape-key.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jan Kuiken
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionMDI parent form Pin
DirA28-Jan-06 5:19
memberDirA28-Jan-06 5:19 
GeneralRe: MDI parent form Pin
Jan Kuiken31-Jan-06 7:33
memberJan Kuiken31-Jan-06 7:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 26 Jan 2006
Article Copyright 2006 by Jan Kuiken
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid