Control.CreateGraphics 方法


建立控制項的 GraphicsCreates the Graphics for the control.

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



控制項的 GraphicsThe Graphics for the control.


下列程式碼範例會調整指定控制項的大小,讓控制項容納其格式化的文字。The following code example resizes the specified control so the control will accommodate its formatted text. 格式化的文字是 Text 屬性,其已將控制項的指派 Font 套用至文字。The formatted text is the Text property with the control's assigned Font applied to the text. 這個範例中的 AutoSizeControl 方法也具有 textPadding 參數,表示要套用至控制項所有邊緣的填補。The AutoSizeControl method in this example also has a textPadding parameter that represents the padding to apply to all edges of the control. 若要讓填補顯示為相等,請將文字對齊 System.Drawing.ContentAlignmentMiddleCenter 值(如果您的控制項支援的話)。To make the padding appear equal, align the text with the MiddleCenter value of System.Drawing.ContentAlignment if your control supports it.

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


在目前的 Windows 訊息已經處理之後,您透過 CreateGraphics 方法抓取的 Graphics 物件通常不會保留下來,因為任何以該物件繪製的專案都會以下一個 WM_PAINT 訊息來清除。The Graphics object that you retrieve through the CreateGraphics method should not normally be retained after the current Windows message has been processed, because anything painted with that object will be erased with the next WM_PAINT message. 因此,您無法快取 Graphics 物件以供重複使用,除非使用如 Graphics.MeasureString的非視覺方法。Therefore you cannot cache the Graphics object for reuse, except to use non-visual methods like Graphics.MeasureString. 相反地,您必須在每次想要使用 Graphics 物件時呼叫 CreateGraphics,然後在使用完畢後呼叫 DisposeInstead, you must call CreateGraphics every time that you want to use the Graphics object, and then call Dispose when you are finished using it. 如需 Windows 訊息的詳細資訊,請參閱 WndProcFor more information about Windows messages, see WndProc.

根據設計,CreateGraphics 會將擁有權設定為呼叫執行緒,如果在其他執行緒上呼叫,則會失敗。By design, CreateGraphics sets ownership to the calling thread, and fails if it is called on other threads.


除了 InvokeRequired 屬性以外,控制項上還有四個安全線程的方法: InvokeBeginInvokeEndInvokeCreateGraphics (如果已經建立控制項的控制碼)。In addition to the InvokeRequired property, there are four methods on a control that are thread safe: Invoke, BeginInvoke, EndInvoke, and CreateGraphics if the handle for the control has already been created. 在背景執行緒上建立控制項的控制碼之前呼叫 CreateGraphics,可能會造成不合法的跨執行緒呼叫。Calling CreateGraphics before the control's handle has been created on a background thread can cause illegal cross thread calls. 對於所有其他方法呼叫,您應該使用其中一個 invoke 方法,將呼叫封送處理至控制項的執行緒。For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread.