Export (0) Print
Expand All

ToolStripItem.ContentRectangle Property

Gets the area where content, such as text and icons, can be placed within a ToolStripItem without overwriting background borders.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public Rectangle ContentRectangle { get; }

Property Value

Type: System.Drawing.Rectangle
A Rectangle containing four integers that represent the location and size of ToolStripItem contents, excluding its border.

Use the ContentRectangle property to do custom painting of a ToolStripItem.

The following code example demonstrates how to use the ContentRectangle property for custom rendering. This code example is part of a larger example provided for the ToolStripItem class.

// This utility method computes the layout of the  
// RolloverItem control's image area and the text area. 
// For brevity, only the following settings are  
// supported: 
// 
// ToolStripTextDirection.Horizontal 
// TextImageRelation.ImageBeforeText  
// TextImageRelation.ImageBeforeText 
//  
// It would not be difficult to support vertical text 
// directions and other image/text relationships. 
private void ComputeImageAndTextLayout()
{
    Rectangle cr = base.ContentRectangle;
    Image img = base.Owner.ImageList.Images[base.ImageKey];

    // Compute the center of the item's ContentRectangle. 
    int centerY = (cr.Height - img.Height) / 2;

    // Find the dimensions of the image and the text  
    // areas of the item. The text occupies the space  
    // not filled by the image.  
    if (base.TextImageRelation == TextImageRelation.ImageBeforeText &&
        base.TextDirection == ToolStripTextDirection.Horizontal)
    {
        imageRect = new Rectangle(
            base.ContentRectangle.Left,
            centerY,
            base.Image.Width,
            base.Image.Height);

        textRect = new Rectangle(
            imageRect.Width,
            base.ContentRectangle.Top,
            base.ContentRectangle.Width - imageRect.Width,
            base.ContentRectangle.Height);
    }
    else if (base.TextImageRelation == TextImageRelation.TextBeforeImage &&
             base.TextDirection == ToolStripTextDirection.Horizontal)
    {
        imageRect = new Rectangle(
            base.ContentRectangle.Right - base.Image.Width,
            centerY,
            base.Image.Width,
            base.Image.Height);

        textRect = new Rectangle(
            base.ContentRectangle.Left,
            base.ContentRectangle.Top,
            imageRect.X,
            base.ContentRectangle.Bottom);
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft