0

在 C++ WINdows Form 2010 中,单击按钮时,我需要在面板内画一条线。我知道如何在油漆中画一条线,但在GO_Click成员中不知道。

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

如何在 GO_Click 成员中使用 DrawLine?

4

1 回答 1

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();
    }

但是,有更多的方法来画线。

于 2013-02-01T09:44:09.170 回答