Click here to Skip to main content
Click here to Skip to main content

SelectionForm

, 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

Introduction

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
{
public:
  Form1(void)
  {
    InitializeComponent();
    //
    //TODO: Add the constructor code here
    //
    this->Paint += gcnew 
      System::Windows::Forms::PaintEventHandler(this, 
      &SelectionForm::_Paint);
    // 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();
    System::Windows::Forms::MessageBox::Show(rect->ToString());
    this->clearSelection();
  } 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.

License

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

Share

About the Author

Jan Kuiken

Netherlands Netherlands
No Biography provided

Comments and Discussions

 
QuestionMDI parent form PinmemberDirA28-Jan-06 4:19 
GeneralRe: MDI parent form PinmemberJan Kuiken31-Jan-06 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.141015.1 | Last Updated 26 Jan 2006
Article Copyright 2006 by Jan Kuiken
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid