Using uxtheme.dll to draw xp themes

noj39

New member
Joined
Jun 2, 2010
Messages
4
Programming Experience
3-5
Hi,

I am using VB 2008 .net and i am using uxtheme.dll to access windows XP themes to draw certain items. Unfortunatley there is little of no info I can find with provides a vb.net example for using this dll.

For this program i am trying to get the theme for a button and just draw it to the client rectangle of a form (this is simply to get the dll functions working before i begin to use them for any application).

I have declared 3 of the funcitons from the dll as follows:

Private Declare Auto Function CloseThemeData Lib "uxtheme.dll" (ByVal hTheme As Integer) As Integer
Public Declare Auto Function OpenThemeData Lib "uxtheme.dll" (ByVal hwnd As Integer, ByVal pszClassList As String) As Integer
Private Declare Function DrawThemeBackground Lib "uxtheme.dll" (ByVal hTheme As Integer, ByVal lHDC As Integer, _
ByVal iPartId As Integer, ByVal iStateId As Integer, _
ByVal pRect As Rectangle, ByVal pClipRect As Rectangle) As Integer


In the Paint event of my form I have the following code:

Dim a As Integer
Dim b As Integer
a = OpenThemeData(0, "Button")
b = DrawThemeBackground(a, e.Graphics.GetHdc, 1, 1, Me.ClientRectangle, Me.ClientRectangle)
CloseThemeData(a)
e.Graphics.ReleaseHdc()


The code runs, but the DrawThemeBackground returns a HRESULT of &H8000403 which is i believe is an invalid pointer????

Can anyone guide me to correctly using the above functions?

Many Thanks,
 
Thanks for your response. Its a good spot so i have tried it below:

Structure RECT
Dim left As Integer
Dim top As Integer
Dim right As Integer
Dim bottom As Integer

End Structure


Declare Function GetSysColor Lib "user32" (ByVal nIndex As Integer) As Integer
' Private Declare Function OpenThemeData Lib "uxtheme.dll" (ByVal hwnd As Long, ByVal pszClassList As Long) As Long
' Private Declare Function OpenThemeData Lib "uxtheme.dll" (ByVal hWnd As Integer, ByVal pszClassList As String) As Integer
Private Declare Auto Function CloseThemeData Lib "uxtheme.dll" (ByVal hTheme As Integer) As Integer
Public Declare Auto Function OpenThemeData Lib "uxtheme.dll" (ByVal hwnd As Integer, ByVal pszClassList As String) As Integer
Private Declare Function DrawThemeBackground Lib "uxtheme.dll" (ByVal hTheme As Integer, ByVal lHDC As Integer, _
ByVal iPartId As Integer, ByVal iStateId As Integer, _
ByVal pRect As RECT, ByVal pClipRect As RECT) As Integer


Form Paint function:

Dim r As RECT

r.left = Me.ClientRectangle.Left
r.top = Me.ClientRectangle.Top
r.right = Me.ClientRectangle.Right
r.bottom = Me.ClientRectangle.Bottom


Dim a As Integer
Dim b As Integer
a = OpenThemeData(0, "Button")
b = DrawThemeBackground(a, e.Graphics.GetHdc, 1, 1, r, r)
CloseThemeData(a)
e.Graphics.ReleaseHdc()


Unfortunatley i still get the same error. I have tried different windows themes such as "Button", "Window" and "Tab" when calling OpenThemeData and i always get a good value (i.e non 0) so i believe that parameter is correct. I am not sure about the GetHdc call to my form though.
 
I am not sure about the GetHdc call to my form though.
From the sample code the GetHdc returned pointer seems to be valid input to hdc parameter.
 
Found the issue. The RECT structures need passing by reference not by value.

Private Declare Function DrawThemeBackground Lib "uxtheme.dll" (ByVal hTheme As Integer, ByVal lHDC As Integer, _
ByVal iPartId As Integer, ByVal iStateId As Integer, _
ByRef pRect As RECT, ByRef pClipRect As RECT) As Integer


Works fine now. Thanks for your help!
 
Ah, should have noticed that, that is the signature pinvoke.net uses also. Btw, you may also use the .Net ButtonRenderer Class (System.Windows.Forms)

I was only using button for an example, however after investigating the ButtonRenderer Class i have seen that i can access all the visual styles using System.Windows.Forms.VisualStyles which to me is much cleaner than using calls to dlls.

Thankyou very much for pointing me in this direction. Much Appreciated.
 
Back
Top