⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 measurestring.vb

📁 Programming the .NET Compact Framework with vb 源代码
💻 VB
字号:
' MeasureString.vb - Shows operation of MeasureString function
'
' Code from _Programming the .NET Compact Framework with C#_
' and _Programming the .NET Compact Framework with VB_
' (c) Copyright 2002-2003 Paul Yao and David Durant. 
' All rights reserved.

Public Class FormMain
    Inherits System.Windows.Forms.Form
      Friend WithEvents cboxFont As System.Windows.Forms.ComboBox
      Friend WithEvents cboxSize As System.Windows.Forms.ComboBox
      Friend WithEvents cboxStyle As System.Windows.Forms.ComboBox
      Friend WithEvents textSample As System.Windows.Forms.TextBox
      Friend WithEvents labelHeight As System.Windows.Forms.Label
      Friend WithEvents labelWidth As System.Windows.Forms.Label
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.cboxFont = New System.Windows.Forms.ComboBox
Me.cboxSize = New System.Windows.Forms.ComboBox
Me.cboxStyle = New System.Windows.Forms.ComboBox
Me.textSample = New System.Windows.Forms.TextBox
Me.labelHeight = New System.Windows.Forms.Label
Me.labelWidth = New System.Windows.Forms.Label
'
'cboxFont
'
Me.cboxFont.Size = New System.Drawing.Size(120, 22)
'
'cboxSize
'
Me.cboxSize.Items.Add("8")
Me.cboxSize.Items.Add("10")
Me.cboxSize.Items.Add("12")
Me.cboxSize.Items.Add("14")
Me.cboxSize.Items.Add("16")
Me.cboxSize.Items.Add("18")
Me.cboxSize.Items.Add("20")
Me.cboxSize.Items.Add("22")
Me.cboxSize.Items.Add("24")
Me.cboxSize.Items.Add("28")
Me.cboxSize.Items.Add("32")
Me.cboxSize.Items.Add("36")
Me.cboxSize.Items.Add("40")
Me.cboxSize.Items.Add("48")
Me.cboxSize.Items.Add("72")
Me.cboxSize.Location = New System.Drawing.Point(128, 0)
Me.cboxSize.Size = New System.Drawing.Size(40, 22)
'
'cboxStyle
'
Me.cboxStyle.Items.Add("Regular")
Me.cboxStyle.Items.Add("Bold")
Me.cboxStyle.Items.Add("Italic")
Me.cboxStyle.Items.Add("Strikeout")
Me.cboxStyle.Items.Add("Underline")
Me.cboxStyle.Location = New System.Drawing.Point(176, 0)
Me.cboxStyle.Size = New System.Drawing.Size(64, 22)
'
'textSample
'
Me.textSample.Location = New System.Drawing.Point(16, 32)
Me.textSample.Size = New System.Drawing.Size(208, 22)
Me.textSample.Text = "Sample"
'
'labelHeight
'
Me.labelHeight.Location = New System.Drawing.Point(8, 64)
Me.labelHeight.Size = New System.Drawing.Size(104, 20)
Me.labelHeight.Text = "Height = "
'
'labelWidth
'
Me.labelWidth.Location = New System.Drawing.Point(120, 64)
Me.labelWidth.Size = New System.Drawing.Size(112, 20)
Me.labelWidth.Text = "Width = "
'
'FormMain
'
Me.Controls.Add(Me.labelWidth)
Me.Controls.Add(Me.labelHeight)
Me.Controls.Add(Me.textSample)
Me.Controls.Add(Me.cboxStyle)
Me.Controls.Add(Me.cboxSize)
Me.Controls.Add(Me.cboxFont)
Me.Menu = Me.MainMenu1
Me.MinimizeBox = False
Me.Text = "Measure String"

    End Sub

#End Region

   ' My private data.
   Private m_fonts As YaoDurant.Drawing.FontCollection

   Private Sub FormMain_Load( _
   ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load
      Try
         ' Create managed code font collection.
         m_fonts = New YaoDurant.Drawing.FontCollection

         ' Loop through collection, adding all items to combo box
         Dim i As Integer
         For i = 0 To m_fonts.Count - 1 Step i + 1
            cboxFont.Items.Add(m_fonts(i))
         Next
      Catch
         Dim str As String
         str = "Error: cannot create FontCollection" + _
            " -- make sure fontlist.dll is installed"
         MessageBox.Show(str, "FontPicker")
         Close()
         Return
      End Try

      cboxFont.SelectedIndex = 0
      cboxSize.SelectedIndex = 0
      cboxStyle.SelectedIndex = 0
   End Sub

   '--------------------------------------------------------
   Private Sub SetFont()
      Dim iFont As Integer = cboxFont.SelectedIndex
      Dim iSize As Integer = cboxSize.SelectedIndex
      Dim iStyle As Integer = cboxStyle.SelectedIndex
      If iFont <> -1 And iSize <> -1 And iStyle <> -1 Then
         Dim strFont As String = cboxFont.Items(iFont).ToString()
         Dim strSize As String = cboxSize.Items(iSize).ToString()
         iSize = Integer.Parse(strSize)

         Dim fs As FontStyle
         If iStyle = 0 Then
            fs = FontStyle.Regular
         ElseIf iStyle = 1 Then
            fs = FontStyle.Bold
         ElseIf iStyle = 2 Then
            fs = FontStyle.Italic
         ElseIf iStyle = 3 Then
            fs = FontStyle.Strikeout
         Else
            fs = FontStyle.Underline
         End If

         ' Create a new font.
         Dim fontNew As Font = New Font(strFont, CSng(iSize), fs)

         ' Connect font to textbox
         Font = fontNew
         Invalidate()
      End If
   End Sub

   Private Sub cboxFont_SelectedIndexChanged( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles cboxFont.SelectedIndexChanged
      SetFont()
   End Sub

   Private Sub cboxSize_SelectedIndexChanged( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles cboxSize.SelectedIndexChanged
      SetFont()
   End Sub

   Private Sub cboxStyle_SelectedIndexChanged( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles cboxStyle.SelectedIndexChanged
      SetFont()
   End Sub

   Private Sub FormMain_Paint( _
   ByVal sender As Object, _
   ByVal e As System.Windows.Forms.PaintEventArgs) _
   Handles MyBase.Paint
      Dim str As String = textSample.Text
      Dim brFore As Brush = New SolidBrush(SystemColors.Window)
      Dim brBack As Brush = New SolidBrush(SystemColors.WindowText)
      Dim sinX As Single = 240 / 4
      Dim sinY As Single = 320 / 3

      Dim szf As SizeF = e.Graphics.MeasureString(str, Font)

      ' Draw rectangle in text background.
      Dim xRect As Integer = CType(sinX, Integer)
      Dim yRect As Integer = CType(sinY, Integer)
      Dim cxRect As Integer = CType(szf.Width, Integer)
      Dim cyRect As Integer = CType(szf.Height, Integer)
      Dim rc As Rectangle
      rc = New Rectangle(xRect, yRect, cxRect, cyRect)

      e.Graphics.FillRectangle(brBack, rc)

      ' Draw string.
      e.Graphics.DrawString(str, Font, brFore, sinX, sinY)

      ' Draw lines to height label.
      '
      '  --- (A)
      '   |
      '   |  (B)
      '   |
      '  --- (C)
      Dim penBlack As Pen = New Pen(Color.Black)
      Dim x1 As Integer
      Dim y1 As Integer
      Dim x2 As Integer
      Dim y2 As Integer
      x1 = labelHeight.Left + labelHeight.Width / 4
      y1 = labelHeight.Top + labelHeight.Height
      x2 = xRect - 5
      y2 = yRect + cyRect / 2
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Line (A)
      x1 = xRect - 3
      y1 = yRect
      x2 = xRect - 7
      y2 = yRect
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Line (B)
      x1 = xRect - 5
      y1 = yRect
      x2 = xRect - 5
      y2 = yRect + cyRect - 1
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Line (C)
      x1 = xRect - 3
      y1 = yRect + cyRect - 1
      x2 = xRect - 7
      y2 = yRect + cyRect - 1
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Draw lines to width label.
      '
      '  |            |
      '  |------------|
      '  |            |
      ' (D)   (E)    (F)
      x1 = labelWidth.Left + labelWidth.Width / 4
      y1 = labelWidth.Top + labelWidth.Height
      x2 = xRect + cxRect / 2
      y2 = yRect - 5
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Line (D)
      x1 = xRect
      y1 = yRect - 3
      x2 = xRect
      y2 = yRect - 7
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Line (E)
      x1 = xRect
      y1 = yRect - 5
      x2 = xRect + cxRect - 1
      y2 = yRect - 5
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Line (F)
      x1 = xRect + cxRect - 1
      y1 = yRect - 3
      x2 = xRect + cxRect - 1
      y2 = yRect - 7
      e.Graphics.DrawLine(penBlack, x1, y1, x2, y2)

      ' Update controls with string size.
      labelHeight.Text = "Height = " + szf.Height.ToString()
      labelWidth.Text = "Width = " + szf.Width.ToString()
   End Sub

   Private Sub textSample_KeyUp( _
   ByVal sender As Object, _
   ByVal e As System.Windows.Forms.KeyEventArgs) _
   Handles textSample.KeyUp
      Invalidate()
   End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -