-
Drawing on the VisualStyles Layer
Does anyone have any sample VB.NET code for doing such?
I have been over this article but its for VB6 and a bit generic. I'm looking to draw on a toolbar button, an image of the
back arrow and forward arrow under XP SP2.
http://www.vbforums.com/attachment.p...id=36276&stc=1
Original thread located here.
This sample is without the VisualStyle enabled since the image of a toolbar button is only available without the visual style.
Also if how to draw the arrows so they are smoother around the edges and less jaggie.
Thanks for any assistance.
-
Re: Drawing on the VisualStyles Layer
Does this look like the correct translation of the C++ call?
VB Code:
HRESULT SetWindowTheme(HWND hwnd,
LPCWSTR pszSubAppName,
LPCWSTR pszSubIdList
);
Private Declare Function SetWindowTheme Lib "uxtheme.dll" (ByVal hWnd As Int32, _
ByVal pszSubAppName As String, ByVal pszSubIdList As String) As Int32
From MS:
The theme manager retains the pszSubAppName and the pszSubIdList associations through the lifetime of the
window, even if visual styles subsequently change. The window is sent a WM_THEMECHANGED message at the
end of a SetWindowTheme call, so that the new visual style can be found and applied.
When pszSubAppName and pszSubIdList are NULL, the theme manager removes the previously applied associations.
To prevent visual styles from being applied to a specified window, pass an empty string, (L" "), which will not match
any section entries.
-
Re: Drawing on the VisualStyles Layer
Here is the link over at M$ for all the C++ calls in case anyone whats to assist.
I figure I need OpenThemeData, ClosethemeData, DrawThemeBackground, DrawThemeIcon, DrawThemeText, etc.
-
Re: Drawing on the VisualStyles Layer
What do you mean it's not visible? Are you putting the Application.DoEvents() call in after you enable the visual styles?
-
Re: Drawing on the VisualStyles Layer
Yes its in there, but the issue is the the imagelist images associated with the toolbar draw on the toolbar layer and if I have VS enabled
they are draw on a secondary layer above the toolbar, like a skinning effect. Thus, they are covered by the VS and can not be seen.
-
Re: Drawing on the VisualStyles Layer
-
Re: Drawing on the VisualStyles Layer
I got a few minutes more to work on this and these are the four API calls involved in extracting the themem bitmap data.
OpenThemeData
DrawThemeParentBackground
DrawThemeBackground
CloseThemeData
More to come...
-
Re: Drawing on the VisualStyles Layer
Quote:
Originally Posted by RobDog888
Also if how to draw the arrows so they are smoother around the edges and less jaggie.
Why don't you try investigate into using alpha transparency on the images? That is the best way, but I'm not sure about the code. Well, hey, At least I gave you an idea of what to look for :D
-
Re: Drawing on the VisualStyles Layer
Thanks for the suggestion but I believe that there must be an easier way to draw them. Perhaps if it can get the APIs to draw
correctly it will take care of itself since I was drawing below the vs layer. :)
-
Re: Drawing on the VisualStyles Layer
Quote:
Originally Posted by RobDog888
Yes its in there, but the issue is the the imagelist images associated with the toolbar draw on the toolbar layer and if I have VS enabled
they are draw on a secondary layer above the toolbar, like a skinning effect. Thus, they are covered by the VS and can not be seen.
I'm a bit mystified by this thread. I have projects with VisualStyles enabled and I don't have issues with toolbar buttons not displaying images. Are you calling DoEvents() after EnableVisualStyles()? Sorry if this is covering old ground but I don't understand why there is an issue at all.
-
Re: Drawing on the VisualStyles Layer
I have even tried creating new test projects of nothing more then a form and a toolbar. Associated the imagelist but not images ever show up.:(
If I turn off the VS then they appear. Either way it is interesting to figure out how its all done instead of just making the call .EnableVisualStyles.
It would be great if it shows with no effort...
-
Re: Drawing on the VisualStyles Layer
This code works for me. If it doesn't for you then that's just wierd!
VB Code:
Shared Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New Form1)
End Sub
-
Re: Drawing on the VisualStyles Layer
Yes, I know how to enable them. If that was the issue I would have solved it immediatly.
frmMain contains my toolbar but the images do show if I have the VS turned on.
VB Code:
'Simplified example....
Module modMain
Public goMain As New frmMain
Public Sub Main()
Application.EnableVisualStyles
Application.DoEvents
Application.Run(goMain)
End Sub
End Module
-
Re: Drawing on the VisualStyles Layer
Your problem is the point at which you are instantiating your startup form. I tried this
VB Code:
Shared Sub Main()
Dim mainForm As New MainWindow
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(mainForm)
End Sub
and the toolbar images do not display. You must have to instantiate your form after the call to EnableVisualStyles or DoEvents or both.
Edit: Just tested and it is both.
-
Re: Drawing on the VisualStyles Layer
Sounds logical. I will test it out in a test proj. I have a few inherited forms and I was always getting errors from the inherited forms
not being set to an object so I had to make them New.
Maybe if this works this way I will reorganize the logic structure so I wont have to use inherited forms.
-
Re: Drawing on the VisualStyles Layer
jmcilhinney, you were correct! How about that. I wasted several hours researching drawing on the theme layer. Although it is cool
to do (probably will still learn more on it ;))
I am going to see if I can redesign my two forms that are inheirting or anyother workaround to incorporate this so the toolbar images draw.
Thanks so much for spotting the issue. :thumb:
-
Re: Drawing on the VisualStyles Layer
Happy to help. I'm guessing you'll be helping me in the not-too-distant future so I don't mind building up some early credit.
-
Re: Drawing on the VisualStyles Layer
I'll try to make sure I pay my debt. Wouldnt want it to go into collections. :p
:thumb:
But I still want to know how to draw so I will update the thread as I get the info. ;)
-
Re: Drawing on the VisualStyles Layer
Using visual styles is not recommended as it is a bug in VS.NET and instead you could always use a manifest file for your application.
-
Re: Drawing on the VisualStyles Layer
Whats wrong with using GDI+ for drawing stuff? Antialiasing is dead simple too.
-
Re: Drawing on the VisualStyles Layer
Why not just make the background colour for your image strip the same as the BG colour as that of the form in Photoshop and then input that image strip into the imagelist? That way you cut down on code by not having to make it cut out the black BG, which you have now and, you won't have to consider applying anti-aliasing or whatever to your image in code, thus making load times faster and you even get that smoothness you want. Of course, you can always do everything in code :P
-
Re: Drawing on the VisualStyles Layer
The images are loaded into a imagelist by way of the .AddStrip function. You only need to specify the imagelists .TransparentColor color
in order to create the mask.
I had thought of using the manifest method but I need to be able to turn the threme styles for my app only in my apps Option dialog.
-
Re: Drawing on the VisualStyles Layer
Also, I need to be able to detect if the tmemes are turned on in my app?
-
Re: Drawing on the VisualStyles Layer
Found the call for detecting if a theme is activated for your app by using the IsThemeActive API.
-
Re: Drawing on the VisualStyles Layer
Actually I found a better API to tell if your app is running themes.
IsAppThemed :thumb:
-
Re: Drawing on the VisualStyles Layer
Being able to turn on and off themes, even with manifest files should be easy...
Just embed the manifest file as a resource and when the user wants visual styles enabled, write it to a file in the applications directory, and require a restart. To disable visual styles just delete the manifest file.
Edit: Link: http://www.codeproject.com/dotnet/ManifestInjection.asp
-
Re: Drawing on the VisualStyles Layer
Thanks, I'll try it. Would I be able to do the same thing with theme api calls? ActivateTheme/DisableTheme? I think that would
be for the entire OS.
-
Re: Drawing on the VisualStyles Layer
I could not locate any Info on the ActivateTheme/DisableTheme API class, so I'm not sure.... I would check MSDN documentation(I couldn't find any)
-
Re: Drawing on the VisualStyles Layer
Looks like I could do it without using a manifest by calling the theme apis. SetWindowTheme looks promising. I will have to also use other theme apis but it can be done.
-
Re: Drawing on the VisualStyles Layer
Why cant I get a handle to the theme data on my form? I am using the OpenThemeData API but I get nothing.
VB Code:
Public Declare Function OpenThemeData Lib "uxtheme.dll" (ByVal hwnd As IntPtr, ByRef pszClassList As String) As IntPtr
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lHwnd As IntPtr
Try
lHwnd = OpenThemeData(Me.Handle, "Button")
MessageBox.Show(lHwnd.ToString)
CloseThemeData(lHwnd.ToInt32)
Catch ex As Exception
MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK)
End Try
End Sub
-
Re: Drawing on the VisualStyles Layer
Rob, could you explain abit on how you fixed the Toolbar image issue? I am just starting to convert over and my first project is working with the toolbar and VisualStyles would be nice to have but also I want my toolbar images :) Thank you for any help :)
-
Re: Drawing on the VisualStyles Layer
It was because of the order that the forms were being instanciated before the Application.EnableVisualStyles(). Enable the VS first
before any forms are set or dimmed with New. ;)
-
Re: Drawing on the VisualStyles Layer
*BUMP*
Any help on why I cant open the theme data and get a hendle? :(
-
Re: Drawing on the VisualStyles Layer
Everbody who wants visual style support should stop phaffing about and get over to www.skybound.ca to download their free VisualStyles component. It fixes almost all visual style issues with almost no effort on the part of the developer. If you want specific theming for your app that is a different story, although they do offer a paid-for theming component as well.
-
Re: Drawing on the VisualStyles Layer
Yes, basically thats what I am trying to do, custom theming for my user control and a toolbar. No possibility for paying them, only for me
to learn how to do it myself.
Interesting link though. Think I will add it to my favorites just in case. ;)
-
Re: Drawing on the VisualStyles Layer
Quote:
Originally Posted by RobDog888
Yes, basically thats what I am trying to do, custom theming for my user control and a toolbar. No possibility for paying them, only for me
to learn how to do it myself.
Interesting link though. Think I will add it to my favorites just in case. ;)
Sorry, I realise that you are trying to learn the details. I had intended my previous post more for the other folks who were asking general visual style questions :)
-
Re: Drawing on the VisualStyles Layer
No prob. Its a good link and I do appreciate any help you may be able to provde.
:)
-
Re: Drawing on the VisualStyles Layer
Hi,
Add 'Auto' to your function declaration and it should be fine:
VB Code:
Public Declare Auto Function OpenThemeData Lib "uxtheme.dll" (ByVal hwnd As System.IntPtr, ByVal pszClassList As String) As System.IntPtr
Kevin