2009-02-06 5 views
3

UserControl에서 파생 된 사용자 정의 컨트롤이 있습니다.VisualStudio : 디자인 타임에 점선 테두리를 UserControl에 추가하는 방법?

양식에 놓을 때 사용자 정의 컨트롤은 보이지 않습니다. 테두리는 보이지 않으므로 사용자 정의 컨트롤이 보이지 않습니다.

디자인 타임에 PictureBox 및 Panel 컨트롤은 파선으로 보이는 1px 테두리를 그립니다.

올바른 방법은 무엇입니까? 거기에 VS가 추가 할 수있는 속성이 있습니까?

답변

3

이 작업을 자동으로 수행 할 속성이 없습니다. 그러나 컨트롤에서 OnPaint를 무시하고 수동으로 사각형을 그려서이를 보관할 수 있습니다.

오버라이드 된 이벤트에서 base.OnPaint (e)를 호출하여 컨트롤 내용을 그린 다음 그래픽 객체를 사용하여 가장자리 주위에 점선을 그립니다.

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     if (this.DesignMode) 
      ControlPaint.DrawBorder(e.Graphics,this.ClientRectangle,Color.Gray,ButtonBorderStyle.Dashed); 
    } 

당신은 단지 당신의 IDE에 그립니다 있도록 컨트롤 속성을 designMode를 조회 if 문이 추가 코드를 포장해야합니다 볼 수 있듯이.

0

Panel이 수행하는 방식 (이것이 실제 올바른 방법임을 나타냄)은 DesignerAttribute입니다. 이 속성은 Control 구성 요소와 비 제어 구성 요소 (예 : Timer)에 대한 설계 시간 추가에 사용할 수 있습니다.

DesignerAttribute을 사용할 때 IDesigner에서 파생 된 클래스를 지정해야합니다. 특히 Control 디자이너의 경우 ControlDesigner에서 파생되어야합니다.

ControlDesigner의 특정 구현에서는 OnPaintAdornment을 무시하고 싶습니다. 이 메서드의 목적은 특히 테두리와 같이 컨트롤 위에 디자이너 힌트를 그리는 것입니다.

Panel이 사용하는 구현은 다음과 같습니다. 복사하여 제어를 위해 사용할 수는 있지만, 구체적으로 Panel 클래스를 참조하는 부분을 조정해야합니다.

internal class PanelDesigner : ScrollableControlDesigner 
{ 
    protected Pen BorderPen 
    { 
     get 
     { 
      Color color = ((double)this.Control.BackColor.GetBrightness() < 0.5) ? ControlPaint.Light(this.Control.BackColor) : ControlPaint.Dark(this.Control.BackColor); 
      return new Pen(color) 
      { 
       DashStyle = DashStyle.Dash 
      }; 
     } 
    } 
    public PanelDesigner() 
    { 
     base.AutoResizeHandles = true; 
    } 
    protected virtual void DrawBorder(Graphics graphics) 
    { 
     Panel panel = (Panel)base.Component; 
     if (panel == null || !panel.Visible) 
     { 
      return; 
     } 
     Pen borderPen = this.BorderPen; 
     Rectangle clientRectangle = this.Control.ClientRectangle; 
     int num = clientRectangle.Width; 
     clientRectangle.Width = num - 1; 
     num = clientRectangle.Height; 
     clientRectangle.Height = num - 1; 
     graphics.DrawRectangle(borderPen, clientRectangle); 
     borderPen.Dispose(); 
    } 
    protected override void OnPaintAdornments(PaintEventArgs pe) 
    { 
     Panel panel = (Panel)base.Component; 
     if (panel.BorderStyle == BorderStyle.None) 
     { 
      this.DrawBorder(pe.Graphics); 
     } 
     base.OnPaintAdornments(pe); 
    } 
} 

ScrollableControlDesigner 당신이 또는 특정 디자이너 구현을위한 기지로 사용하지 않을 수있다 공공 클래스입니다.

관련 문제