Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

Control.CreateGraphics méthode ()

 

Date de publication : novembre 2016

Crée le Graphics pour le contrôle.

Espace de noms:   System.Windows.Forms
Assembly:  System.Windows.Forms (dans System.Windows.Forms.dll)

public Graphics CreateGraphics()

Valeur de retour

Type: System.Drawing.Graphics

Graphics du contrôle.

The T:System.Drawing.Graphics object that you retrieve through the M:System.Windows.Forms.Control.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. Therefore you cannot cache the T:System.Drawing.Graphics object for reuse, except to use non-visual methods like M:System.Drawing.Graphics.MeasureString(System.String,System.Drawing.Font). Instead, you must call M:System.Windows.Forms.Control.CreateGraphics every time that you want to use the T:System.Drawing.Graphics object, and then call M:System.Drawing.Graphics.Dispose when you are finished using it. For more information about Windows messages, see M:System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message@).

By design, M:System.Windows.Forms.Control.CreateGraphics sets ownership to the calling thread, and fails if it is called on other threads.

System_CAPS_noteRemarque

In addition to the P:System.Windows.Forms.Control.InvokeRequired property, there are four methods on a control that are thread safe: M:System.Windows.Forms.Control.Invoke(System.Delegate), M:System.Windows.Forms.Control.BeginInvoke(System.Delegate), M:System.Windows.Forms.Control.EndInvoke(System.IAsyncResult), and M:System.Windows.Forms.Control.CreateGraphics if the handle for the control has already been created. Calling M:System.Windows.Forms.Control.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.

The following code example resizes the specified control so the control will accommodate its formatted text. The formatted text is the P:System.Windows.Forms.Control.Text property with the control's assigned P:System.Windows.Forms.Control.Font applied to the text. The AutoSizeControl method in this example also has a textPadding parameter that represents the padding to apply to all edges of the control. To make the padding appear equal, align the text with the MiddleCenter value of T: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.
   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();
}

UIPermission

for safe subwindows to call this method. Associated enumeration: SafeSubWindows value of T:System.Security.Permissions.UIPermissionWindow.

.NET Framework
Disponible depuis 1.1
Retour au début
Afficher: