Create a Status Bar using the API
I will take you through the functions found in the form's code. The first, create (found under the command button cmdCreate) is, quite obviously, used to create the status bar. When creating it, we need to apply (as a minimum) the WS_CHILD and WS_VISIBLE constants. These force the status bar to be visible and make it a child of our form. If you omitted the WS_CHILD constants the status bar would draw on the desktop. You might want this, although I doubt your users would! Anyway, after that we use the MoveWindow API call. By default, the status bar assumes its own size/position. A call to the MoveWindow function ensures that we can see our status bar.
The next part, AddCaption (found under cmdAddCaption) changes the text in the status bar. I had some problems using this, mainly getting it to draw text in different panels. You will see what I mean when you use it. Anyway, this uses a small routine to apply new text. We use the SendMessage call to apply the new text using the SB_SETTEXT constant. We also include the style for the panel. I have used the default SBT_SUNKEN style but you could use the SBT_POPOUT (included) or other styles supported.
We now apply a font to the status bar. By using the WM_GETFONT we can get the font of our form (set to Tahoma) and then use the WM_SETFONT and SendMessage to apply the font. If you were wrapping this in an activex control, then you would be getting the font of the usercontrol. You can get the font of any object that has a hWnd property.
Notice in the Form_Resize event I have used the MoveWindow call to resize the status bar. If we didn't do this, then the status bar would just stay the same size both in length and width.
The Add function (found under cmdAdd) basically adds a new panel using the SB_SETPARTS and the SendMessage. First, we have to define the size of this panel and then call the SendMessage function. The destroy function just calls the DestroyWindow with a reference to the status bars window handle.
The form requires six command buttons (although this could be scaled down). Rename them cmdDestroy, cmdCreate, cmdAddCaption, cmdFont, cmdAdd and cmdGetText. Copy and paste the code below into the form:
Option Explicit '// Holds the window handle for our status bar Private lnghWnd As Long Private Sub cmdCreate_Click() Dim lngStyle As Long 'WS_CHILD - Makes the status bar a child of 'this form. 'WS_VISIBLE - Makes the statusbar visible lngStyle = lngStyle Or WS_CHILD lngStyle = lngStyle Or WS_VISIBLE ' Make the statusbar using the ' CreateStatusWindow API Call ' Note: This is much easier than using ' the CreateWindowEx API lnghWnd = CreateStatusWindowA _ (lngStyle, "VB Square API Drawn _ Statusbar", Me.hWnd, 0) ' By default, our new status bar assumes ' its own position and ' size. Use the MoveWindow API to move it ' to the bottom of the form MoveWindow lnghWnd, 0, ScaleHeight - 20, _ ScaleWidth, 20, True End Sub Private Sub cmdAddCaption_Click() Dim strText As String ' Sets the caption for both panels using a ' string from an input box ' For some reason the text doesn't draw ' If anyone knows why, please let me know strText = InputBox$ _ ("Enter a caption...") SetCaption strText cmdFont_Click End Sub Private Sub cmdFont_Click() Dim lRet As Long ' Set the font of the status bar using ' the WM_SETFONT by getting the font from ' this form using the WM_GETFONT lRet = SendMessage(Me.hWnd, WM_GETFONT, 0, 0) SendMessage lnghWnd, WM_SETFONT, lRet, 0 End Sub Private Sub cmdAdd_Click() Dim lngParts As Long ' Holds the size of the new panel lngParts = ScaleWidth - 100 ' Adds the new panel to our status bar SendMessage lnghWnd, SB_SETPARTS, _ ByVal 2, lngParts End Sub Private Sub cmdDestroy_Click() ' Use the DestroyWindow API to destroy ' our status bar DestroyWindow lnghWnd ' Set the window handle holder to 0 lnghWnd = 0 End Sub Private Sub cmdGetText_Click() Dim pnlNum As Long ' Find out which panel the user wants ' the text from pnlNum = Val _ (InputBox("Text from panel number:")) ' Return the text using the GetText function MsgBox GetText(pnlNum) End Sub Private Sub Form_Load() ' Move the form to the centre of the screen Move (Screen.Width - Width) * 0.5, _ (Screen.Height - Height) * 0.5 ' Set the scalemode to pixels because ' certain functions require this. ScaleMode = vbPixels End Sub Private Sub Form_Resize() ' Use the WM_SIZE constant to resize the ' status bar to keep it at the bottom ' of the form SendMessage lnghWnd, WM_SIZE, 0, 0 End Sub Function GetPanels() As Integer ' Returns the number of panels GetPanels = SendMessage _ (lnghWnd, SB_GETPARTS, 0, ByVal 0) End Function Sub SetCaption(strText As String) Dim lngRet As Long ' Set some new text in the status bar using ' the SM_SETTEXT. Use the SBT_ constants ' to define how the panel looks. lngRet = GetPanels() - 1 SendMessage lnghWnd, SB_SETTEXT, _ ByVal SBT_SUNKEN, ByVal strText SendMessage lnghWnd, SB_SETTEXT, _ ByVal lngRet Or SBT_SUNKEN, ByVal strText End Sub Function GetTextLen(pnlNum As Long) As Long Dim lngRet As Long ' Returns the lenght of the string ' in the panel lngRet = SendMessage _ (lnghWnd, SB_GETTEXTLENGTH, ByVal pnlNum, 0) GetTextLen = (lngRet And &HFFFF&) End Function Function GetText(pnlNum As Long) As String Dim strText As String Dim lngRet As Long ' Returns the string in the panel ' specificed by pnlNum strText = String$(GetTextLen(pnlNum), 0) lngRet = SendMessage(lnghWnd, SB_GETTEXT, _ ByVal pnlNum, ByVal strText) GetText = Left$(strText, _ (lngRet And &HFFFF&)) End Function
Page 3 of 3