Control.CreateGraphics Control.CreateGraphics Control.CreateGraphics Control.CreateGraphics Method

定義

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

public:
 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. 若要讓填補變得相等, 請在控制項支援MiddleCenter時, System.Drawing.ContentAlignment將文字與的值對齊。To make the padding appear equal, align the text with the MiddleCenter value of System.Drawing.ContentAlignment if your control supports it.

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

備註

Graphics目前的 Windows 訊息已處理CreateGraphics之後, 您透過方法抓取的物件通常不應保留下來, 因為任何以該物件繪製的專案都會以下一個 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. 相反地, 您必須CreateGraphics在每次想要Graphics使用物件時呼叫, 然後在使用Dispose完畢後呼叫。Instead, 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 訊息的詳細資訊, WndProc請參閱。For 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.

安全性

UIPermission
適用于安全的子視窗來呼叫這個方法。for safe subwindows to call this method. 相關聯的SafeSubWindows列舉: UIPermissionWindow的值。Associated enumeration: SafeSubWindows value of UIPermissionWindow.

適用於

另請參閱