2012-03-18 98 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

相關問題