954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Drawing shapes in Visual C++ 2008

So, I need to make a program that draws a finite automata from the input data.
I had some experience with the graphics.h header but as far as I know it's a Borland only header and VC++ doesn't support it. So I was wondering, is there a way to draw some simple shapes and lines on VC++, besides using OpenGL?

Basteon
Newbie Poster
21 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

Try the "allegro" library. Do a search on YouTube for how to set it up with VC++. There is a good set of tutorials and some example (the examples are all games-based) at:
http://www.cppgameprogramming.com/cgi/nav.cgi?page=allegbasics

Just double click anywhere on the site to bring up the menu :)

SgtMe
Nearly a Posting Virtuoso
1,205 posts since Oct 2009
Reputation Points: 68
Solved Threads: 85
 

Thanks, I'll give it a try. :)

Does anyone know if I can draw on a window (I mean a window forms application)? I remember using a component TDrawingBox in Borland C++, maybe there is a similar one in Visual C++ that I can use.

Basteon
Newbie Poster
21 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

See this: http://www.bobpowell.net/faqmain.htm

Caveat is the examples are in C# and VB.NET, but the usual dot used for namespace in C# changes to C++ :: ,and dot used for member method in C# changes to -> (if the variable in question is a pointer) will apply. Look on MSDN to confirm the syntax if need be.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

I did some more research on the web and found this topic about shapes in Microsoft's MSDN Library " Shapes and Basic Drawing in WPF Overview ". It seems that the only way to draw in a window is by using .Net and XAML. Typical for Microsoft, always making things more complicated than they aleready are.

If someone knows another way of drawing on a Form please respond, I would really apreciate'it.

Basteon
Newbie Poster
21 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

You don't need to do it in WPF (using that with C++ is one of those things that's theoretically possible, but not really doable anyway), the GDI+ stuff works in C++/CLI. I'll try to throw together a sample at some point.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Here's a sample that moves a circle around the window when you press the button.

private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
{
    //get this event from the right hand side of the screen in the form design    
    //  window, click on the the lightening bolt and double click the cell next to 
     //the Paint()
				 
    Graphics ^ g = e->Graphics; //get the graphics handle from the form

    Random ^ rand = gcnew Random(); //random number generator

    g->DrawEllipse(gcnew Pen(Color::Black),
    Rectangle(rand->Next(0,ClientRectangle.Height),rand 
    ->Next(0,ClientRectangle.Width),10,10)); //make a circle in a 10x10 box at some 
//point over the window (box has upper left hand corner coordinate specified).  
//Sometimes it overlaps off the screen, but you get the idea

 //this normally requires a handle to a RectangleF (a rectangle with float coords), but this is an overload.
				 
}

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
   Invalidate(); //forces Paint() event to be called
}

(the lines are too long so it looks like hell, but you get the idea).

See if that gets you started in the right direction and you can start translating the Bob Powell tutorial (or one of your choosing) into C++. That graphics object holds all of the methods (drawellipse, fillellipse,etc.). The graphics object only comes from the Paint method, so you'll have to use Invalidate() in your other methods to control it.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

hi
iam new to c++ i want to draw a circle in c++ using microsoft visual c++.when i use graphics.h its not sopporting.what can i do now please any one help.

sathishraji
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: