| Option Explicit
'Necessary Windows API Declares:
Private Const SM_CYCAPTION = 4
Private Const SM_CXBORDER = 45
Private Const SM_CYBORDER = 46
Private Declare Function GetSystemMetrics Lib "user32" (ByVal _
nIndex As Long) As Long
Private Declare Function OffsetRgn Lib "gdi32" (ByVal hRgn As Long, _
ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function BeginPath Lib "gdi32" _
(ByVal hdc As Long) As Long
Private Declare Function EndPath Lib "gdi32" _
(ByVal hdc As Long) As Long
Private Declare Function PathToRegion Lib "gdi32" _
(ByVal hdc As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd _
As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
'Paths to a window region.
Private Sub Form_KeyPress(KeyAscii As Integer)
Static sEntered As String ' The text that's been entered
Dim hRgn As Long ' Created region
Dim xSides As Long ' Width of the window border
Dim yTop As Long ' Height of the border + caption
Dim yBottom As Long ' Height of the window border
xSides = GetSystemMetrics(SM_CXBORDER)
yTop = GetSystemMetrics(SM_CYCAPTION) + _
GetSystemMetrics(SM_CYBORDER)
yBottom = GetSystemMetrics(SM_CYBORDER)
'Setup the form for drawing, note that the font must
' be a truetype font.
Cls
Font = "Times New Roman"
FontSize = 30
FontBold = True
BackColor = vbCyan
'Act upon the keystroke
Select Case KeyAscii
Case 8 'Back space, Remove one character
'Make sure there's atleast one character
If Len(sEntered) >= 1 Then
sEntered = Mid(sEntered, 1, Len(sEntered) - 1)
End If
Case 13 'Enter, add a Cr Lf to the string
sEntered = sEntered & vbNewLine
Case 27 'Escape, exit the program
Unload Me
Exit Sub
Case Else 'Otherwise add the character to our string
sEntered = sEntered & Chr(KeyAscii)
End Select
'Make sure the form is big enough for the text to be displayed
Width = TextWidth(sEntered) + ((2 * xSides) * _
Screen.TwipsPerPixelX)
Height = TextHeight(sEntered) + ((yTop + yBottom) * _
Screen.TwipsPerPixelY)
BeginPath hdc 'Start saving the drawing into a path
Print sEntered 'Print the text (goes into the path)
EndPath hdc 'All done, save the path
hRgn = PathToRegion(hdc) 'Convert the path into a region
'Move the region so it shows in the client area of the form
OffsetRgn hRgn, xSides, yTop
'Finally, set the region to our window
SetWindowRgn hwnd, hRgn, True
'Since PathToRegion will return null if print didn't draw
'anything, and SetWindowRgn removes the region when you pass
'it null, we don't have to worry about hiding our window with
'a blank region. Also, since the path will be deleted next
'time a path is created, and the region deleted next time
'SetWindowRgn is called, we don't have to clean up anything.
End Sub
|