DrawText (user32)
Last changed: -205.210.232.62

.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, uint uFormat);

User-Defined Types:

private struct Rect
{
    public int Left,Top,Right,Bottom;
    public Rect(Rectangle r)
    {
        this.Left = r.Left;
        this.Top = r.Top;
        this.Bottom = r.Bottom;
        this.Right = r.Right;
    }
}

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

[DllImport("user32.dll", CharSet=CharSet.Unicode)]
static extern int DrawText(IntPtr hdc, string lpStr, int nCount,ref Rect lpRect, int wFormat);

[DllImport("gdi32.dll")]
static extern int SetTextColor(IntPtr hdc, int crColor);
[DllImport("gdi32.dll")]
static extern int SetBkColor(IntPtr hdc, int crColor);

private const int DT_CENTER = 0x1;
private const int DT_VCENTER = 0x4;
private const int DT_SINGLELINE = 0x20;

private struct Rect
{
    public int Left,Top,Right,Bottom;
    public Rect(Rectangle r)
    {
        this.Left = r.Left;
        this.Top = r.Top;
        this.Bottom = r.Bottom;
        this.Right = r.Right;
    }
}

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    TabPage tabPage = this.TabPages[e.Index] as TabPage;
    using (Brush backcolorBrush = new SolidBrush(Color.Pink))
    {
        e.Graphics.FillRectangle(backcolorBrush, e.Bounds);
        Rect bounds = new Rect(e.Bounds);
        IntPtr hdc = e.Graphics.GetHdc();

        SetTextColor(hdc, ColorTranslator.ToWin32(Color.SteelBlue));
        SetBkColor(hdc, ColorTranslator.ToWin32(Color.LemonChiffon));

        int flags = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
        DrawText(hdc, tabPage.Text, tabPage.Text.Length, ref bounds, flags);
        e.Graphics.ReleaseHdc(hdc);
    }
    e.DrawFocusRectangle();
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
DrawText on MSDN