Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

DrawText (user32)
 
.
Summary
Summary

C# Signature:

[DllImport("user32.dll")]
static extern int DrawTextEx(IntPtr hdc, StringBuilder lpchText, int cchText,
   ref RECT lprc, uint dwDTFormat, ref DRAWTEXTPARAMS lpDTParams);

User-Defined Types:

DRAWTEXTPARAMS

static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, uint uFormat);

Notes:

None.

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;
    }
}

Tips & Tricks:

Notes:

None.

private const int DT_TOP = 0x00000000;

private const int DT_LEFT = 0x00000000;

private const int DT_CENTER = 0x00000001;

private const int DT_RIGHT = 0x00000002;

private const int DT_VCENTER = 0x00000004;

private const int DT_BOTTOM = 0x00000008;

private const int DT_WORDBREAK = 0x00000010;

private const int DT_SINGLELINE = 0x00000020;

private const int DT_EXPANDTABS = 0x00000040;

private const int DT_TABSTOP = 0x00000080;

private const int DT_NOCLIP = 0x00000100;

private const int DT_EXTERNALLEADING = 0x00000200;

private const int DT_CALCRECT = 0x00000400;

private const int DT_NOPREFIX = 0x00000800;

private const int DT_INTERNAL = 0x00001000;

private const int DT_EDITCONTROL = 0x00002000;

private const int DT_PATH_ELLIPSIS = 0x00004000;

private const int DT_END_ELLIPSIS = 0x00008000;

private const int DT_MODIFYSTRING = 0x00010000;

private const int DT_RTLREADING = 0x00020000;

private const int DT_WORD_ELLIPSIS = 0x00040000;

private const int DT_NOFULLWIDTHCHARBREAK = 0x00080000;

private const int DT_HIDEPREFIX = 0x00100000;

private const int DT_PREFIXONLY = 0x00200000;

Sample Code:

Tips & Tricks:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Sample Code:

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

Documentation
DrawTextEx on MSDN

[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:

System.Drawing.Graphics.DrawString

Documentation
DrawText on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions