Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Windows Forms C++ Mandelbrot Explorer/Zoom with Julia Walkabout

0.00/5 (No votes)
10 May 2010 1  
A Windows Forms C++ Mandelbrot Explorer/Zoom with Julia walkabout

Introduction

Designing apps in Microsoft Visual C++ 2005 Express Edition is pretty straightforward. Point this, click that, presto! Place it on form. But seriously, click on the desired component (control), located at the left toolbar of Visual C++ 2005 Express Edition, and draw it at an appropriate location on the form.

Components on the form align to rectangular grids, giving your apps a symmetric look. This simple app demonstrates the ease of creating applications and handling mouse events with Windows Forms syntaxes in C++.

Events

Up to this point in time, fast forwarding a bit, we've created a Windows Form, placed controls like buttons and pictureboxes onto the form. To have the button respond to mouse clicks, we have to put some code into it. So we double-click on it and we are presented with an event method. All we have to do is place some code in that control's event method.

Let's click on a picturebox and place relevant code in its MouseDown section, for we have some events to handle on it.

The MouseDown Event

The MouseDown event is called upon when the left button is pressed down. When the user presses down the left mouse button, our application stores the mouse's current x and y coordinates in the variables start.x and start.y. As the user drags the mouse, a rectangle is drawn, it defines the size of the region of the mandelbrot on which to zoom in on.

private: System::Void pictureBox1_MouseDown(System::Object^  sender, 
	System::Windows::Forms::MouseEventArgs^  e) 
{
	switch ( e->Button )
	{
		case System::Windows::Forms::MouseButtons::Left:
		if (mouse_x>=0 && mouse_x<=WIDTH && mouse_y>=0 && mouse_y<=HEIGHT)
		{
			m_show = true;
			sx = mouse_x; sy = mouse_y;ex = mouse_x;ey = mouse_y;
			rect.X=sx; rect.Y=sy;xorEx= sx+100;	xorEy= sy+100;
			endx=0;	endy=0;	startx=mouse_x;	starty=mouse_y;
			zsx=(xmin+(xmax-xmin)*((mousex)-px)/(WIDTH-1));
			zsy=(ymin+(ymax-ymin)*((mousey)-py)/(HEIGHT-1));
			zex=(xmin+(xmax-xmin)*((mousex)-px)/(WIDTH-1));
			zey=(ymin+(ymax-ymin)*((mousey)-py)/(HEIGHT-1));
			tempx=xmin;tempy=ymin;xmin=zsx;	ymin=zsy;displayData();
		}
		break;
		
		case System::Windows::Forms::MouseButtons::Right:
		if (mouse_x<width><height) 
			mousex=""mouse_x;"" mousey=""mouse_y;""></width>

The MouseMove Event

The MouseMove event is called upon when the user drags the mouse. As the mouse is being dragged, a rectangular box is drawn to show where the next zoom region will be.

private: System::Void pictureBox1_MouseMove(System::Object^  sender, 
	System::Windows::Forms::MouseEventArgs^  e) 
{
	mouse_x=e->X;  mouse_y=e->Y;
	if (mouse_x>=0 && mouse_x<=WIDTH && mouse_y>=0 && mouse_y<=HEIGHT)
	{
		if (mouse_x>= sx && mouse_y >= sy )
		{
		ex = mouse_x; ey = mouse_y;	dx=zex-zsx;	dy=zey-zsy;
			if (e->Button==System::Windows::Forms::MouseButtons::Left)
			{
				rect.Width=e->X- sx;  rect.Height=e->Y- sy;
				pictureBox1->Invalidate( rect );
				label1->Text=e->X+"x"+e->Y;
			}
		}

	if ( m_show ){}
	mousex=mouse_x;	mousey=mouse_y;	displayData(); 
		if (e->Button==System::Windows::Forms::MouseButtons::Left) 
		{
			if (zoomcnt<1)pictureBox1->Image=pictureBox3->Image; 
			else pictureBox1->Image=backBuffer;
		}
	}
} 

The MouseUp Event

The MouseUp event is called upon when the left button is released. Once the user releases the left mouse button, our application stores the mouse's current x and y coordinates in the variables end.x and end.y.
Our application now calculates the new interpolants on which to calculate the mandelbrot from.

private: System::Void pictureBox1_MouseUp(System::Object^  sender, 
	System::Windows::Forms::MouseEventArgs^  e) 
{
	switch ( e->Button )
	{
		case System::Windows::Forms::MouseButtons::Left:
		if ((mouse_x>=0) &&( mouse_x<=WIDTH) && 
		(mouse_y>=0) && (mouse_y<=HEIGHT) && (ex-sx>2) && (ey-sy>2))
		{
			m_show = false;zoomcnt++;
			endx=mouse_x;endy=mouse_y;ex =mousex; ey =mousey;
			rect.Width=(ex-sx); rect.Height=(ey-sy);
			if (endx >startx+5 && endy >starty +5)
			{
			 initWalk(); MouseX=WIDTH/2; 
				MouseY=HEIGHT/2;dx=zex-zsx;dy=zey-zsy;
				 if (mouse_x>= sx && mouse_y >= sy )
				 {
				 zex=(xmin+(xmax-xmin)*((mousex)-px)/(WIDTH-1));
				 zey=(ymin+(ymax-ymin)*((mousey)-py)/(HEIGHT-1));
				 xmax=zex; ymax=zey; dx=zex-zsx;dy=zey-zsy;
				 displayData(); calculateFractal(/*hdc*/); 
					putCursor(); walkabout();
				}
			} //else{pictureBox1->Invalidate( rect );}
		}

		break;
		
		case System::Windows::Forms::MouseButtons::Right:
		walkabout();
		break;
	}
} 

And that is how easy it is to create applications and handle mouse events with Windows Forms syntaxes in C++.

Thanks for reading.

History

  • 2010-05-08 Updates made
  • 2005-11-28 Code complete

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