2012-03-18 64 views
2

我有一个Panel控件,我需要在它上面画几条线和圆。为什么面板保持不动?C++/CLI为什么这段代码没有画出一条线?

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
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); 
    } 
+0

错误\t \t 1错误C2039: '处置':不是“系统::绘制:: Graphics的 – 2012-03-18 09:21:14

+1

一个构件好吧,删除作品,但面板上仍然没有任何东西。有没有其他方式在面板上绘制东西?我以前的代码可能有误 – 2012-03-18 09:36:00

相关问题