다음을 통해 공유


방법: 텍스트 주위에 상자 표시

업데이트: 2007년 11월

다음 예제 프로그램은 텍스트 문자열 주위에 사각형을 그립니다.

예제

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim s As String = ".NET Compact Framework"
    Dim pen As New Pen(Color.Red, 5)
    Dim font As New Font("Arial", 10, FontStyle.Regular)
    Dim brush As New SolidBrush(Color.Black)
    Dim textSize As SizeF = e.Graphics.MeasureString(s, font)

    ' Create a rectangle with padding space between string and box.
    Dim r As New Rectangle(45, 70, CInt(Fix(textSize.Width) + 10), _
        CInt(Fix(textSize.Height) + 10))
    e.Graphics.DrawRectangle(pen, r)
    e.Graphics.DrawString(s, font, brush, 50F, 75F)
    MyBase.OnPaint(e)
End Sub
protected override void OnPaint(PaintEventArgs e)
{
    string s = ".NET Compact Framework";
    Pen pen = new Pen(Color.Red, 5);
    Font font = new Font("Arial", 10, FontStyle.Regular);
    SolidBrush brush = new SolidBrush(Color.Black);
    SizeF textSize = e.Graphics.MeasureString(s, font);
    int newW = (int) textSize.Width + 10;
    int newH = (int) textSize.Height + 10;

    Rectangle r = new Rectangle(45, 70, newW, newH);
    e.Graphics.DrawRectangle(pen, r);
    e.Graphics.DrawString(s, font, brush, 50F, 75F);
base.OnPaint(e);
}

코드 컴파일

이 예제에는 다음과 같은 네임스페이스에 대한 참조가 필요합니다.

참고 항목

개념

.NET Compact Framework 방법 항목

기타 리소스

.NET Compact Framework의 그래픽 및 그리기