Text and Fonts

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 11.  Introduction to GDI+


In a GUI environment such as Windows, displaying text is just another kind of graphical operation. In place of using pens and brushes as our drawing tool, we use fonts . Fonts are quite complicated and beyond the scope of this chapter to discuss in any detail. In fact, drawing with text using GDI+ is quite complicated, and normally you won't need to do that unless you are writing a rather specialized application such as a word processor. Normally, you will work with text data using the rich array of Windows controls.

We illustrate the fundamentals of working with text and fonts with the TextDemo program, which also demonstrates handling a KeyPress event for entering a character. The program allows you to type text, which is displayed in the main window as you type. A toolbar button brings up the font common dialog, which allows the user to change the font that is displayed. Figure 11-11 illustrates the string "hello text" typed in and displayed with a Times New Roman 24-point font. Note that our program also changes the font of the toolbar button!

Figure 11-11. Typing in text and displaying with a desired font.

graphics/11fig11.jpg

 Imports System.Text Public Class Form1    Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " ...    Private m_text As New StringBuilder()    Protected Overrides Sub OnPaint(_     ByVal e As System.Windows.Forms.PaintEventArgs)       Dim str As String = m_text.ToString       Dim g As Graphics = e.Graphics       g.DrawString(str, Font, Brushes.Black, 0, 0)       MyBase.OnPaint(e)    End Sub    Private Sub Form1_KeyPress(ByVal sender As Object, _     ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress       m_text.Append(e.KeyChar)       Invalidate()    End Sub    Private Sub ToolBar1_ButtonClick(_     ByVal sender As System.Object, ByVal e As _     System.Windows.Forms.ToolBarButtonClickEventArgs) _     Handles ToolBar1.ButtonClick       Dim dlg As New FontDialog()       dlg.Font = Font       Dim status As DialogResult = dlg.ShowDialog       If status = DialogResult.OK Then          Font = dlg.Font       End If       Invalidate()    End Sub End Class 

Measuring Text

Another issue that arises in drawing text is the necessity to obtain suitable text metrics so that you can position text appropriately on the output surface. As an example, consider the problem of displaying multiple lines of text in a window. You can position the first line at the top-left of the window at coordinates (0,0). But where should you position the second line? You need to compute a suitable offset of the y-axis, which can be based on the height of the font used in drawing.

The program ScrollDemo\Version 1 provides an illustration.

 Public Class Form1    Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " ...    Private m_strings() As String = {"This is", _       "an example", "of text", "distributed", _       "over several", "lines"}  Private m_deltaY As Integer  Private Sub Form1_Load(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles MyBase.Load  m_deltaY = Font.Height  Me.BackColor = Color.White    End Sub    Private Sub Form1_Paint(ByVal sender As Object, _     ByVal e As System.Windows.Forms.PaintEventArgs) _     Handles MyBase.Paint       Dim g As Graphics = e.Graphics       Dim i As Integer  Dim y As Integer = 0  For i = 0 To m_strings.Length - 1          g.DrawString(m_strings(i), Font, _          Brushes.Black, 0,  y  )  y   += m_deltaY  Next    End Sub End Class 

Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net