2011-09-12 4 views
0

그림 상자에 무언가를 그려 제거하고 싶습니다. 이 경우 필자는 채우기 원을 그려야하고 시간이 지나면 그것을 제거하고 채우지 않고 원을 그립니다.그림 상자는 C#의 메서드를 무효화합니다.

아래 코드를 사용하지만 도형을 제거하고 싶을 때 새로운 모양으로 다른 시간에 picturebox를 그릴 필요가 있고 box.now에서 invalidate() 메서드를 사용해야한다는 것을 알고 있습니다. 하지만 어디서 어떻게 사용해야하는지 모르겠다.

void pbmapDo() 
    { 
     Graphics graphicPBMap = pbMap.CreateGraphics(); 
     // usually Values : gridNeedUpdate = true; rulersNeedUpdate = true; rulerNeedUpdate = true; backGroundNeedUpdate = true; nodesNeedUpdate = true; 
     if (backGroundNeedUpdate) 
     { 
      Bitmap srce = new Bitmap(BackGround); 
      Bitmap dest = new Bitmap(pbMap.Width, pbMap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 
      Graphics gr = Graphics.FromImage(dest); 
      gr.DrawImage(srce, new Rectangle(Point.Empty, dest.Size)); 
      graphicPBMap.DrawImage(dest, 0, 0); 
     } 
     backGroundNeedUpdate = false; 

     if (isGridShow && gridNeedUpdate) 
     { 
      for (int i = 0; i < pbMap.Width/60 + 1; i++) 
      { 
       graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, 0, pbMap.Width - i * 60, pbMap.Height); 
      } 
      for (int i = 0; i < pbMap.Height/60 + 1; i++) 
      { 
       graphicPBMap.DrawLine(gridPen, pbMap.Width, i * 60, 0, i * 60); 
      } 
     } 
     gridNeedUpdate = false; 

     if (isShowRulers && rulersNeedUpdate) 
     { 
      graphicPBMap.DrawLine(new Pen(Brushes.Black, 46), pbMap.Width - 1, pbMap.Height - 1, 0 - 1, pbMap.Height - 1); 
      graphicPBMap.DrawLine(new Pen(Brushes.Black, 49), 0, 0, 0, pbMap.Height); 

      for (int i = 0; i < pbMap.Width/60 + 1; i++) 
      { 
       graphicPBMap.DrawString(XPixelToLong((double)pbMap.Width - i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, pbMap.Width - i * 60, pbMap.Height - 15); 
       graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, pbMap.Height - 24, pbMap.Width - i * 60, pbMap.Height); 
      } 
      for (int i = 0; i < pbMap.Height/60 + 1; i++) 
      { 
       graphicPBMap.DrawLine(gridPen, 0, i * 60, 25, i * 60); 
       graphicPBMap.DrawString(YPixelToLat(i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, 0, i * 60); 
      } 
     } 
     rulersNeedUpdate = false; 

     if (rulerNeedUpdate) 
     { 
      if (x0ruler != -1 && y0ruler != -1) 
      { 
       if (x1ruler != -1 && y1ruler != -1) 
       { 
        Rectangle rectAngle = new Rectangle((int)(x1ruler - 1), (int)(y1ruler - 1), 2, 2); 
        graphicPBMap.DrawEllipse(rulerPen, rectAngle); 
        graphicPBMap.DrawLine(rulerPen, x0ruler, y0ruler, x1ruler, y1ruler); 
        rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2); 
        graphicPBMap.DrawEllipse(rulerPen, rectAngle); 
       } 
       else 
       { 
        Rectangle rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2); 
        graphicPBMap.DrawEllipse(rulerPen, rectAngle); 
       } 
      } 

     } 
     rulerNeedUpdate = false; 

     if (nodesNeedUpdate) 
     { 
      nodesNeedUpdate = false; 
      Node node; 
      for (int i = 0; i < nodes.Count; i++) 
      { 
       node = (Node)(nodes[i]); 
       if (node.IsOn) 
       { 
        drawOnCircle(graphicPBMap,node.Longitude, node.Latitude, node.RInMeter, node.IsSelected); 
       } 
       else 
       { 
        drawOffCircle(graphicPBMap, node.Longitude, node.Latitude, node.RInMeter, node.IsSelected); 
       } 
      } 
     } 
    } 

EDIT 1

는 한번은 코드를 변경.

답변

0

마지막으로 해결책을 찾았습니다.

그림 상자의 페인트 이벤트를 사용해야합니다. 내가 모든 내가 pbMap 그림 상자와 함께하고 싶은 생각 넣어이 이벤트에

private void pbMap_Paint(object sender, PaintEventArgs e) 
    { 
     //do every think you want with picture box like draw circle or change background 
    } 

.

와 나는 아래와 같은 기능을 호출 일부 내 변경 사항을 적용하기 위해 변경하는 경우

public void updateMap() 
    { 
     if (pbMap.InvokeRequired) // just if you call in thread 
     { 
      pbMap.Invoke(new Action(() => pbMap.Invalidate())); 
     } 
     else 
     { 
      pbMap.Invalidate(); 
     } 
    } 
0

picturebox의 이미지를 변경해야하는 invalidate() 또는 pictureBox.image = null;을 사용할 수 있습니다. 어쨌든 항상 배경을 변경할 필요는 없습니다. 대신 비트 맵을 만들고 pictureBox.Image = bitmap으로 배치 할 수 있습니다. 정확한 샘플 소스 코드가 필요하면 알려주십시오.

+0

yes.please 나에게 올바른 모드를 보냅니다. 무효화를 사용하면 배경 그림이 그대로 유지되고 다른 모양이 표시되지 않습니다. –

0

죄송합니다. 시도해보십시오.

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    Bitmap bckMap = null; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     bckMap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); 
     using (Graphics g = Graphics.FromImage(bckMap)) 
     { 
      g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, bckMap.Width, bckMap.Height)); 
      g.Dispose(); 
     } 

     pictureBox1.BackgroundImage = bckMap; 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Image = null; 
     Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); 
     using (Graphics g = Graphics.FromImage(ellips)) 
     { 
      g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(0, 0, ellips.Width, ellips.Height)); 
      g.Dispose(); 
     } 
     this.pictureBox1.Image = ellips; 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Image = null; 
     Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); 
     using (Graphics g = Graphics.FromImage(ellips)) 
     { 
      g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(5, 5, ellips.Width-10, ellips.Height-10)); 
      g.Dispose(); 
     } 
     this.pictureBox1.Image = ellips; 
    } 
+0

고맙습니다. 가능한 한 빨리 여기에 게시 할 새로운 솔루션을 발견했습니다. –

관련 문제