VB and VBA Users Source Code: Dynamically Setting the Application Task Text in the Task Manager (API equivalent to App.Title)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Dynamically Setting the Application Task Text in the Task Manager (API equivalent to App.Title)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, May 04, 2005
Hits:
1942
Category:
Windows Forms/GUI/Controls/Graphics
Article:
The VB App.Title property sets the task manager title of an application, but it only allows you a maximum of 40 characters. The code below gets and sets the application title bar (or taskbar title), but does not have a 40 character limit. Option Explicit Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long Private Const GWL_HWNDPARENT As Long = (-8) Private zlAppHandlerHwnd As Long 'Purpose : Gets the application title text which appears in the task bar. 'Inputs : [lHwndWindow] The handle of the window to get the taskbar title bar text of. ' If not specified, gets the AppTitle of the current application. 'Outputs : Returns the Task Bar title text of the specified window, or an empty string. 'Author : Andrew Baker 'Date : 01/11/2000 13:17 'Notes : Doesn't suffer from the 40 character limit that App.Title has. 'Revisions : 'Assumptions : Public Function AppTitleGet(Optional lHwndWindow As Long) Dim lTestHwnd As Long, lEndPos As Long, lHwndTopMost As Long Dim sBuffer As String * 255 AppTitleGet = "" If lHwndWindow Then 'Has specified a window handle, get the topmost window lHwndTopMost = lHwndWindow lTestHwnd = GetWindowLong(lHwndWindow, GWL_HWNDPARENT) Do While lTestHwnd <> 0 lHwndTopMost = lTestHwnd lTestHwnd = GetWindowLong(lHwndTopMost, GWL_HWNDPARENT) Loop Else 'No window handle specified, find the window handler for the current application If zlAppHandlerHwnd = 0 Then Dim sOldTitle As String 'Find the window handler '(Note, every VB application has a hidden top-level window to which all messages and events are 'initially sent. This window is derived from the class called ThunderRT6Main. This window owns all other VB forms in the application.) 'First make the title unique sOldTitle = App.Title App.Title = Left$(App.Title, 32) & " " & GetCurrentProcessId zlAppHandlerHwnd = FindWindow("ThunderRT6Main", App.Title) If zlAppHandlerHwnd = 0 Then 'Running the VB IDE (in debug mode) zlAppHandlerHwnd = FindWindow("ThunderMain", App.Title) End If 'Restore the title App.Title = sOldTitle End If lHwndTopMost = zlAppHandlerHwnd End If If lHwndTopMost Then 'The handle is the topmost, get the windowtext lEndPos = GetWindowText(lHwndTopMost, sBuffer, Len(sBuffer)) AppTitleGet = Left(sBuffer, lEndPos) End If End Function 'Purpose : Sets the application title text which appears in the task bar. 'Inputs : sTitleText The text to set the taskbar or application title to. ' [lHwndWindow] The handle of the window to set the task bar title bar text of. ' If not specified, sets the AppTitle of the current application. 'Outputs : N/A 'Author : Andrew Baker 'Date : 01/11/2000 13:17 'Notes : Doesn't suffer from the 40 character limit that App.Title has. 'Revisions : 'Assumptions : Public Sub AppTitleSet(sTitleText As String, Optional lHwndWindow As Long) Dim lTestHwnd As Long, lHwndTopMost As Long If lHwndWindow Then 'Has specified a window handle, get the topmost window lHwndTopMost = lHwndWindow lTestHwnd = GetWindowLong(lHwndWindow, GWL_HWNDPARENT) Do While lTestHwnd <> 0 lHwndTopMost = lTestHwnd lTestHwnd = GetWindowLong(lHwndTopMost, GWL_HWNDPARENT) Loop Else 'No window handle specified, find the window handler for the current application If zlAppHandlerHwnd = 0 Then Dim sOldTitle As String 'Find the window handler '(Note, every VB application has a hidden top-level window to which all messages and events are 'initially sent. This window is derived from the class called ThunderRT6Main. This window owns all other VB forms in the application.) 'First make the title unique sOldTitle = App.Title App.Title = Left$(App.Title, 32) & " " & GetCurrentProcessId zlAppHandlerHwnd = FindWindow("ThunderRT6Main", App.Title) If zlAppHandlerHwnd = 0 Then 'Running the VB IDE (in debug mode) zlAppHandlerHwnd = FindWindow("ThunderMain", App.Title) End If 'Restore the title App.Title = sOldTitle End If lHwndTopMost = zlAppHandlerHwnd End If If lHwndTopMost Then 'The handle is the topmost SetWindowText lHwndTopMost, sTitleText End If End Sub 'Demonstration routine Sub Test() 'Set taskbar title Call AppTitleSet(AppTitleGet() & " - VERY LONG TEXT BLAH BLAH BLAH BLAH BLAH. ") 'Set taskbar title again (to check the 40 character limit fix works) Call AppTitleSet(AppTitleGet() & " END.") End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder