Home > Articles > Programming > Visual Basic

Making a Splash

Like this article? We recommend

Like this article? We recommend

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

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020