Can you change the color of the titlebar of a userform in VBA using Windows API?

17,760

Just for fun;

enter image description here

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)

Share:
17,760
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    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
    Admin about 10 years
    This is great! Thanks very much
  • tbur
    tbur about 10 years
    Well, color me impressed!
  • Mertinc
    Mertinc about 7 years
    Alex, 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 recognize gHWND variable, I don't know why. (I defined it as Dim gHWND as Long before gHWND = Setup(Me) ) And I write that *.BAS part of your code into my Module8 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
    Mertinc about 7 years
    I should also mention that, I have another User Form as a Splash Screen of my workbook. And in that splash screen I have a Public 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
    Mertinc about 7 years
    Sorry for consecutive comments Alex, but I think I now understood what you have done! You are not changing the title, but you are only hiding title bar, then the green thing we see as a title is a green fancy picture located at the top of user form, and Green Machine Label over that picture. Picture also has a Special Effect 6- (Bump) to show it like Title :) wow that's smart way actually.
  • Alex K.
    Alex K. about 7 years
    Yep, 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.