2013-01-18 3 views
0

UserControlbutton이 있습니다. UserControlOnPaint 이벤트에서 둥근 모서리 경계선 (또는 반지름이 0 인 경우 간단한 직사각형)을 그린 다음 전체 컨트롤을 채 웁니다. 이러한 조작 후 내 Button (btnClose) 사라집니다. button을 다시 표시하려면 어떻게해야합니까?UserControl OnRepaint 이벤트 후 부모 UserControl 컨트롤이 사라집니다.

protected override void OnPaint(PaintEventArgs pe) 
{ 
    using (System.Drawing.Pen p = new Pen(new SolidBrush(this.BorderColor))) 
    { 
     if (borderRadius > 0) 
     { 
      DrawRoundRect(pe.Graphics, p, 0, 0, this.Width - 1, this.Height - 1, borderRadius, this.FillColor); 
     } 
     else 
     { 
      this.BackColor = this.FillColor; 
      pe.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1); 
     } 
     btnClose.Location = new Point(this.Width - btnClose.Width - BTN_MARGIN_DELTA, BTN_MARGIN_DELTA); 
    } 
    base.OnPaint(pe); 
} 

그냥 경우, 것은 drawRoundRect 기능 : 페인트 이벤트에 컨트롤을 이동

protected override void OnResize(EventArgs e) { 
    btnClose.Location = new Point(this.Width - btnClose.Width - BTN_MARGIN_DELTA, BTN_MARGIN_DELTA); 
} 

는에 재귀 호출이 발생할 수 :

void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius, Color _fillColor) 
{ 
    using (GraphicsPath gp = new GraphicsPath()) 
    { 
     gp.AddLine(X + radius, Y, X + width - (radius * 2), Y); 
     gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90); 
     gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2)); 
     gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90); 
     gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height); 
     gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90); 
     gp.AddLine(X, Y + height - (radius * 2), X, Y + radius); 
     gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90); 
     gp.CloseFigure(); 

     using (SolidBrush brush = new SolidBrush(_fillColor)) 
     { 
      g.FillPath(brush, gp); 
      g.DrawPath(p, gp); 
     } 
    } 
} 

답변

1

가 크기 조정 방법에 위치 코드를 이동하십시오 페인트 이벤트 페인트 이벤트에서만 "페인트".

+0

글쎄, 내 실수 였어. UserControl에서 모든 컨트롤을 삭제하는 함수였습니다. 코드는 아래에 제공됩니다. 어쨌든, OnResize는 버튼의 올바른 위치를 확립하는 데 도움이되었습니다. – kirpi4

0

FillColor = Color.Gray, BorderColor = Color.Black, borderRadius = 5, BTN_MARGIN_DELTA = 2으로 설정하면 아무런 문제없이 작동하는 것 같습니다. 여기에 스크린 샷은 다음과 같습니다

enter image description here

나는 문제가 코드 줄하지라고 생각합니다.

+0

그래, 쉬어. 문제는 내가 컨트롤을 완전히 삭제 한 다른 곳에서 발생했습니다. – kirpi4

0

글쎄, 내 실수. UserControl에서 모든 컨트롤을 삭제하는 함수였습니다. 그래서 나는 제거에 대한 컨트롤을 걸러냅니다.

void ClearControls() 
{ 
    for (int i = 0; i < Items.Count; i++) 
    { 
     foreach (Control cc in Controls) 
     { 
      if (cc.Name.Contains(LINK_LABEL_FAMILY) || (cc.Name.Contains(LABEL_FAMILY))) 
      { 
       Controls.RemoveByKey(cc.Name); 
      } 
     } 
    } 
}