Click here to Skip to main content
6,595,444 members and growing! (17,029 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Windows Forms     Intermediate

SelectionForm

By Jan Kuiken

Using a class derived from System::Windows::Forms::Form to enable region selection of a form.
C++/CLI.NET 2.0, WinXP, WinForms, VS2005, Dev
Posted:26 Jan 2006
Views:15,605
Bookmarked:10 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.52 Rating: 4.19 out of 5

1
1 vote, 25.0%
2

3
1 vote, 25.0%
4
2 votes, 50.0%
5

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

About the Author

Jan Kuiken


Member

Location: Netherlands Netherlands

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
QuestionMDI parent form PinmemberDirA5:19 28 Jan '06  
GeneralRe: MDI parent form PinmemberJan Kuiken7:33 31 Jan '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jan 2006
Editor: Smitha Vijayan
Copyright 2006 by Jan Kuiken
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project