Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
settextcolor (gdi32)

C# Signature:

static extern uint SetTextColor(IntPtr hdc, int crColor);

VB.NET Signature

<DllImport("gdi32.dll", SetLastError:=True)> _
Public Shared Function SetTextColor(hdc As IntPtr, crColor as Integer) as UInteger
End Function

VB Signature

Public Declare Function SetTextColor Lib "gdi32.dll" _
         (ByVal prmlngHDc As Long, _
          ByVal prmlngRGB As Long) As Long

User-Defined Types:



The below example is need because of an error in the managed version.

Tips & Tricks:

Please add some!

Sample Code:

Example usage; ownerdraw on tabcontrol (tab headers)

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

static extern int SetTextColor(IntPtr hdc, int crColor);
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)
    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);

Alternative Managed API:

Do you know one? Please contribute it!


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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version