Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms

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

Rate me:
Please Sign up or sign in to vote.
4.09/5 (5 votes)
10 May 2010CPOL2 min read 24.5K   453   6   2
A Windows Forms C++ Mandelbrot Explorer/Zoom with Julia walkabout
Image 1

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.

C++
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.

C++
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sweden Sweden
About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions

 
GeneralC++/CLI Pin
TobiasP2-Dec-10 2:28
TobiasP2-Dec-10 2:28 
GeneralRe: C++/CLI Pin
.:floyd:.2-Aug-16 14:01
.:floyd:.2-Aug-16 14:01 

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.