Control.CreateGraphics 메서드

정의

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

public:
 System::Drawing::Graphics ^ CreateGraphics();
public System.Drawing.Graphics CreateGraphics ();
member this.CreateGraphics : unit -> System.Drawing.Graphics
Public Function CreateGraphics () As Graphics

반환

컨트롤에 대한 Graphics입니다.

예제

다음 코드 예제에서는 컨트롤의 서식이 지정된 텍스트를 수용할 수 있도록 지정된 컨트롤의 크기를 조정합니다. 서식이 지정된 텍스트는 Text 컨트롤이 텍스트에 할당된 Font 속성입니다. AutoSizeControl 이 예제의 메서드에는 textPadding 컨트롤의 모든 가장자리에 적용할 안쪽 여백을 나타내는 매개 변수도 있습니다. 안쪽 여백이 동일하게 표시되도록 하려면 컨트롤이 MiddleCenter 지원하는 경우 텍스트를 의 System.Drawing.ContentAlignment 값에 맞춥니다.

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.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 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

설명

메서드를 통해 CreateGraphics 검색하는 개체는 Graphics 현재 Windows 메시지가 처리된 후에는 일반적으로 유지되지 않아야 합니다. 해당 개체로 그린 모든 항목은 다음 WM_PAINT 메시지로 지워지므로 유지되지 않습니다. 따라서 와 같은 Graphics.MeasureString시각적 개체가 아닌 메서드를 Graphics 사용하는 경우를 제외하고는 개체를 다시 사용할 수 없습니다. 대신 개체를 사용 Graphics 하려는 때마다 를 호출 CreateGraphics 한 다음, 사용이 완료되면 를 호출 Dispose 해야 합니다. Windows 메시지에 대한 자세한 내용은 를 참조하세요 WndProc.

기본적으로 CreateGraphics 는 소유권을 호출 스레드로 설정하고 다른 스레드에서 호출되면 실패합니다.

참고

속성 외에도 InvokeRequired 스레드로부터 안전한 Invoke컨트롤에는 , , BeginInvokeEndInvokeCreateGraphics 및 컨트롤에 대한 핸들이 이미 만들어진 경우 네 가지 메서드가 있습니다. 백그라운드 스레드에서 컨트롤의 핸들을 만들기 전에 를 호출 CreateGraphics 하면 잘못된 스레드 간 호출이 발생할 수 있습니다. 다른 모든 메서드 호출의 경우 호출 메서드 중 하나를 사용하여 컨트롤의 스레드에 대한 호출을 마샬링해야 합니다.

적용 대상

추가 정보