zoukankan      html  css  js  c++  java
  • 保存TextBox中的文字为Path功能

    保存TextBox中的文字为Path功能

    今天再设计一个我自己程序的Icon时使用了Path+textbox做了图形,我不想导出为PNG,因为颜色比较单一,我又想通过代码控制颜色,所以我想完整的保存为Path。所以做了这个软件,支持设置不同的字体和字号,然后点击获取Path,就导出了path。然后粘贴到想用的地方即可。

    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Media;
    
    namespace TextTransformationPath
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent(); 
            }
    
            [Obsolete]
            public string CreateText(string text)
            {
                System.Windows.FontStyle fontStyle = FontStyles.Normal;
                FontWeight fontWeight = FontWeights.Medium; 
                var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip; 
                FormattedText formattedText = new FormattedText(
                    text,
                    CultureInfo.GetCultureInfo("en-us"),
                    FlowDirection.LeftToRight,
                    new Typeface(
                        new FontFamily(cbo_fontFamily.SelectedItem.ToString()),
                        fontStyle,
                        fontWeight,
                        FontStretches.Normal),
                    Convert.ToInt32(FontSizeTextBox.Text),
                    System.Windows.Media.Brushes.Black,  
                   pixelsPerDip
                    ); 
                // Build the geometry object that represents the text.
                var _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0));
    
                // Build the geometry object that represents the text highlight.
                var res = PathGeometry.CreateFromGeometry(_textGeometry).ToString();
                return res;
            }
    
            private void GetTextTransformPath_Click(object sender, RoutedEventArgs e)
            {
                ResultPathTextBox.Text= CreateText(SourceText.Text);
            }
        }
    }
    
    

    XAML代码

    <Window x:Class="TextTransformationPath.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TextTransformationPath"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <StackPanel>
                <TextBlock Margin="0,5" Text="输入待获取Path的内容:"/>
                <TextBox FontSize="{Binding ElementName=FontSizeTextBox,Path=Text}" Height="50" x:Name="SourceText" Text="杜文龙" FontFamily="{Binding ElementName=cbo_fontFamily,Path=SelectedItem}"/>
                <ComboBox x:Name="cbo_fontFamily"  SelectedIndex="0" ItemsSource="{x:Static Fonts.SystemFontFamilies}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" FontFamily="{Binding}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                    <ComboBox.ItemsPanel >
                        <ItemsPanelTemplate >
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </ComboBox.ItemsPanel>
                </ComboBox>
                <TextBlock Text="字号:"/>
                <TextBox x:Name="FontSizeTextBox" Text="30"/>
                <Button Margin="0,5" Content="点击获取Path" Click="GetTextTransformPath_Click"/>
                <TextBox x:Name="ResultPathTextBox"/>
            </StackPanel>
        </Grid>
    </Window>
    
    
  • 相关阅读:
    总结一些关于操作数据库是sql语句还是存储过程问题
    vs2010 创建预编译头 Debug 正常 Release Link Error问题解决
    创建Unicode格式的INI文件
    dos命令记录以及dos下通过进程id查找工作路径
    windows下多字节和宽字节转换
    关于多字节传输导致的乱码问题
    关于mysql数据库字符集优先级问题
    转: Apache开启gzip
    HTML 5 drag and drop 简介
    转: ES6异步编程: co函数库的含义与用法
  • 原文地址:https://www.cnblogs.com/duwenlong/p/14905721.html
Copyright © 2011-2022 走看看