你能建议一个简单的绘图程序吗?

时间:2011-10-02 14:21:20

标签: c++-cli

我是C ++的新手。我尝试绘制一个多边形。我的代码可以构建,但是当我运行它时,表单中没有任何内容。有谁能请给我一些帮助。谢谢(下面的大部分内容来自默认的VS模板,我只添加了几行)

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);
        Point 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 );

    }
};
}

1 个答案:

答案 0 :(得分:0)

您忘记订阅Paint事件:

        this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);

单击“属性”窗口中的闪电图标,然后双击“绘制”。正确的方法是覆盖OnPaint()方法。