Graphics C++/CLI为什么此代码不划一条线?

Graphics C++/CLI为什么此代码不划一条线?,graphics,c++-cli,drawing,panel,Graphics,C++ Cli,Drawing,Panel,我有一个面板控件,我需要在上面画一些线和圆。为什么面板保持不变 Graphics ^ graphics = panel->CreateGraphics(); Pen^ penCurrent = gcnew Pen(Color::Red); Point p1(10,10); Point p2(20,20); graphics->DrawLine(penCurrent,p1,p2); //panel->Invalidate(); //tried this to refresh to

我有一个面板控件,我需要在上面画一些线和圆。为什么面板保持不变

Graphics ^ graphics = panel->CreateGraphics();
Pen^ penCurrent = gcnew Pen(Color::Red);
Point p1(10,10);
Point p2(20,20);
graphics->DrawLine(penCurrent,p1,p2);
//panel->Invalidate(); //tried this to refresh too
下一个完整的示例是工作和绘制线

    #include "windows.h" 

    #using <mscorlib.dll> 
    #using <System.dll> 
    #using <System.Windows.Forms.dll> 
    #using <System.Drawing.dll> 

    using namespace System::Windows::Forms; 
    using namespace System;
    using namespace System::Drawing;


    ref class MyForm : public Form 
    { 

        public: 

        MyForm() 
        {  

             Text = "Hello, Windows Forms!"; 

             auto button = gcnew Button();  
             button->Text = "Click Me!"; 

             button->Click += gcnew EventHandler(this, &MyForm::button_click); 

             this->Controls->Add(button);  
        } 



        void button_click(Object^ sender, EventArgs^ e) 
        { 

            auto g = this->CreateGraphics();

            g->DrawLine(Pens::Black, Point(10, 10), Point(50, 50));

            delete g;
        } 

    }; 


    int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)  
    { 
      Application::Run(gcnew MyForm);  
    } 
#包括“windows.h”
#使用
#使用
#使用
#使用
使用命名空间System::Windows::Forms;
使用名称空间系统;
使用名称空间系统::绘图;
参考类MyForm:公共表单
{ 
公众:
MyForm()
{  
Text=“您好,Windows窗体!”;
自动按钮=gcnew按钮();
按钮->文本=“单击我!”;
按钮->单击+=gcneweventhandler(这个,&MyForm::按钮单击);
此->控制->添加(按钮);
} 
无效按钮\单击(对象^sender,事件参数^e)
{ 
auto g=此->CreateGraphics();
g->DrawLine(画笔:黑色,点(10,10),点(50,50));
删除g;
} 
}; 
int WinMain(HINSTANCE HINSTANCE、HINSTANCE hPrevInstance、LPSTR lpCmdLine、int nCmdShow)
{ 
应用程序::运行(gcnewmyform);
} 

Error 1 Error C2039:“Dispose”:不是“System::Drawing::Graphics”的成员确定,删除作品,但面板上仍然没有任何内容。有没有其他方法在面板上画东西?我以前的代码可能有误。。
    #include "windows.h" 

    #using <mscorlib.dll> 
    #using <System.dll> 
    #using <System.Windows.Forms.dll> 
    #using <System.Drawing.dll> 

    using namespace System::Windows::Forms; 
    using namespace System;
    using namespace System::Drawing;


    ref class MyForm : public Form 
    { 

        public: 

        MyForm() 
        {  

             Text = "Hello, Windows Forms!"; 

             auto button = gcnew Button();  
             button->Text = "Click Me!"; 

             button->Click += gcnew EventHandler(this, &MyForm::button_click); 

             this->Controls->Add(button);  
        } 



        void button_click(Object^ sender, EventArgs^ e) 
        { 

            auto g = this->CreateGraphics();

            g->DrawLine(Pens::Black, Point(10, 10), Point(50, 50));

            delete g;
        } 

    }; 


    int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)  
    { 
      Application::Run(gcnew MyForm);  
    }