Draw text to the background of a control by accessing the control's DrawingContext. : DrawingGroup « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » DrawingGroup 
24.99.2.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.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1() : base()
        {
            InitializeComponent();
        }

        private void WindowLoaded(object sender, EventArgs e
        {
            myLabel.Background = new DrawingBrush(DrawMyText("My Custom Label"));
            myButton.Background = new DrawingBrush(DrawMyText("Display Text"));
        }
        private Drawing DrawMyText(string textString)
        {
            DrawingGroup drawingGroup = new DrawingGroup();
            using (DrawingContext drawingContext = drawingGroup.Open())
            {
                FormattedText formattedText = new FormattedText(
                    textString,
                    CultureInfo.GetCultureInfo("en-us"),
                    FlowDirection.LeftToRight,
                    new Typeface("Comic Sans MS Bold"),
                    48,
                    Brushes.Black 
                    );
                Geometry textGeometry = formattedText.BuildGeometry(new Point(200));
                drawingContext.DrawRoundedRectangle(Brushes.PapayaWhip, null, 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;
            }
        }
        public void OnButtonClick(object sender, EventArgs e)
        {
            myCanvas.Background = new DrawingBrush(DrawMyText(myTextBox.Text));
        }
    }
}
WPF Draw Text To The Background Of A Control By Accessing The Controls Drawing Context
24.99.DrawingGroup
24.99.1.DrawingGroup and GeometryGroupDrawingGroup and GeometryGroup
24.99.2.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.