Visual Basic Window/Form Routines
DrawText: Adding Title Text to the Fake TitleBar
     
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

DrawFocusRect: Simulating Non-Client Form Movement
     
 Prerequisites
Form and module from the project developed in DrawFocusRect: Simulating Non-Client Form Movement.

This code shows how to use the DrawText API to add a caption to the fake titlebar.
 BAS Module Code
Place the following API declare code into the general declarations area of a bas module. If this is a one-form project, the declares below could be placed into the general declaration section of the form instead, with all Public references changed to Private.

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const COLOR_CAPTIONTEXT = 9
Public Const DT_CENTER = &H1      'centre left to right
Public Const DT_VCENTER = &H4     'centre top to bottom
Public Const DT_NOCLIP = &H100    'fast draw
Public Const DT_SINGLELINE = &H20 'single line only
Public Const DT_FLAGS = DT_SINGLELINE Or DT_CENTER Or DT_VCENTER Or DT_NOCLIP

Public Declare Function DrawText Lib "user32" _
    Alias "DrawTextA" _
    (ByVal hdc As Long, _
     ByVal lpStr As String, _
     ByVal nCount As Long, _
     lpRect As RECT, _
     ByVal wFormat As Long) As Long
 Form Code
To the form code in the previous article, add the following code to the form:

Option Explicit

Private Sub Form_Load()

   Dim frameHeight As Long
   Dim frameWidth As Long
   Dim btnSize As Integer

   Toolbar.ScaleMode = 3
   
  'compute the width of the left and right dialog frame   
   frameHeight = GetSystemMetrics(SM_CYDLGFRAME) * 2
   
  'compute the width of the top and bottom dialog frame   
   frameWidth = GetSystemMetrics(SM_CXDLGFRAME) * 2
   
  'get the size of one of the square toolbar buttons   
   btnSize = SSCommand1(0).Width
   
  'set the tool window size    
   Toolbar.Height = ((btnSize * 4) + _
                      frameHeight + _
                      Picture1.Height + 1) * Screen.TwipsPerPixelY
   Toolbar.Width = ((btnSize * 3) + _
                      frameWidth) * Screen.TwipsPerPixelX

  'Use active caption color for label's caption:  
   Picture1.ForeColor = GetSysColor(COLOR_CAPTIONTEXT)
      
  'Choose a small font or whatever looks best on your system
   Picture1.FontName = "Arial"
   Picture1.FontSize = 7
   Picture1.FontBold = False

   Toolbar.ScaleMode = 1
   
  'set the mock titlebar color to that of an active window   
   Picture1.BackColor = GetSysColor(COLOR_ACTIVECAPTION)

End Sub


'Add the following new code to the ToolBar form:

Private Sub Form_Resize()
     
  'Change size of Picture1     
   Picture1.Width = Me.ScaleWidth
    
   Toolbar.ScaleMode = 3
     
  'Draw the word "Toolbar" into the fake picturebox titlebar     
   Dim rc As RECT
    
   rc.Left = Picture1.Left
   rc.Top = Picture1.Top
   rc.Right = rc.Left + Picture1.Width
   rc.Bottom = rc.Top + Picture1.Height
    
   Picture1.Cls
    
   Call DrawText(Picture1.hdc, "Toolbar", 7, rc, DT_FLAGS)
    
   Toolbar.ScaleMode = 1
  
End Sub
 Comments
The DrawText API takes the following parameters:

   DrawText (hdc, lpStr, nCount, lpRect As RECT, wFormat)

   hdc    Picture1.hdc - Handle to the display context of the device to draw the text onto
   lpStr    "Toolbar" - The text to print
   7    - the number of characters in the text string
   lpRect As RECT    rc - the rectangle for the output text to be drawn into
   wFormat    DT_FLAGS - the DrawText flags.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter