Results 1 to 8 of 8

Thread: Remove UserForm's TitleBar And Frame

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13

    Remove UserForm's TitleBar And Frame

    Every now and again, someone on a forum requests to have the title bar removed from their UserForm. The reason may simply be to remove the Exit Button (the red X in the upper right corner) or to get rid of the windows framework which is deemed "ugly" given the UserForm's design. Actually, with some simple Windows API function calls (sorry Mac users, but this code will not work for you), removing the title bar and windows border is quite easy to do. Of course, without a title bar, your user will not be able to move the UserForm around the screen. Again, the Windows API functions come to the rescue... setting up the ability to move a UserForm having no title bar is also quite easy to do. Simply put this code into your UserForm's code window and you are done.

    Code:
    '**** Start of API Calls To Remove The UserForm's Title Bar ****
    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
      
    
    Private Declare Function DrawMenuBar Lib "user32" _
                   (ByVal hWnd As Long) As Long
    '**** End of API Calls To Remove The UserForm's Title Bar ****
    
    '**** Start of API Calls To Allow User To Slide UserForm Around The Screen ****
    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 ReleaseCapture Lib "user32" () As Long
     
    
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Const HTCAPTION = 2
    '**** End of API Calls To Allow User To Slide UserForm Around The Screen ****
    
    Dim hWndForm As Long
    
    Private Sub UserForm_Initialize()
       Dim Style As Long, Menu As Long
       hWndForm = FindWindow("ThunderDFrame", Me.Caption)
       Style = GetWindowLong(hWndForm, &HFFF0)
       Style = Style And Not &HC00000
       SetWindowLong hWndForm, &HFFF0, Style
       DrawMenuBar hWndForm
    End Sub
    
    Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
      If Button = xlPrimaryButton And Shift = 1 Then
        Call ReleaseCapture
        Call SendMessage(hWndForm, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&)
      End If
    End Sub
    Note 1
    ------------
    Important -- make sure to provide an "Exit" button for closing the UserForm down (remember, there will be no Exit Button on the Window frame for the users to use to do this, so you have to provide one for them).

    Note 2
    -----------
    To slide the UserForm around, hold the Shift Key down, left click a blank area of the UserForm (NOT a control on the UserForm) and with the left mouse button still depressed, slide the mouse around the screen until you have it located where you want. I chose to use the Shift Key as part of this functionality for its mnemonic value (Shift Key = Shift UserForm Around), but this can be changed as needed in the If..Then statement inside the UserForm_MouseDown event procedure.
    Last edited by Rick Rothstein; 08-29-2012 at 10:33 PM.

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Great solution works perfectly

    Last edited by DocAElstein; 09-22-2023 at 05:34 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2016
    Posts
    2
    Rep Power
    0
    Rick

    I know only enough to be dangerous, so bear with me. To adapt your code above to also run/work on a 64-bit excel, would "PtrSafe" need to be added between the words Declare... Function each time it is mentioned above? Is that all that is needed?
    I do not have the 64-bit version of Excel, so I cannot check, but that does sound right from the reading I have done.



    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg. 9fxrOrrvTln9g9wr8mv2CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg. 9fz3_oaiUeK9g96yGbAX4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg. 9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg. 9g9wJCunNRa9gJGhDZ4RI2
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugz-pow-E8FDG8gFZ4l4AaABAg.9f8Bng22e5d9f8hoJGZY-5
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxev2gQt7BKZ0WYMfh4AaABAg. 9f6hAjkC0ct9f8jleOui-u
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxg9iT7MPWGBWruIzR4AaABAg
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 07-12-2023 at 05:19 PM.

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by birdieman View Post
    Rick

    To adapt your code above to also run/work on a 64-bit excel, would "PtrSafe" need to be added between the words Declare... Function each time it is mentioned above? Is that all that is needed?
    I don't have the 64-bit version of Excel, so I cannot check, but as far as I know, yes, that all you need to do.

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/@alanelston2330
    https://www.youtube.com/watch?v=yXaYszT11CA&lc=UgxEjo0Di9-9cnl8UnZ4AaABAg.9XYLEH1OwDIA35HNIei0z-
    https://eileenslounge.com/viewtopic.php?p=316154#p316154
    https://www.youtube.com/watch?v=TW3l7PkSPD4&lc=UgwAL_Jrv7yg7WWC8x14AaABAg
    https://teylyn.com/2017/03/21/dollarsigns/#comment-191
    https://eileenslounge.com/viewtopic.php?p=317050#p317050
    https://eileenslounge.com/viewtopic.php?f=27&t=40953&p=316854#p316854
    https://www.eileenslounge.com/viewtopic.php?v=27&t=40953&p=316875#p316875
    https://eileenslounge.com/viewtopic.php?p=316057#p316057
    https://eileenslounge.com/viewtopic.php?p=316705#p316705
    https://eileenslounge.com/viewtopic.php?p=316704#p316704
    https://eileenslounge.com/viewtopic.php?p=176255#p176255
    https://eileenslounge.com/viewtopic.php?f=27&t=40919&p=316597#p316597
    https://eileenslounge.com/viewtopic.php?p=316412#p316412
    https://eileenslounge.com/viewtopic.php?p=316254#p316254
    https://eileenslounge.com/viewtopic.php?p=316280#p316280
    https://eileenslounge.com/viewtopic.php?p=315915#p315915
    https://eileenslounge.com/viewtopic.php?p=315512#p315512
    https://eileenslounge.com/viewtopic.php?p=315744#p315744
    https://www.eileenslounge.com/viewtopic.php?p=315512#p315512
    https://eileenslounge.com/viewtopic.php?p=315680#p315680
    https://eileenslounge.com/viewtopic.php?p=315743#p315743
    https://www.eileenslounge.com/viewtopic.php?p=315326#p315326
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40752
    https://eileenslounge.com/viewtopic.php?p=314950#p314950
    https://www.eileenslounge.com/viewtopic.php?p=314940#p314940
    https://www.eileenslounge.com/viewtopic.php?p=314926#p314926
    https://www.eileenslounge.com/viewtopic.php?p=314920#p314920
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 05-16-2024 at 02:52 PM.

  5. #5
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14
    Excel version 2016 64 Bit, the code has been adapted to operate in both versions

    As Userform1
    Code:
    '**** Start of API Calls To Remove The UserForm's Title Bar ****
    Option Explicit
    
    #If VBA7 Then
        Private Declare PtrSafe Function FindWindow Lib "user32" _
                    Alias "FindWindowA" _
                   (ByVal lpClassName As String, _
                    ByVal lpWindowName As String) As Long
      
    
        Private Declare PtrSafe Function GetWindowLong Lib "user32" _
                    Alias "GetWindowLongA" _
                   (ByVal hWnd As Long, _
                    ByVal nIndex As Long) As Long
      
    
        Private Declare PtrSafe Function SetWindowLong Lib "user32" _
                    Alias "SetWindowLongA" _
                   (ByVal hWnd As Long, _
                    ByVal nIndex As Long, _
                    ByVal dwNewLong As Long) As Long
      
    
        Private Declare PtrSafe Function DrawMenuBar Lib "user32" _
                   (ByVal hWnd As Long) As Long
    '**** End of API Calls To Remove The UserForm's Title Bar ****
    
    '**** Start of API Calls To Allow User To Slide UserForm Around The Screen ****
        Private Declare PtrSafe 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 PtrSafe Function ReleaseCapture Lib "user32" () As Long
    #Else
        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
      
    
        Private Declare Function DrawMenuBar Lib "user32" _
                   (ByVal hWnd As Long) As Long
    '**** End of API Calls To Remove The UserForm's Title Bar ****
    
    '**** Start of API Calls To Allow User To Slide UserForm Around The Screen ****
        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 ReleaseCapture Lib "user32" () As Long
    #End If
    
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Const HTCAPTION = 2
    '**** End of API Calls To Allow User To Slide UserForm Around The Screen ****
    
    Dim hWndForm As Long
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
       Dim Style As Long, Menu As Long
       hWndForm = FindWindow("ThunderDFrame", Me.Caption)
       Style = GetWindowLong(hWndForm, &HFFF0)
       Style = Style And Not &HC00000
       SetWindowLong hWndForm, &HFFF0, Style
       DrawMenuBar hWndForm
    End Sub
    
    Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
      If Button = xlPrimaryButton And Shift = 1 Then
        Call ReleaseCapture
        Call SendMessage(hWndForm, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&)
      End If
    End Sub

  6. #6

  7. #7
    Junior Member
    Join Date
    Apr 2020
    Posts
    1
    Rep Power
    0
    I have been delighted with this code and have used in on several userforms with striking results. I do have a question (a challenge!) for you though. Is it possible keep the titlebar, and instead make "active?" I don't even know if that's the right term. Specifically, I need the form I have in mind to show today's date. But the form is very "busy." I wondered if, instead of putting yet another textbox on the form itself, if I could get the date to show in the titlebar in place of or next to the userform title?

    thank you.

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/@alanelston2330
    https://www.youtube.com/watch?v=yXaYszT11CA&lc=UgxEjo0Di9-9cnl8UnZ4AaABAg.9XYLEH1OwDIA35HNIei0z-
    https://eileenslounge.com/viewtopic.php?p=316154#p316154
    https://www.youtube.com/watch?v=TW3l7PkSPD4&lc=UgwAL_Jrv7yg7WWC8x14AaABAg
    https://teylyn.com/2017/03/21/dollarsigns/#comment-191
    https://eileenslounge.com/viewtopic.php?p=317050#p317050
    https://eileenslounge.com/viewtopic.php?f=27&t=40953&p=316854#p316854
    https://www.eileenslounge.com/viewtopic.php?v=27&t=40953&p=316875#p316875
    https://eileenslounge.com/viewtopic.php?p=316057#p316057
    https://eileenslounge.com/viewtopic.php?p=316705#p316705
    https://eileenslounge.com/viewtopic.php?p=316704#p316704
    https://eileenslounge.com/viewtopic.php?p=176255#p176255
    https://eileenslounge.com/viewtopic.php?f=27&t=40919&p=316597#p316597
    https://eileenslounge.com/viewtopic.php?p=316412#p316412
    https://eileenslounge.com/viewtopic.php?p=316254#p316254
    https://eileenslounge.com/viewtopic.php?p=316280#p316280
    https://eileenslounge.com/viewtopic.php?p=315915#p315915
    https://eileenslounge.com/viewtopic.php?p=315512#p315512
    https://eileenslounge.com/viewtopic.php?p=315744#p315744
    https://www.eileenslounge.com/viewtopic.php?p=315512#p315512
    https://eileenslounge.com/viewtopic.php?p=315680#p315680
    https://eileenslounge.com/viewtopic.php?p=315743#p315743
    https://www.eileenslounge.com/viewtopic.php?p=315326#p315326
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40752
    https://eileenslounge.com/viewtopic.php?p=314950#p314950
    https://www.eileenslounge.com/viewtopic.php?p=314940#p314940
    https://www.eileenslounge.com/viewtopic.php?p=314926#p314926
    https://www.eileenslounge.com/viewtopic.php?p=314920#p314920
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 05-16-2024 at 02:56 PM.

  8. #8
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Sorry for the long delay in answering your question. Actually, I am not sure I know how to do what you asked for; however, given you are using a Windows' computer, why isn't the date and time displayed in the Windows Toolbar sufficient?


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 06-12-2023 at 05:50 PM.

Similar Threads

  1. Remove Special Characters From Text Or Remove Numbers From Text
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 5
    Last Post: 05-31-2013, 04:43 PM
  2. One userform and 60 Combo Boxes
    By k0st4din in forum Excel Help
    Replies: 11
    Last Post: 04-25-2013, 09:55 AM
  3. Multiple graph in userform
    By Tony in forum Excel Help
    Replies: 3
    Last Post: 10-20-2012, 09:32 AM
  4. Replies: 3
    Last Post: 12-07-2011, 09:59 PM
  5. Userform With DataGrid or FlexGrid
    By Rasm in forum Excel Help
    Replies: 5
    Last Post: 04-09-2011, 09:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •