- User Features
- Developer Features
- Making a Splash
- Get Into Shape
- Wrap Up
Get Into Shape
Listing 6 shows the ShapeForm subroutine. It begins by defining a series of constants that determine the form's appearance. After a bunch of variable declarations, the routine turns on AutoRedraw, so the drawing it does is permanent. It also sets the form's ScaleMode to vbPixels. API functions work in pixels, so setting ScaleMode to pixels makes things easier.
ShapeForm then uses the CustomFont function to create a customized font. If you look at the constants defined at the beginning of the routine, you'll find that this is the font named Westminster and has a height of 350 pixels. Setting the font's width to zero makes the font mapper select a width that matches the height.
Before you select a font for your splash screen, make sure that the font is installed on your customers' computers. If their systems don't have Westminster loaded, it's not clear what font the system will give you. If you don't know what fonts will be available on your customers' computers, stick to Arial, Courier New, and Times New Roman.
Next, ShapeForm uses the SelectObject API function to install the new font on the form. If it were to draw text now, the text would use this font.
ShapeForm then uses the GetTextExtentPoint API function to see how big the text defined by the TEXT1 constant (in this case, Splash!) would be in this font. Normally, you could use the form's TextWidth and TextHeight functions to find the text's dimensions, but those functions are not reliable when you install a custom font.
The program resizes the form so it is big enough to hold the text. It also centers the form onscreen. This example ignores the taskbar, so it may overlap the system's taskbar if it is too large.
Next, ShapeForm calls the BeginPath API function to start recording a path. It draws the text using the new font, and calls EndPath to close the path. This makes a path that represents the text's outline. ShapeForm calls the PathToRegion API function to convert the path into a Windows region.
The routine then uses CreateRoundRectRgn to make another region that represents the rounded rectangle that holds the splash form's version and copyright information. It calls CombineRgn to turn the two regions into a single combined region.
Now, the program calls SetWindowRgn to constrain the form to the combined region. That cuts the form into the interesting shape you see in Figures 1 and 2.
After cutting the form into the right shape, ShapeForm paints the form. It starts by using GetTextMetrics to get detailed information about the new font. The font's internal leading measurement tells how much empty space lies above the text when you draw using the font.
The program starts drawing colored horizontal lines across the form starting the internal leading distance down from the top of the form. That prevents the program from drawing on areas above the text that are clipped off. Drawing on those areas wouldn't cost the program much time, but it would make the lines' shading start deep in the hidden area and the bluest lines would be hidden. Starting down the form a bit makes the lines shade nicely from bright blue to bright red. For more information on font metrics, see http://www.vb-helper.com/vbgp.htm again.
If you stopped here, you would see a form shaded from blue to red and cut out to fit a rounded rectangle and the text Splash! That's pretty cool, but there's more work to do before the form looks like Figure 2.
ShapeForm again calls BeginPath, draws the text, and calls EndPath to make another path that follows the text's outline. Instead of converting the path into a region, this time the program sets the form's DrawWidth and ForeColor properties, and calls the StrokePath API function. StrokePath draws the text's outline.
If you look at the routine's constants, you'll see that DRAW_WIDTH is 6. It's probably impossible to tell from Figure 2, but the borders around the text are only three pixels wide because the routine clipped the form to the same path that defines the text outline. When the routine calls StrokePath, the six-pixel-wide line sits squarely along the Window region's path. Half of the line lies outside the clipped area so you only see three pixels; the other three are chopped off.
The program uses RoundRect to draw the rounded rectangle containing the program's version and copyright information. Again half of the six-pixel-wide rounded rectangle is chopped off, so you only see three black pixels.
Next, ShapeForm creates a new font to display the http://www.vb-helper.com URL. It calls function CustomFont to make an Arial Black font rotated 90 degrees. If you don't have the Arial Black font on your system, use another font.
ShapeForm selects the new font, and uses GetTextExtentPoint to see how much room the URL will take up using this font. It saves the coordinates of the rectangle that will contain the font in the m_HotSpotXmin, m_HotSpotXmax, m_HotSpotYmin, and m_HotSpotYmax variables.
Notice that the routine uses the text's height (sz.cy) to calculate the x coordinate, and uses the text's width (cz.cx) to calculate the y coordinate. This seems backward and it is, but GetTextExtentPoint doesn't realize the font is rotated. Because the font is rotated 90 degrees, its height and width are switched. If you were rotating the text by some other angle such as 60 degrees, you would need to do some calculations to see where the text's bounding box was.
After performing these calculations, ShapeForm draws the URL text. It then uses SelectObject to restore the form's original font, and uses Delete object to destroy the fonts it created. This is extremely important. Fonts use up system resources. If you don't free the space used by the fonts, those resources are lost. Eventually, your system may run low on system resources, and crash.
At this point, ShapeForm is done with tricky API calls. Drawing the program's version and copyright information is much easier. The program selects a new font using Visual Basic's normal Font.Name, Font.Size, and Font.Bold properties. It positions the text and draws it using normal Visual Basic methods. The only complication is calculating the text's position so it is centered in the rounded rectangle.
Listing 6 The ShapeForm Subroutine Cuts the Form Down to Size
' Shape the login window. Private Sub ShapeForm() Const TEXT1 = "Splash!" Const FONT_NAME1 = "Westminster" Const TEXT_HGT1 = 350 Const TEXT_WID1 = 0 Const FONT_NAME2 = "Arial Black" Const TEXT_HGT2 = 15 Const TEXT_WID2 = 0 Const FONT_NAME3 = "Times New Roman" Const TEXT_HGT3 = 15 Const TEXT_WID3 = 0 Const DRAW_WIDTH = 6 Const RR_X1 = 230 Const RR_Y1 = 275 Const RR_X2 = 780 Const RR_Y2 = 347 Const RR_HGT = 20 Const RR_WID = 20 Dim font1 As Long Dim font2 As Long Dim origfont As Long Dim h_rgn1 As Long Dim h_rgn2 As Long Dim sz As Size Dim wid_pixels As Single Dim hgt_pixels As Single Dim wid_twips As Single Dim hgt_twips As Single Dim tm As TEXTMETRIC Dim Y As Single Dim clr As Single Dim dclr As Single Dim txt As String ' Prepare the form. AutoRedraw = True ScaleMode = vbPixels ' Get the size of the text. font1 = CustomFont(TEXT_HGT1, TEXT_WID1, 0, 0, _ FW_BOLD, False, False, False, _ FONT_NAME1) origfont = SelectObject(hdc, font1) GetTextExtentPoint hdc, TEXT1, Len(TEXT1), sz wid_pixels = sz.cx hgt_pixels = sz.cy ' Make the form big enough. wid_twips = ScaleX(wid_pixels, vbPixels, vbTwips) hgt_twips = ScaleY(hgt_pixels, vbPixels, vbTwips) Move (Screen.Width - wid_twips) / 2, _ (Screen.Height - hgt_twips) / 2, _ wid_twips, hgt_twips ' Make the text region. BeginPath hdc CurrentX = 0 CurrentY = 0 Print TEXT1 EndPath hdc h_rgn1 = PathToRegion(hdc) ' Create a rounded rectangle region. h_rgn2 = CreateRoundRectRgn(RR_X1, RR_Y1, RR_X2, RR_Y2, _ RR_WID, RR_HGT) ' Combine two regions. CombineRgn h_rgn1, h_rgn1, h_rgn2, RGN_OR ' Constrain the form to the region. ' Note: Do not destroy the region to free ' resources. The window owns the region now ' and it will handle this as necessary. SetWindowRgn hwnd, h_rgn1, False ' Fill the text with color. GetTextMetrics hdc, tm dclr = 255 / (hgt_pixels - tm.tmInternalLeading + 1) DrawWidth = 1 For Y = tm.tmInternalLeading To hgt_pixels Line (0, Y)-Step(wid_pixels, 0), RGB(clr, 0, 255 - clr) clr = clr + dclr Next Y ' Draw with a hollow font. BeginPath hdc CurrentX = 0 CurrentY = 0 Print TEXT1 EndPath hdc DrawWidth = DRAW_WIDTH ForeColor = vbBlack StrokePath hdc ' Outline the copyright area. RoundRect hdc, RR_X1, RR_Y1, RR_X2, RR_Y2, RR_WID, RR_HGT ' See where the second string will go. font2 = CustomFont(TEXT_HGT2, TEXT_WID2, 900, 0, _ FW_BOLD, False, False, False, _ FONT_NAME2) SelectObject hdc, font2 m_HotSpotXmin = 730 m_HotSpotYmax = 203 GetTextExtentPoint hdc, LINK_URL, Len(LINK_URL), sz m_HotSpotXmax = m_HotSpotXmin + sz.cy m_HotSpotYmin = m_HotSpotYmax - sz.cx ' Draw the second string. ForeColor = vbWhite CurrentX = m_HotSpotXmin CurrentY = m_HotSpotYmax Print LINK_URL ' Restore the original font. SelectObject hdc, origfont ' Free font resources (important!) DeleteObject font1 DeleteObject font2 ' Draw the copyright information. Font.Name = FONT_NAME3 Font.Size = TEXT_HGT3 Font.Bold = True txt = App.ProductName & " " & App.Major & "." & _ App.Minor & "." & App.Revision CurrentX = (RR_X1 + RR_X2 - TextWidth(txt)) / 2 CurrentY = RR_Y1 + 0.75 * Font.Size Print txt; txt = App.LegalCopyright CurrentX = (RR_X1 + RR_X2 - TextWidth(txt)) / 2 CurrentY = CurrentY + 1.5 * Font.Size Print txt; End Sub