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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » DrawingGroupScreenshots 
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.
  


<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));
        }
    }
}

   
    
  
Related examples in the same category
1.DrawingGroup and GeometryGroupDrawingGroup and GeometryGroup
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.