Control.CreateGraphics 方法

定义

为控件创建 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. 带格式的文本是应用于文本的控件的分配 FontText 属性。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.

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

注解

在处理当前 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. 对于所有其他方法调用,应使用调用方法之一封送对控件线程的调用。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. 关联枚举: UIPermissionWindowSafeSubWindows 值。Associated enumeration: SafeSubWindows value of UIPermissionWindow.

适用于

另请参阅