다음을 통해 공유


Control.CreateGraphics 메서드

해당 컨트롤에 대한 Graphics를 만듭니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Function CreateGraphics As Graphics
‘사용 방법
Dim instance As Control
Dim returnValue As Graphics

returnValue = instance.CreateGraphics
public Graphics CreateGraphics ()
public:
Graphics^ CreateGraphics ()
public Graphics CreateGraphics ()
public function CreateGraphics () : Graphics

반환 값

컨트롤에 대한 Graphics입니다.

설명

더 이상 필요하지 않은 반환된 GraphicsDispose 메서드를 호출하여 삭제해야 합니다. Graphics는 현재 창 메시지의 재생 시간 동안에만 유효합니다.

참고

컨트롤에는 InvokeRequired 속성과 함께 스레드로부터 안전한 네 개의 메서드(Invoke, BeginInvoke, EndInvoke, CreateGraphics)가 있습니다. 다른 스레드에서 메서드를 호출하는 경우 이러한 호출 메서드 중 하나를 사용하여 컨트롤의 스레드에 대한 호출을 마샬링해야 합니다.

예제

다음 코드 예제에서는 서식이 지정된 텍스트가 컨트롤 안에 들어가도록 지정한 컨트롤의 크기를 조정합니다. 서식이 지정된 텍스트는 컨트롤의 할당된 Font가 텍스트에 적용된 Text 속성입니다. 이 예제에서 AutoSizeControl 메서드는 컨트롤의 모든 가장자리에 적용될 안쪽 여백을 나타내는 textPadding 매개 변수도 사용합니다. 안쪽 여백을 똑같이 나타내려면 텍스트를 System.Drawing.ContentAlignmentMiddleCenter 값에 맞춥니다(컨트롤에서 지원할 경우).

Private Sub AutoSizeControl(control As Control, textPadding As Integer)
   ' Create a Graphics object for the Control.
   Dim g As Graphics = control.CreateGraphics()
   
   ' Get the Size needed to accommodate the formatted Text.
   Dim preferredSize As Size = g.MeasureString( _
     control.Text, control.Font).ToSize()
   
   ' Pad the text and resize the control.
   control.ClientSize = New Size( _
     preferredSize.Width + textPadding * 2, _
     preferredSize.Height + textPadding * 2)
   
   ' Clean up the Graphics object.
   g.Dispose()
End Sub
private void AutoSizeControl(Control control, int textPadding)
{
   // Create a Graphics object for the Control.
   Graphics g = control.CreateGraphics();

   // Get the Size needed to accommodate the formatted Text.
   Size preferredSize = g.MeasureString(
      control.Text, control.Font).ToSize();

   // Pad the text and resize the control.
   control.ClientSize = new Size(
      preferredSize.Width + (textPadding * 2), 
      preferredSize.Height+(textPadding * 2) );

   // Clean up the Graphics object.
   g.Dispose();
}
private:
   void AutoSizeControl( Control^ control, int textPadding )
   {
      
      // Create a Graphics object for the Control.
      Graphics^ g = control->CreateGraphics();
      
      // Get the Size needed to accommodate the formatted Text.
      System::Drawing::Size preferredSize = g->MeasureString( control->Text, control->Font ).ToSize();
      
      // Pad the text and resize the control.
      control->ClientSize = System::Drawing::Size( preferredSize.Width + (textPadding * 2), preferredSize.Height + (textPadding * 2) );
      
      // Clean up the Graphics object.
      delete g;
   }
private void AutoSizeControl(Control control, int textPadding)
{
    // Create a Graphics object for the Control.
    Graphics g = control.CreateGraphics();
    // Get the Size needed to accommodate the formatted Text.
    Size preferredSize = g.MeasureString(control.get_Text(), control.
        get_Font()).ToSize();
    // Pad the text and resize the control.
    control.set_ClientSize(new Size(preferredSize.get_Width() 
        + textPadding * 2, preferredSize.get_Height() + textPadding * 2));
    // Clean up the Graphics object.
    g.Dispose();
} //AutoSizeControl

.NET Framework 보안

  • UIPermission  안전한 하위 창에서 이 메서드를 호출하는 데 필요한 권한입니다. 연관된 열거형: UIPermissionWindowSafeSubWindows

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Control 클래스
Control 멤버
System.Windows.Forms 네임스페이스
Graphics