|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWhile 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 Using the codeAdd the #include "SelectionForm.h"
Derive your form class from //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 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 The class has some logic to keep track of the selection and the appearance of the mouse pointer. Points of InterestAnnoying: 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||