This is a fairly complex example. Most of the fun happens in the ShapeForm subroutine.
It uses the CreateFont API function to make a specially sized font and uses SelectObject to select the font.
Then the program use the GetTextExtentPoint API function to get the size of the text it will display. It uses the result for the text's width.
Because the height returned by GetTextExtentPoint includes space above and below the actual text, the program uses GetTextMetrics to see exactly how tall the printed part of the text is (ascent) and how far from the "top" of the text the actual drawing starts (internal leading space).
With these values, the program calculates exactly where it will put its two lines of text and the PictureBox in the middle.
Next ShapeForm makes Windows regions for the two text strings and the PictureBox. This code fragment shows how it makes a region for the first line of text. The BeginPath, EndPath, and PathToRegion API functions do the work here.
|
CombineRgn hRgn1, hRgn1, hRgn2, RGN_OR
CombineRgn hRgn1, hRgn1, hRgn3, RGN_OR
' Constrain the form to the combined region.
SetWindowRgn hWnd, hRgn1, False
|
' Shape the login window.
Private Sub ShapeForm()
Const TEXT1 = "RAD"
Const TEXT2 = "VB"
Const TEXT_HGT1 = 250
Const TEXT_WID1 = 100
Const TEXT_HGT2 = 250
Const TEXT_WID2 = 100
Const FONT_NAME1 = "Times New Roman"
Const FONT_NAME2 = "Times New Roman"
Const VGAP1 = -20
Const VGAP2 = -40
Const DRAW_WIDTH = 5
Dim font1 As Long
Dim font2 As Long
Dim origfont As Long
Dim hRgn1 As Long
Dim hRgn2 As Long
Dim hRgn3 As Long
Dim x1 As Long
Dim x2 As Long
Dim y1 As Long
Dim y2 As Long
Dim sz As Size
Dim tm As TEXTMETRIC
Dim wid As Single
Dim hgt As Single
Dim pix_wid As Single
Dim pix_hgt As Single
Dim text1_wid As Single
Dim text1_hgt As Single
Dim text1_int As Single
Dim text2_wid As Single
Dim text2_hgt As Single
Dim text2_int As Single
' Prepare the form.
AutoRedraw = True
ScaleMode = vbPixels
BorderStyle = vbBSNone
BackColor = vbBlue
ForeColor = vbBlack
Caption = ""
DrawWidth = DRAW_WIDTH
' ControlBox = False ' Set at design time.
' MinButton = False ' Set at design time.
' MaxButton = False ' Set at design time.
' ShowInTaskbar = False ' Set at design time.
' 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
text1_wid = sz.cx
GetTextMetrics hdc, tm
text1_int = tm.tmInternalLeading
text1_hgt = tm.tmAscent - text1_int
font2 = CustomFont(TEXT_HGT2, TEXT_WID2, 0, 0, _
FW_BOLD, False, False, False, _
FONT_NAME2)
SelectObject hdc, font1
GetTextExtentPoint hdc, TEXT2, Len(TEXT2), sz
text2_wid = sz.cx
GetTextMetrics hdc, tm
text2_int = tm.tmInternalLeading
text2_hgt = tm.tmAscent - text2_int
' Make the form big enough.
wid = picLogin.Height
If wid < text1_wid Then wid = text1_wid
If wid < text2_wid Then wid = text2_wid
hgt = picLogin.Height + text1_hgt + text2_hgt + VGAP1 + _
VGAP2
pix_wid = ScaleX(wid, vbPixels, vbTwips)
pix_hgt = ScaleY(hgt, vbPixels, vbTwips)
Move (Screen.Width - pix_wid) / 2, _
(Screen.Height - pix_hgt) / 2, _
pix_wid, pix_hgt
' Make the regions.
SelectObject hdc, font1
BeginPath hdc
CurrentX = (wid - text1_wid) / 2
CurrentY = -text1_int
Print TEXT1
EndPath hdc
hRgn1 = PathToRegion(hdc)
SelectObject hdc, font2
BeginPath hdc
CurrentX = (wid - text2_wid) / 2
CurrentY = text1_hgt + VGAP1 + VGAP2 + picLogin.Height _
- text2_int
Print TEXT2
EndPath hdc
hRgn2 = PathToRegion(hdc)
picLogin.Move (wid - picLogin.Width) / 2, text1_hgt + _
VGAP1
x1 = picLogin.Left
x2 = x1 + picLogin.Width
y1 = picLogin.Top
y2 = y1 + picLogin.Height
hRgn3 = CreateRectRgn(x1, y1, x2, y2)
' Combine the regions.
CombineRgn hRgn1, hRgn1, hRgn2, RGN_OR
CombineRgn hRgn1, hRgn1, hRgn3, RGN_OR
' Constrain the form to the combined region.
SetWindowRgn hWnd, hRgn1, False
' Draw with a hollow font.
SelectObject hdc, font1
BeginPath hdc
CurrentX = (wid - text1_wid) / 2
CurrentY = -text1_int
Print TEXT1
EndPath hdc
StrokePath hdc
SelectObject hdc, font2
BeginPath hdc
CurrentX = (wid - text2_wid) / 2
CurrentY = text1_hgt + VGAP1 + VGAP2 + picLogin.Height _
- text2_int
Print TEXT2
EndPath hdc
StrokePath hdc
' Restore the original font.
SelectObject hdc, origfont
' Free font resources (important!)
DeleteObject font1
DeleteObject font2
End Sub
|