Export (0) Print
Expand All
Expand Minimize

Control.CreateGraphics Method

Creates the Graphics object for the control.

[Visual Basic]
Public Function CreateGraphics() As Graphics
[C#]
public Graphics CreateGraphics();
[C++]
public: Graphics* CreateGraphics();
[JScript]
public function CreateGraphics() : Graphics;

Return Value

The Graphics object for the control.

Remarks

The returned Graphics object must be disposed through a call to its Dispose method when it is no longer needed. The Graphics object is only valid for the duration of the current window's message.

Note   There are four methods on a control that are safe to call from any thread: Invoke, BeginInvoke, EndInvoke, and CreateGraphics. For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread.

Example

[Visual Basic, C#, C++] The following example resizes the specified control so the control will accommodate its formatted text. The formatted text is the Text property with the control's assigned 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 ContentAlignment.MiddleCenter value, if your control supports it.

[Visual Basic] 
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

[C#] 
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();
}

[C++] 
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.
        g->Dispose();
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

Control Class | Control Members | System.Windows.Forms Namespace | Graphics

Show:
© 2014 Microsoft