如何使用按钮单击绘制 - C ++ Windows窗体

时间:2013-02-01 08:29:28

标签: c++ windows forms

在C ++ WIndows Form 2010中,我需要在单击按钮时在Panel中绘制一条线。我知道如何在绘画中绘制一条线但在GO_Click成员中没有。

private: System::Void GO_Click(System::Object^  sender, System::EventArgs^  e) 
{
    m->DrawLine(Pens::Blue, 500, 550, 700, 500);
}

如何在GO_Click成员中使用DrawLine?

1 个答案:

答案 0 :(得分:1)

看看这个样本:

    private: System::Void GO_Click(System::Object^  sender, System::EventArgs^  e) 
    {
        // This works, but the drawn line will be lost when refreshing the panel etc.

        //Graphics^ g = panel1->CreateGraphics();
        //g->DrawLine(System::Drawing::Pens::Blue, 500, 550, 700, 500);

        // This approach draws the line on the BackroungImage of the panel
        if (panel1->BackgroundImage == nullptr)
        {
            panel1->BackgroundImage = gcnew System::Drawing::Bitmap(panel1->Width, panel1->Height);
        }

        Graphics^ buffGraphics = Graphics::FromImage(panel1->BackgroundImage);

        buffGraphics->Clear(panel1->BackColor);
        buffGraphics->DrawLine(System::Drawing::Pens::Blue, 500, 550, 700, 500);

        panel1->Update();
    }

但是,还有更多方法可以画线。