I am new to C++. I try to draw a polygon. My code can be built, but nothing shows in the form when I run it. Could anyone please give me some help. Thanks (Most of the stuff below was from the default VS template, I added a few lines only)
namespace DrawMyShape {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
System::ComponentModel::Container ^components;
void InitializeComponent(void)
{
this->components = gcnew System::ComponentModel::Container();
this->Size = System::Drawing::Size(800,800);
this->Text = L"Form1";
this->Padding = System::Windows::Forms::Padding(0);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
}
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
Graphics ^g = e->Graphics; //require for drawing
SolidBrush^ blackPen = gcnew SolidBrush( Color::Blue );
// Create points that define polygon.
Point point1 = Point(50,50);
Point point2 = Point(100,25);
Point point3 = Point(200,5);
Point point4 = Point(250,50);
Point point5 = Point(300,100);
Po开发者_如何学Cint point6 = Point(350,200);
Point point7 = Point(250,250);
array<Point>^ curvePoints = {point1,point2,point3,point4,point5,point6,point7};
// Draw polygon to screen.
e->Graphics->FillPolygon( blackPen, curvePoints );
}
};
}
You forgot to subscribe the Paint event:
this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
Click the lightning bolt icon in the Properties window and double-click Paint. The proper way to do this is to override the OnPaint() method.
精彩评论