Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than uxtheme, prefix the name with the module name and a period.
DrawThemeText (uxtheme)
.
C# Signature:
[DllImport("uxtheme", ExactSpelling=true, CharSet=CharSet.Unicode)]
public extern static Int32 DrawThemeText(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, String text, int textLength, UInt32 textFlags, UInt32 textFlags2, ref RECT pRect);
VB .NET Signature:
Declare Function DrawThemeText Lib "uxtheme.dll" (TODO) As TODO
User-Defined Types:
None.
Notes:
None.
Tips & Tricks:
Please add some!
Sample Code:
VB.Net:
Imports System.Runtime.InteropServices
'// Make sure Themes are enabled in the application
Public Sub New()
MyBase.New()
' Enable WindowsXP Themes without a manifest file
System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.DoEvents()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'//
'// P/Invoke events
<DllImport("Comctl32.dll", ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
Private Overloads Shared Function DllGetVersion(ByRef pdvi As DLLVERSIONINFO) As Integer
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure DLLVERSIONINFO
Friend cbSize As Integer
Friend dwMajorVersion As Integer
Friend dwMinorVersion As Integer
Friend dwBuildNumber As Integer
Friend dwPlatformID As Integer
End Structure
<DllImport("UxTheme.dll", ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
Public Overloads Shared Function OpenThemeData(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.LPTStr)> ByVal pszClassList As String) As IntPtr
End Function
<DllImport("UxTheme.dll", ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
Public Overloads Shared Function CloseThemeData(ByVal htheme As IntPtr) As Integer
End Function
<DllImport("UxTheme.dll", ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
Public Overloads Shared Function IsAppThemed() As Boolean
End Function
' IsAppThemed will return true if the Os is themed even if the app is not.
' To overcome this problem we must also check which version of ComCtl32.dll is
' being used. Since ComCtl32.dll version 6 is exclusive to WindowsXP, we do not
' need to check the OSVersion.
Public ReadOnly Property IsAppXPThemed() As Boolean
Get
Dim dllVer As New DLLVERSIONINFO
dllVer.cbSize = Marshal.SizeOf(dllVer)
DllGetVersion(dllVer)
If dllVer.dwMajorVersion >= 6 Then If IsAppThemed() Then Return True
End Get
End Property
'//
'// Text to String Pointer Class
Public Class StrPtr
Public Handle As New IntPtr
Private My_Text As String
Public Sub New()
End Sub
Public Sub New(ByVal Value As String)
My_Text = Value
If Not Me.Handle.Equals(IntPtr.Zero) Then Marshal.FreeHGlobal(Handle)
Me.ToPointer(My_Text)
End Sub
Protected Overrides Sub Finalize()
Marshal.FreeHGlobal(Handle)
MyBase.Finalize()
End Sub
Public Sub ToPointer(ByVal TextString As String)
Handle = Marshal.StringToHGlobalAuto(TextString)
End Sub
Public Overrides Function ToString() As String
Return Marshal.PtrToStringAuto(Handle)
End Function
Public Property Text() As String
Get
If Not Me.Handle.Equals(IntPtr.Zero) Then Return Me.ToString
Return My_Text
End Get
Set(ByVal Value As String)
My_Text = Value
If Not Me.Handle.Equals(IntPtr.Zero) Then Marshal.FreeHGlobal(Handle)
Me.ToPointer(My_Text)
End Set
End Property
End Class
'//
'// Use the P/Invoke API
Public Function DrawThemedText(ByVal Text As String, ByVal ThemeClass As UX_ThemeClass, ByVal ThemePart As UX_ThemePart, ByVal ThemeState As UX_ThemeState, ByVal Bounds As Rectangle) As Bitmap
' The handle for the theme objects.
Dim My_ThemeHandle As IntPtr
' Setup String Pointer.
Dim My_ThemeText As New StrPtr(Text)
' Create Destination Bitmap from bounding dimensions.
Dim My_Bitmap As New Bitmap(Bounds.Width, Bounds.Height)
' Create Graphics to ge the HDC from.
Dim My_Graphics As Graphics = Graphics.FromImage(My_Bitmap)
' If Themed then draw themed, otherwise draw non-themed.
If IsAppXPThemed Then
' Get the HDC from the hidden atribute.
Dim My_HDC As IntPtr = My_Graphics.GetHdc
' If we have a Pointer open we will now close it.
If Not My_ThemeHandle.Equals(IntPtr.Zero) Then CloseThemeData(My_ThemeHandle)
' We will now open a pointer to a Theme Data object. Notice that the string is being parsed from the Enum.
My_ThemeHandle = OpenThemeData(Me.Handle, [Enum].GetName(GetType(UX_ThemeClass), ThemeClass))
If My_ThemeText.Text <> "" Then
' Draw Theme Background to the destination Bitmap.
DrawThemeText(My_ThemeHandle, My_HDC, ThemePart, ThemeState, My_ThemeText.Handle, -1, DrawTextFlags.DT_CENTER Or DrawTextFlags.DT_VCENTER, 0, Bounds)
End If
' HDC is do longer required so clean it up.
My_Graphics.ReleaseHdc(My_HDC)
End If
' Finished using the Graphics object so dispose of it.
My_Graphics.Dispose()
' Return the extracted bitmap.
Return My_Bitmap
End Function
'//
'// How to use the Functions
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.DrawImage(DrawThemedText("Hellow World!", UX_ThemeClass.Button, UX_ThemePart.BP_PUSHBUTTON, UX_ThemeState.ABS_DOWNHOT, New Rectangle(Point.Empty, Me.Size)), Point.Empty)
End Sub
'//
Please add some more!
Alternative Managed API:
TODO
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).