So , I've been obsessing about this for a while , as stupid as it may sound , I don't seem to know where exactly can I declare functions when I'm Drawing in VC++ 2008 , all I did so far was initialize paint Handle , wrote a couple lines in it and a line was drawn , I thought : "fantastic! , now let's do recursions!" , turned out not quite as easy as I imagined , so if anyone would help I'd be grateful.
The function is listed below , just tell me where am I supposed to declare it and if you have some extra time on your hands , how can I declare a header containing it .

void DrawTemplate(int level,float X,float Y,float length,float angle) {
			if (level != 0) {
				float newx;float newy;
				newx = X+cos(angle)*length;
				newy = Y-sin(angle)*length;				
				formGraphics->DrawLine(myPen,X ,Y, newx, newy);				
				X=newx;Y=newy;				
				newx = X+sin(angle)*length;
				newy = Y+cos(angle)*length;
				formGraphics->DrawLine(myPen,X ,Y, newx, newy);				
				X=newx;Y=newy;					
				newx = X-cos(angle)*length;
				newy = Y+sin(angle)*length;
				formGraphics->DrawLine(myPen,X ,Y, newx, newy);				
				X=newx;Y=newy;
				newx = X-sin(angle)*length/2;
				newy = Y-cos(angle)*length/2;
				formGraphics->DrawLine(myPen,X ,Y, newx, newy);
				void DrawTemplate(int level--,newx,newy,length/2,angle-(pi/4));
				X=newx;Y=newy;
				newx = X-sin(angle)*length/2;
				newy = Y-cos(angle)*length/2;
				formGraphics->DrawLine(myPen,X ,Y, newx, newy);				
			}
		}

P.S 1: I'm only a beginner.
P.S 2 : Thanks in advance.
Another P.S : You don't even have to read the code ,just tell me where can I put it and I'd be thankful.

Recommended Answers

All 6 Replies

>>P.S 1: I'm only a beginner
Then you are way way over your head. Learn the language before jumping into the deep water.

>>just tell me where can I put it
Do you really mean that :)

Before you can draw anything on a form you need a Graphics object.
In your code it is named formGraphics. You can't define a Graphics object yourself (the class is sealed) but you can obtain one from the PaintEventArgs class you get from a Paint event handler. Once you have one you can start drawing like hell! But I should also take into consideration the wise words of an Ancient Dragon...

What type is formGraphics? Are you using some kind of image library or something - ie what are you trying to draw on?

Dave

What type is formGraphics?

formGraphics is of type Graphics which in GDI+ is a class which offers a drawing surface to draw on.

I should have elaborated a little more , my bad.
After long googling hours , I started a VC++ windows forms project , double clicked on the paint event ,double clicked on the form and voila,I was able to draw 'I show later in code where I was able to do that " but in the main file , nothing happens , it just says it can't find the 'Graphic' library, might be a little confusing , but the project is named Graphics , notice that I didn't write any of that namespace , it all just appeared when I pressed the form.

#pragma once
#include<math.h>
namespace Graphic {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->SuspendLayout();
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(512, 483);
			this->Cursor = System::Windows::Forms::Cursors::Default;
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
			this->ResumeLayout(false);
		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
			System::Drawing::Pen^ myPen =gcnew System::Drawing::Pen(System::Drawing::Color::Red);
			System::Drawing::Graphics^ formGraphics;
			formGraphics = this->CreateGraphics();				
			//in this place I'm able to write working code :)							
			delete myPen;
			delete formGraphics;
		}
	};
}

So , as you see from the code , this is where I managed to actually draw something , but I need to define functions somewhere to move on , so please again , any suggestions ?
P.S : Is this deep water ? It's the next logical thing in proving my recursive drawing algorithms work , so I guess I should get there by now :p.

Why should you call CreateGraphics? PaintEventArgs^ gives you a Graphics object for free!
formGraphics = e::Graphics(); or something like that.
Some graphic calls also require you to use
using namespace System:: Drawing:: Drawing2D;
Happy drawing!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.