Draw text to the background of a control by accessing the control's DrawingContext. : DrawingGroup « Windows Presentation Foundation « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Windows Presentation Foundation » DrawingGroup 
16.86.1.Draw text to the background of a control by accessing the control's DrawingContext.
<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Draw Text to a Control's Background"
  Background="FloralWhite"
  Width="800" 
  Loaded="WindowLoaded">

  <StackPanel>
      <Label Name="myLabel" Width="200" Height="36" />
      <TextBox Name="myTextBox" MaxLength="25" >Sample Text</TextBox>
      <Button Name="myButton" Click="OnButtonClick" Width="160" Height="40" />
      <Canvas Name="myCanvas" Width ="760" Height="300"/>
  </StackPanel>
</Window>

//File:Window.xaml.vb

Imports System
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Media

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      MyBase.New()
      InitializeComponent()
    End Sub

    Private Sub WindowLoaded(sender As Object, e As EventArgs)
      myLabel.Background = New DrawingBrush(DrawMyText("My Custom Label"))
      myButton.Background = New DrawingBrush(DrawMyText("Display Text"))
    End Sub
    Private Function DrawMyText(textString As StringAs Drawing
      Dim drawingGroup As New DrawingGroup()
      Using drawingContext As DrawingContext = drawingGroup.Open()
        Dim formattedText As New FormattedText(textString, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Comic Sans MS Bold")48, Brushes.Black)
        Dim textGeometry As Geometry = formattedText.BuildGeometry(New Point(200))
        drawingContext.DrawRoundedRectangle(Brushes.PapayaWhip, Nothing, New Rect(New Size(formattedText.Width + 50, formattedText.Height + 5))5.05.0)
        drawingContext.DrawGeometry(Brushes.Gold, New Pen(Brushes.Maroon, 1.5), textGeometry)
        Return drawingGroup
      End Using
    End Function
    Public Sub OnButtonClick(sender As Object, e As EventArgs)
      myCanvas.Background = New DrawingBrush(DrawMyText(myTextBox.Text))
    End Sub
  End Class
End Namespace
WPF Draw Text To The Background Of A Control By Accessing The Controls Drawing Context
16.86.DrawingGroup
16.86.1.Draw text to the background of a control by accessing the control's DrawingContext.Draw text to the background of a control by accessing the control's DrawingContext.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.