'Modern' Controls for the Userform - UXTheme Visual Styles

Dan_W

Well-known Member
Joined
Jul 11, 2018
Messages
1,976
Office Version
  1. 365
Platform
  1. Windows
Hi

Does anyone have any experience in using the UXTheme.dll visual styles to create 'modern' looking controls in the VBA Userform?
I've been experimenting with it, and I've hit a wall of sorts - especially when it comes to the Textbox. I would be grateful for any guidance or recommended resources.
Thanks.

SJsAg.gif
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Never use UXTheme.dll before, but have created standard Windows controls for use on UserForm once. Performance is the only real problem I can see for using those controls on UserForm. And its very unpredictable when the UserForm will update/repaint itself, causing many flickers at runtime.
 
Upvote 0
Thank you for your reply. I take your point about repainting. I haven't had too many problems with flickering thus far, except for the trackbar (the one after the scrollbar and before the edit control) - moving the thumb on that leaves 'residue' that you can clear away by using the Repaint method (on, say, the frame control or the userform control), but this is when the flickering comes in. My solution to that was just to paint over everything with a white rectangle then repaint each layer, and I seem to have got away with it.

I've discovered that I can actually paint OVER the label controls without it causing any problems. So those buttons above are all positioned on top of labels, and I just paint right over them, and yet I can still use for events.

I have no actual idea what I'm doing, but it is fun.

Can I ask please - when you say you've created standard windows controls, do you mean via API? I've just started delving into CreateWindowEx myself.
 
Upvote 0
Interestingly, I've just noticed that the (hard to find) NS 5.0 Dialog (forms?) seem to automagically have the Visual Styles enabled when you run the dialog. Here is the before and after.

1734887435476.png


I find this fascinating. Equally fascinating is that the Listbox control (top right) and the scrollbar control ( which is just beneath it, but you can't see it properly because it kinda autohides when it loses focus/hovering) both nonetheless have the visual styles being applied to them at design time... odd... I would add also that the Textbox control doesn't seem to behave the way you'd expect with visual styles enabled - that is, the blue bar appearing on the lower border when it gets focus.
 
Upvote 0
do you mean via API?
Yes, and to manipulate the controls I have to subclass the UserForm, which is really unstable in terms of VBE safety. The whole Excel application crashes very often due to subclassing UserForm.

In this regard, I found Kr00l's VBCCR a far better solution than creating controls using plain API, directly.

The end goal is, to create modern-looking controls with more functionality than those provided by FM20.DLL (the DLL that are used by VBA to draw forms and classic-looking controls on Windows). And we need this in VBA environment only because in others like VB6, VB.NET or VSTO, it's too easy to create controls that have modern appearance and functionality.

Unfortunately, the OCX version of Kr00l's VBCCR currently has only 32-bit version. And I've tried the claimed "safest and fastest" subclassing thunk by Paul Caton successfully on WinXP SP2 with Excel 2003, though that thunk was intended for VB5/6 only, not VBA. Still, that thunk works only in 32-bit environment. I'm looking for a thunk that's similar to Paul Caton's but can work in 64-bit environment. (Jaafar wrote a DLL in C++ for this, but it doesn't work really well in 64-bit either. I love his code, anyway)
 
Upvote 0
Hi Dan,
Just seen this ... how did you get those themed-looking controls appearing in your first post?
And what are NS 5.0 Dialogs ?
 
Upvote 0
Oops, sorry, typo - that should be MS 5 Dialog Sheets (?). Those are the Userform equivalent of the Excel 4 XLM code, I guess. They are the "Oh wow thats still there" thing hidden in Excel.

1734946989193.png


You can get to them by right clicking on the worksheet tab -> Insert -> MS 5.0 Dialog. The most information I've ever been about to find on them was in fact courtesy of Tom Urtis: Dialog Sheets – Tom Urtis

The classname for the dialog form is bosa_sdm_XL9, and the the edit control has one too, EDTBX. The command buttons, however, appear not to have a hwnd, so it's interesting that it has visual styles enabled.
 
Upvote 0
Yes, and to manipulate the controls I have to subclass the UserForm, which is really unstable in terms of VBE safety. The whole Excel application crashes very often due to subclassing UserForm.

In this regard, I found Kr00l's VBCCR a far better solution than creating controls using plain API, directly.

The end goal is, to create modern-looking controls with more functionality than those provided by FM20.DLL (the DLL that are used by VBA to draw forms and classic-looking controls on Windows). And we need this in VBA environment only because in others like VB6, VB.NET or VSTO, it's too easy to create controls that have modern appearance and functionality.

Unfortunately, the OCX version of Kr00l's VBCCR currently has only 32-bit version. And I've tried the claimed "safest and fastest" subclassing thunk by Paul Caton successfully on WinXP SP2 with Excel 2003, though that thunk was intended for VB5/6 only, not VBA. Still, that thunk works only in 32-bit environment. I'm looking for a thunk that's similar to Paul Caton's but can work in 64-bit environment. (Jaafar wrote a DLL in C++ for this, but it doesn't work really well in 64-bit either. I love his code, anyway)
I'm very familiar with Kr00l's replacement controls - they're great. They still use APIs, thankfully, because this is where I'm learning about how to create them! It's my best resource for the datepicker control and monthview controls. I've managed to get some way without delving into subclassing them:
1734947872452.png

They still have some functionality without subclassing them - just no events. It's good, and I never thought I'd manage to actually make them, so I'm pretty happy with what I've managed to do so far.

Thanks to Jaafar, I've FINALLY started to understand the whole visual styles thing, and the role of the manifest - which is how I managed to get the above 'newer' monthview control and not this one - which, I've just discovered, will not let me set the date range on it like I did with the one above.
1734948141491.png


Thunks are well beyond me. I see the word, but no idea what it actually is - all I know is that if the VB6 code uses thunks, I've got no hope of converting it to be 64bit VBA usable. Is that what The Trick used for his 64bit VBA-compatible timer: GitHub - thetrik/VbTrickTimer: CTrickTimer - the timer class for VB6/VBA compatible with 64 bit office.
 
Upvote 0
Hi Dan,
Just seen this ... how did you get those themed-looking controls appearing in your first post?
And what are NS 5.0 Dialogs ?
Hi

They are literally drawn on using the UXTheme.dll APIs - mostly DrawThemeBackground
I'm still working on trying to make a sensible class of the code I've managed to piece together. I can post it here if it would be helpful.
I've been using the VBAccelerator code/page as a main resource: vbAccelerator - Drawing with XP Visual Styles
Then using the MS documentation, and converting code from other languages: Using Visual Styles with Custom and Owner-Drawn Controls - Win32 apps
And the source code from Kr00l's controls is also helpful.
It's just tricky to make sense of what I should be doing - as you can see from the screengrab, the scrollbar and trackbar controls are not drawn in the standard way, but then I don't know what the pixel margins need to be, so I'm just making things up as I go! :ROFLMAO:
For example, pne thing I've been doing that I've not seen anywhere else (so is probably the 'wrong thing') is I've been piggybacking off the label control - I basically paint over it with the API (and it doesn't get repainted with windows messages), and yet its events still trigger perfectly, so I've been using that for a button. The Edit Control has been tricky, though. From what I can find online, it suggests drawing the control and the text with DrawThemeText, but I don't quite get how to do that, and the cursor, and make it look normal. The approach I took in the screen grab is to paint over the textbox. I explain it in the GIF below - the flickering that you see occassiionally is probably what's actually happening with the approach I've taken, though to be fair to me, you don't actually see it with the human eye. The first time I saw this flickering was right now when I uploaded the gif.
SP4AS.gif


I see that you can enable visual styles automatically as regards the entire 'application', but I don't see how that works with userforms, especially when many of our controls don't have usable hwnds. But then I saw the MS 5 Dialog, and I'm like well hang on - how come THAT gets visual styling, but not userforms...
 
Upvote 0
Thank you Dan for the explanation.

I don't think the visual styles of windowless userform controls can be changed. IMHO, the way to go (albeit not easy) is probably to enable visual styles by adding a manifest to the common control 6 assembly and then (like mentioned earlier) create the custom or owner drawn controls at runtime from the system classes (Button, Edit, Static etc). Finally, subclassing the form or subclassing the individual control(s). The subclassing (callback winproc) code could reside in a loaded dll easily written in Twinbasic to avoid potential crashings.

This is a lot of work particularly when dealing with different controls. Futhermore, I haven't seen this done before so I am not sure if it can be done or how much of it can be achieved.

One could start experimenting with Buttons first and see how it goes since other controls are trickier.
 
Upvote 0

Forum statistics

Threads
1,225,669
Messages
6,186,346
Members
453,349
Latest member
neam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top