Click here to Skip to main content
15,897,334 members
Articles / Desktop Programming / WPF

WPF Draggable Label

Rate me:
Please Sign up or sign in to vote.
4.63/5 (16 votes)
13 Apr 2010CPOL3 min read 62.9K   6.3K   43  
WPF label control that can be dragged and resized at runtime
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Printing;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
using System.Xml.Linq;

namespace DraggableLabelTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void layoutButton_Click(object sender, RoutedEventArgs e)
        {
            var win = new LayoutWindow();
            win.ShowDialog();
        }

        private void printButton_Click(object sender, RoutedEventArgs e)
        {
            var nameLocation = new Point(12, 12);
            var nameWidth = double.NaN;
            var nameHeight = double.NaN;
            var addressLocation = new Point(12, 48);
            var addressWidth = double.NaN;
            var addressHeight = double.NaN;
            var emailLocation = new Point(12, 120);
            var emailWidth = double.NaN;
            var emailHeight = double.NaN;
            var countryLocation = new Point(12, 156);
            var countryWidth = double.NaN;
            var countryHeight = double.NaN;
            var ageLocation = new Point(12, 192);
            var ageWidth = double.NaN;
            var ageHeight = double.NaN;
            var genderLocation = new Point(12, 228);
            var genderWidth = double.NaN;
            var genderHeight = double.NaN;
            var dateLocation = new Point(12, 264);
            var dateWidth = double.NaN;
            var dateHeight = double.NaN;

            if (File.Exists("layout.xml"))
            {
                XElement layout = XElement.Load("layout.xml");

                var elements = layout.Elements().Select(
                    el => new { Name = el.Name, Top = (double) el.Attribute("Top"), Left = (double) el.Attribute("Left"), Width = (double) el.Attribute("Width"), Height = (double) el.Attribute("Height") });

                var element = elements.Where(el => el.Name == "NameLabel").Single();
                nameLocation = new Point(element.Left, element.Top);
                nameWidth = element.Width;
                nameHeight = element.Height;

                element = elements.Where(el => el.Name == "AddressLabel").Single();
                addressLocation = new Point(element.Left, element.Top);
                addressWidth = element.Width;
                addressHeight = element.Height;

                element = elements.Where(el => el.Name == "EmailLabel").Single();
                emailLocation = new Point(element.Left, element.Top);
                emailWidth = element.Width;
                emailHeight = element.Height;

                element = elements.Where(el => el.Name == "CountryLabel").Single();
                countryLocation = new Point(element.Left, element.Top);
                countryWidth = element.Width;
                countryHeight = element.Height;

                element = elements.Where(el => el.Name == "AgeLabel").Single();
                ageLocation = new Point(element.Left, element.Top);
                ageWidth = element.Width;
                ageHeight = element.Height;

                element = elements.Where(el => el.Name == "GenderLabel").Single();
                genderLocation = new Point(element.Left, element.Top);
                genderWidth = element.Width;
                genderHeight = element.Height;

                element = elements.Where(el => el.Name == "DateLabel").Single();
                dateLocation = new Point(element.Left, element.Top);
                dateWidth = element.Width;
                dateHeight = element.Height;
            }

            using (var xpsStream = new MemoryStream())
            {
                using (Package package = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite))
                {
                    string packageUriString = "memorystream://data.xps";
                    var packageUri = new Uri(packageUriString);

                    PackageStore.AddPackage(packageUri, package);
                    var xpsDocument = new XpsDocument(package, CompressionOption.Maximum, packageUriString);

                    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
                    var visual = new DrawingVisual();

                    using (DrawingContext ctx = visual.RenderOpen())
                    {
                        var text = new FormattedText(nameTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = nameWidth;
                        text.MaxTextHeight = nameHeight;
                        ctx.DrawText(text, nameLocation);

                        text = new FormattedText(addressTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = addressWidth;
                        text.MaxTextHeight = addressHeight;
                        ctx.DrawText(text, addressLocation);

                        text = new FormattedText(emailTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = emailWidth;
                        text.MaxTextHeight = emailHeight;
                        ctx.DrawText(text, emailLocation);

                        text = new FormattedText(countryTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = countryWidth;
                        text.MaxTextHeight = countryHeight;
                        ctx.DrawText(text, countryLocation);

                        text = new FormattedText(ageTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = ageWidth;
                        text.MaxTextHeight = ageHeight;
                        ctx.DrawText(text, ageLocation);

                        text = new FormattedText(genderComboBox.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = genderWidth;
                        text.MaxTextHeight = genderHeight;
                        ctx.DrawText(text, genderLocation);

                        text = new FormattedText(dateDatePicker.Text, System.Globalization.CultureInfo.CurrentCulture,
                                                     FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
                        text.MaxTextWidth = dateWidth;
                        text.MaxTextHeight = dateHeight;
                        ctx.DrawText(text, dateLocation);
                    }

                    var printTicket = new PrintTicket();
                    printTicket.PageMediaSize = new PageMediaSize(816, 1248);

                    writer.Write(visual, printTicket);

                    FixedDocumentSequence document = xpsDocument.GetFixedDocumentSequence();
                    xpsDocument.Close();

                    var printPreview = new PrintPreview();
                    printPreview.Owner = this;
                    printPreview.Document = document;
                    printPreview.ShowDialog();

                    PackageStore.RemovePackage(packageUri);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            nameTextBox.Text = "John Smith";
            addressTextBox.Text = "2000 West Park Drive\nWestborough\nMA 01581";
            emailTextBox.Text = "johnsmith@gmail.com";
            countryTextBox.Text = "United States";
            ageTextBox.Text = "45";
            genderComboBox.SelectedIndex = 0;
            dateDatePicker.Text = DateTime.Now.Date.ToShortDateString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions