0

我在pictureBox中创建了一些矩形,我想通过单击另一个按钮来删除其中一个,而不影响其他矩形。我使用“g->DrawRectangle()”来绘制一个矩形,但我不能删除其中一个。我尝试了pitureBox1->Refresh(),但它删除了我所有的矩形。我只想删除其中一个。

我该怎么做?这是代码:

private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
{
    int x;
    int y;
    int length;
    int width;
    Color b; 

    Graphics^ g = pictureBox1->CreateGraphics();

    b = Color::FromArgb(Convert::ToInt32(textBox7->Text),Convert::ToInt32(textBox8->Text),Convert::ToInt32(textBox9->Text));
    Pen^ myPen = gcnew Pen(b);
    myPen->Width = 2.0F;

    x = Convert::ToInt32(textBox10->Text);
    y = Convert::ToInt32(textBox13->Text);
    length = Convert::ToInt32(textBox11->Text);
    width = Convert::ToInt32(textBox12->Text);

    //Rectangle 
    hh = Rectangle(x, y, length, width);    
    g->DrawRectangle(myPen,hh);

}

4

1 回答 1

1

You need to keep a list of rectangle to draw, to delete one simply stop drawing it.

Example:

std::vector<Rectangle> rectangles;

void YourButtonClick(...){
  // do your stuff
  hh = Rectangle(x, y, length, width);
  rectangles.push_back(hh);

  draw();
}

void draw()
{
  pictureBox1->Refresh() 
  Graphics^ g = pictureBox1->CreateGraphics();
  for(int i = 0, max = rectangles.size(); i<max; i++){
    g->DrawRectangle(pen, rectangles[i]);
  }
}

void deleteRectangle(int index){
  Rectangle* rect = rectangles[index];
  rectangles.erase(rectangles.begin()+index);

  draw();
}
于 2013-05-02T03:32:12.530 回答