Can you change the color of the titlebar of a userform in VBA using Windows API?
17,760
Just for fun;
UserForm:
Private gHWND As Long
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then HandleDragMove gHWND
End Sub
Private Sub UserForm_Initialize()
gHWND = Setup(Me)
End Sub
Private Sub UserForm_Click()
Unload Me
End Sub
*.BAS
Option Explicit
Private Const WM_NCLBUTTONDOWN = &HA1&
Private Const HTCAPTION = 2&
Private Const GWL_STYLE = (-16)
Private Const WS_BORDER = &H800000
Private Const WS_DLGFRAME = &H400000
Private Const WS_CAPTION = WS_BORDER Or WS_DLGFRAME
Private Declare Sub ReleaseCapture Lib "User32" ()
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal HWND As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal HWND As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Function Setup(objForm As Object) As Long
Setup = FindWindow("ThunderDFrame", objForm.Caption)
SetWindowLong Setup, GWL_STYLE, GetWindowLong(Setup, GWL_STYLE) And Not WS_CAPTION
End Function
Public Sub HandleDragMove(HWND As Long)
Call ReleaseCapture
Call SendMessage(HWND, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End Sub
(Would need mod for 64bit Office)
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on June 04, 2022Comments
-
Admin about 2 years
Is it possible to change the color of the title bar for a VBA userform using Windows API. Please note that I am only interested in changing the color of the title bar for a particular userform and not a system-wide them change. Thanks!
-
Admin about 10 yearsThis is great! Thanks very much
-
tbur about 10 yearsWell, color me impressed!
-
Mertinc about 7 yearsAlex, I don't know what's the difference with VBA's Module and that /*.BAS/ of Windows API (I'm not so experienced) but I tried the same thing on my
VBA User Form
but it doesn't work. Firstly it doesn't recognizegHWND
variable, I don't know why. (I defined it asDim gHWND as Long
beforegHWND = Setup(Me)
) And I write that*.BAS
part of your code into myModule8
But then the only thing happens on my userform when I run it is, instead of changing the color, Title Bar Disappears Why it's acting like this? How can I run it correctly? That seems wonderful by the way, cheers. -
Mertinc about 7 yearsI should also mention that, I have another User Form as a
Splash Screen
of my workbook. And in that splash screen I have aPublic Sub HideTitleBar
macro but it's in different Module and I don't think they have any duplicate variable so that your macro confusing with it and running that one instead of your one. -
Mertinc about 7 yearsSorry for consecutive comments Alex, but I think I now understood what you have done! You are not
changing the title
, but you are onlyhiding title bar
, then the green thing we see as a title isa green fancy picture
located at the top of user form, andGreen Machine Label
over that picture. Picture also has aSpecial Effect 6- (Bump)
to show it like Title :) wow that's smart way actually. -
Alex K. about 7 yearsYep, the green stuff at the top is an image in an image control, the white text is a label control who's mousemove event is used to allow dragging.