Using WinAPI to change the color on the title bar of a UserForm

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,490
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
I search for highlight the title bar of a UserForm .
I have this code should highlight red ,but it doesn't
VBA Code:
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function SetSysColors Lib "user32" (ByVal nChanges As Long, lpSysColor As Long, lpColorValues As Long) As Long
Private originalColor As Long
Private Sub UserForm_Activate()
  originalColor = GetSysColor(2)
  Call SetSysColors(1, 2, RGB(255, 0, 0))  'RGB(255,0,0) = RED
End Sub
Private Sub UserForm_Terminate()
  Call SetSysColors(1, 2, originalColor)
End Sub
any solution experts ?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
To be honest, I think you should avoid using SetSysColor to do this - it will probably cause you more headaches in the long run. It certainly gave me some headaches when I first experimented with it in my projects. I would strongly recommend the project written by Jaafar here, which will accomplish the same goal and also give you a lot of control over the appearance of the UserForm.
 
Upvote 0
thanks Dan !

actually I saw this thread . always @Jaafar Tribak 's work is so smart & unique. but this huge code and may be cause problem with the others codes are existed in the same form and become heavy when load the form or some errors . in my mind the code above is short , may be can find out why doesn't work.

if I have to use the link you provided how can I make back color for the form is same color for the titl bar . I don't find properties inside the code to change it . I mean if I change back color the form from windows properties. when run the userform should be the same color with the title bar automatically as the userform without interfer from me .
 
Upvote 0
Sorry for the late response. After sending my earlier reply, it did actually occur to me that perhaps you would want something involving less code. I'm happy to help you with SetSysColor if you like, it's just my personal experience wasn't great, but yours might be different. One thing I didn't realise was that SetSysColor changes the colour of the item for all windows across the system, and given it was a work computer, that would've made me many (more) enemies in our IT department! :-/

how can I make back color for the form is same color for the titl bar .
I found this interesting - I really should've asked you before - what exactly is it that you're trying to accomplish? Because there may be another solution available. If all you're after is to make the colour of the title bar and the userform the same colour, one way you could accomplish that is to just get rid of the titlebar.

For example, you can take a normal userform:
1653528192312.png

Remove the titlebar (and probably the border too) and then, if you wanted to still have some form of caption, dynamically add a label with a transparent background and make its caption that of the userform, and it could look like this:
1653528309610.png

By losing the titlebar, you then lose a normal way of moving the form, but there that is easily fixed with a few lines of code. Is that what you were after? Was there any other kind of functionality you would want (like how would you want to close the userform)?
 
Upvote 0
I'm happy to help you with SetSysColor if you like,
thanks I hope so if you can.
as there any other kind of functionality you would want (like how would you want to close the userform)?
yes based on picture2 this is what I look after it . about close the userform is by commandbutton it will be enough.

if I don't answer some details you asked me for them just informe me what you want to know .
thanks for your time
 
Upvote 0
So when I try your code, it certainly does something:
1653618034877.png

Just not what you'd expect it to do...
It may be a matter of just trying other UI elements - the list is here: GetSysColor UI element list

But if you'd rather, I can turn this into a class module that you can use in your projects. It just requires 2-3 lines of code in the userform, and that is all. The example below shows what happens in the default style setting. An alternative is you can set the titlebar color to whatever you want, and it will remain unchanged. The close button is optional and can be made invisible. If this is what you're after I can get it done by tomorrow, I guess.

CustomUserform.gif
 
Upvote 0
Well, I cycled through each of the UI elements in that list out of curiosity. Though some changed other UI elements, none of them changed the title bar. You will see in that list, also, that it specifically identifies certain UI elements as being not supported for Windows 10+ (the one you selected in your code is not supported anymore).

The only methods I'm aware of then is the one Jaafar used, or the less sophisticated version I proposed above. I am curious though, so will see what other approaches have been taken.
 
Upvote 0
Great. I just wanted to give you a quick update. I've been unexpectedly busy the past two days, and now I've got to a surprise birthday party!
Anyway, it's mostly finished. It's a bit long, but the idea is that it is all contained within a class module that you just need to add to any project, and then you add maybe two-three lines of code in the userform you want to customise. So it will require very little work on your part. Hope that makes sense, and I will definitely get it uploaded for you/others this weekend.
 
Upvote 0
Also, quick question - do you know if you use 32bit Excel or 64bit Excel?
 
Upvote 0

Forum statistics

Threads
1,223,673
Messages
6,173,743
Members
452,533
Latest member
Alex19k

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