CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    [VB6] How Can I "Play" With The Windows Start Button?

    Q: What Can We Do With The System's Start Button; can We Actually Manipulate It From VB6?

    A: Well, amazingly we can do "the impossible" with the Start Button We can Hide and Show the physical Start Button, we can Disable and Enable the physical Start Button, and, wait for it... We can even change the text of the Start Button!

    Q: Cool! Where Do We Start?

    A: We need to add some APIs. These APIs will allow us to gain access to the physical Start Button window. Then, some of the APIs, such as SendMessage will allow us to manipulate the Start Button window. :

    Code:
    'The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    'The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    
    'The ShowWindow function sets the specified window's show state
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    'The EnableWindow function enables or disables mouse and keyboard input to the specified window or control.
    Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
    
    'Sends the specified message to a window or windows.
    Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    
    Private Const SW_SHOW = 5 'Shows A Window
    Private Const SW_HIDE = 0 'Hides A Window
    
    Private Const WM_SETTEXT = &HC 'Set Text of a window
    Private Const WM_GETTEXT = &HD 'Get Text of a window
    [If you have experience with the Windows APIs, especially the SendMessage API, you will notice that the SendMessage API is declared a bit differently than usual. The normal implementation of SendMessage looks like :

    Code:
    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
    Whereas ours look like :

    Code:
    Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    The reason why the implementation is a bit different, is because we need to send a String message, and this saves us time, so that we don't have to convert it to String.

    Q: How Do I Hide The Start Button?

    A: Add the following code :

    Code:
    'Hide Start Button
    Private Sub Command1_Click()
    
        Dim TaskBarWin As Long 'TaskBar Window
        Dim StartButton As Long 'Start Button Window
    
        TaskBarWin = FindWindow("shell_traywnd", vbNullString) 'Get Taskbar Window
    
        StartButton = FindWindowEx(TaskBarWin, 0&, "button", vbNullString) 'Get StartButton Window
    
        Call ShowWindow(StartButton, SW_HIDE) 'Hide It
    
    End Sub
    Q: How Do I Show The Start Button?

    A: Add the following code :

    Code:
    'Show Start Button
    Private Sub Command2_Click()
        Dim TaskBarWin As Long 'TaskBar Window
        Dim StartButton As Long 'Start Button Window
        
        TaskBarWin = FindWindow("shell_traywnd", vbNullString) 'Get Taskbar Window
    
        StartButton = FindWindowEx(TaskBarWin, 0&, "button", vbNullString) 'Get StartButton Window
            
        Call ShowWindow(StartButton, SW_SHOW) 'Show It
        
    End Sub

    Q: How Do I Disable The Start Button?

    A: Almost the same principle as the above 2 code segments, we could follow them by the letter, and just use the EnableWindow API instead of ShowWindow. I have decidedx to actually show you a different way to obtain a window's handle via the use of the FindWindowEx API only. Let us now create a Function that finds the Start Button window, then we could just call it when needed :

    Code:
    Private Function Enable_Windows_StartButton(Status As Boolean)
        
        Dim TaskBarWin As Long 'TaskBar Window
       
        'Use FindWindowEx To Find Taskbar, then Start Button
        'This Uses the CLASS of the Object to Find the Objects Handle
        TaskBarWin = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
        TaskBarWin = FindWindowEx(TaskBarWin, 0&, "Button", vbNullString)
        
        Call EnableWindow(TaskBarWin, Status) 'Enable / Disable Button
    
    End Function
    
    'Disable Start Menu
    Private Sub Command3_Click()
    
        Call Enable_Windows_StartButton(False)
    
    End Sub
    Q: How Do I Enable The Start Button?

    A: Well, it is the same as the above code segment, the only difference is that we call the Enable_Windows_StarButton Function with a TRUE parameter :

    Code:
    'Enable Start Menu
    Private Sub Command4_Click()
    
        Call Enable_Windows_StartButton(True)
    
    End Sub
    Q: How Do I Change The Start Button's Text?

    A: We follow the same principle here, we first find the main containing window, then the Start Button Window inside it. We then obtain the text the user enetered into a TextBox, and limits it 5 characters ( which is unfortunately the limit ) and send the new text to the Start Button :

    Code:
    'Changes Start Button Text
    'NewText = New Start Button Text
    Private Sub SetStartCaption(NewText As String)
    
       Dim TaskBarWin As Long 'TaskBar Window
       Dim StartButton As Long 'Start Button Window
       
       Dim NewCaption As String 'New Start Button Text ( ONLY 5 Chars )
       
       TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString) 'Get Taskbar Window
       
       StartButton = FindWindowEx(TaskBarWin, 0&, "button", vbNullString) 'Get StartButton Window
       
       NewCaption = Left(NewText, 5) 'Truncate New Text
       
       SendMessageSTRING StartButton, WM_SETTEXT, 256, NewCaption 'Apply New Text
       
       Exit Sub
       
       End Sub
    
    
    'Set Button Text
    Private Sub Command5_Click()
        
        SetStartCaption Text1.Text
    
    End Sub
    full working sample is attached to this post.
    Attached Files Attached Files

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured