C ++ / CLI为什么这段代码不画线?

时间:2012-03-18 08:50:40

标签: graphics c++-cli drawing panel

我有一个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 too

1 个答案:

答案 0 :(得分:1)

Graphics ^ graphics = panel->CreateGraphics(); 
Pen^ penCurrent = gcnew Pen(Color::Red); 
Point p1(10,10); 
Point p2(20,20); 
graphics->DrawLine(penCurrent,p1,p2); 

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