{"id":527,"date":"2011-01-06T20:16:14","date_gmt":"2011-01-06T20:16:14","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=527"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"graphics-general","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/graphics\/graphics-general","title":{"rendered":"Graphics General"},"content":{"rendered":"<p>GDI+ is the graphical library used in the .NET Framework.<\/p>\n<h4>Good Resources<\/h4>\n<p>http:\/\/www.functionx.com\/vccli\/index.htm<\/p>\n<h4>How To Draw Graphics On A Form<\/h4>\n<p>Include the namespace:<\/p>\n<pre><code>\tSystem::Drawing<\/code><\/pre>\n<h5>Add the following to the forms constructor<\/h5>\n<pre><code>\r\n\tbmpDrawingArea   = gcnew Bitmap(Width, Height);\r\n\tgraphDrawingArea = Graphics::FromImage(bmpDrawingArea);\r\n<\/code><\/pre>\n<h5>Then<\/h5>\n<p>Create the forms paint event (select form &gt; select the properties lighting bolt icon &gt; double click event) and add:<\/p>\n<pre><code>\r\n\te-&gt;Graphics-&gt;DrawImage(bmpDrawingArea, 0, 0);\r\n<\/code><\/pre>\n<p>Select the form and set is DoubleBuffered property to true, to avoid blinking when its updated<\/p>\n<p>Then in the function that you want to draw all the graphics bits in use this at the beginning:<\/p>\n<pre><code>\tgraphDrawingArea-&gt;Clear(this-&gt;BackColor);<\/code><\/pre>\n<p>Then do all your drawing bits and bobs<br \/>\nThen use this to cause the form to be updated<\/p>\n<pre><code>\tInvalidate();\t\t\/\/Trigger the paint event<\/code><\/pre>\n<h4>Getting a Graphic Object<\/h4>\n<p>Every Windows control automatically inherits a method called CreateGraphics(), which gives you access to the graphic part of a control.  The CreateGraphics() method returns the Graphics object of the variable you call it from. An example of getting the Graphics object of a form:<\/p>\n<pre><code>\r\nSystem::Void button1_Click(System::Object ^  sender,\r\n\t\t\t   System::EventArgs ^  e)\r\n{\r\n    Graphics ^ graph = this-&gt;CreateGraphics();\r\n<\/code><\/pre>\n<p>Another technique is to call the Graphics::FromHwnd() static method. Its syntax is:<\/p>\n<pre><code>\r\nSystem::Void button1_Click(System::Object ^  sender,\r\n\t\t\t   System::EventArgs ^  e)\r\n{\r\n    Graphics ^ graph = Graphics::FromHwnd(this-&gt;Handle);\r\n<\/code><\/pre>\n<h4>Stopping blinking of the form<\/h4>\n<p>Select the form and set its DoubleBuffered property to true.<\/p>\n<h4>Pen Colour<\/h4>\n<p>Specifying by name<\/p>\n<pre><code>\tBackColor = Color::Turquoise;<\/code><\/pre>\n<p>Specifying from RGB values<\/p>\n<pre><code>\tBackColor = Color::FromArgb(26, 69, 174);<\/code><\/pre>\n<p>Creating a variable to hold colour<\/p>\n<pre><code>\tColor clrBlue = Color::Blue;<\/code><\/pre>\n<h4>Solid Brush Colour<\/h4>\n<pre><code>\r\nSolidBrush^ lightColour = gcnew SolidBrush(Color::Lime);\r\nlightColour-&gt;Color = Color::FromArgb(5, 10, 20);\r\n<\/code><\/pre>\n<h4>Pen<\/h4>\n<p>The most basic graphic tool<\/p>\n<pre><code>    Pen ^ penRed = gcnew Pen(Color::Blue);<\/code><\/pre>\n<h5>Defining a pen to use to change colour<\/h5>\n<pre><code>\r\n\tPen ^ penColour = gcnew Pen(Color::Blue);\r\n\tpenColour-&gt;Color = Color::Red;\r\n<\/code><\/pre>\n<h4>Drawing Rectangles<\/h4>\n<h5>Declare two variables as follows<\/h5>\n<pre><code>\r\nprivate:\r\n\t\t\/\/\/ &lt;summary&gt;\r\n\t\t\/\/\/ Required designer variable.\r\n\t\t\/\/\/ &lt;\/summary&gt;\r\n\t\tGraphics^ graphDrawingArea;\r\n\t\tBitmap^ bmpDrawingArea;\r\n<\/code><\/pre>\n<h5>In the form load event<\/h5>\n<pre><code>\r\n\tbmpDrawingArea   = gcnew Bitmap(Width, Height);\r\n\tgraphDrawingArea = Graphics::FromImage(bmpDrawingArea);\r\n<\/code><\/pre>\n<h5>Create the paint event for the form and put this in it<\/h5>\n<pre><code>\r\n\te-&gt;Graphics-&gt;DrawImage(bmpDrawingArea, 0, 0);\r\n<\/code><\/pre>\n<h5>Then to draw rectangles use the following<\/h5>\n<pre><code>\r\n \tgraphDrawingArea-&gt;Clear(this-&gt;BackColor);\r\n\r\n\tgraphDrawingArea-&gt;DrawRectangle(gcnew Pen(Color::Red), 20, 20, 40, 60);\t\/\/colour, x, y, width, height - x,y is top left corner\r\n\r\n\tInvalidate();\t\t\/\/Trigger the paint event\r\n\r\n\t\/\/or use\r\n\tgraphDrawingArea-&gt;FillRectangle(gcnew SolidBrush(Color::Lime), 20, 20, 40, 60);\t\/\/colour, x, y, width, height - x,y is top left corner\r\n<\/code><\/pre>\n<h4>Drawing Elipses \/ Circles<\/h4>\n<p>Note that the X and Y coords define the top left corner of a rectangle that encloses the elipse.<\/p>\n<pre><code>\r\n\tgraphDrawingArea-&gt;DrawEllipse(gcnew Pen(Color::Red), Rectangle(\r\n\t\t\t\t\ttargets_start_x + (targets_width &gt;&gt; 1) + x_coord,\r\n\t\t\t\t\ttargets_start_y + y_coord,\r\n\t\t\t\t\ttarget_diameter,\r\n\t\t\t\t\ttarget_diameter\r\n\t\t\t\t\t));\t\/\/colour, x, y, width, height - x,y is top left corner\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GDI+ is the graphical library used in the .NET Framework. Good Resources http:\/\/www.functionx.com\/vccli\/index.htm How To Draw Graphics On A Form Include the namespace: System::Drawing Add the following to the forms constructor bmpDrawingArea = gcnew Bitmap(Width, Height); graphDrawingArea = Graphics::FromImage(bmpDrawingArea); Then Create the forms paint event (select form &gt; select the properties lighting bolt icon &gt; [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-graphics"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":1060,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/527\/revisions\/1060"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}