zoukankan      html  css  js  c++  java
  • HexEditor

    HexEditor.xaml
     1 <UserControl x:Class="MeterGui_BlendView.HexEditor"
     2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     5              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     6              mc:Ignorable="d"   x:Name="myHexEditer"
     7              FontFamily="Arial"  Width="510"  MaxHeight="1000" MinHeight="180"
     8              >
     9     <Grid>
    10         <Grid.RowDefinitions>
    11             <RowDefinition Height="auto"/>
    12             <RowDefinition Height="auto"/>
    13             <RowDefinition Height="auto"/>
    14             <RowDefinition/>
    15             <RowDefinition Height="auto"/>
    16         </Grid.RowDefinitions>
    17         <StackPanel Grid.Row="2" HorizontalAlignment="Stretch" Background="#FFECE9D9"
    18                     Orientation="Horizontal" VerticalAlignment="Top">
    19             <TextBox
    20                     MinLines="1" Padding="0" BorderThickness="1" Width="50" BorderBrush="{x:Null}"
    21                     FontFamily="Consolas" TextWrapping="Wrap" Background="{x:Null}" IsReadOnly="True">
    22             </TextBox>
    23             <TextBox
    24                     Padding="0" BorderThickness="0" Width="315" BorderBrush="Gray"
    25                     FontFamily="Consolas" TextWrapping="Wrap" Foreground="Gray" Background="{x:Null}" IsReadOnly="True" VerticalAlignment="Bottom">
    26                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
    27             </TextBox>
    28             <TextBox
    29                     Padding="0" BorderThickness="0" Width="110" BorderBrush="LightGray"
    30                     FontFamily="Consolas" TextWrapping="Wrap" Foreground="White" Background="{x:Null}" IsReadOnly="True">
    31 
    32             </TextBox>
    33         </StackPanel>
    34         <ScrollViewer Grid.Row="3" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Visible" MaxHeight="200">
    35             <Grid x:Name="GridM">
    36                 <Grid.ColumnDefinitions>
    37                     <ColumnDefinition Width="auto"/>
    38                     <ColumnDefinition Width="auto"/>
    39                     <ColumnDefinition/>
    40                 </Grid.ColumnDefinitions>
    41                 <TextBox x:Name="SeriseN"
    42                     MinLines="10" Padding="0" BorderThickness="0,0,1,0" Width="50"
    43                     FontFamily="Consolas" TextWrapping="Wrap" Foreground="Black" Background="White" IsReadOnly="True" BorderBrush="Gray">
    44                     0x0001 0x0002 0x0003 0x0004 0xefef 0xabcd 0x0001 0x0002 0x0003
    45                 </TextBox>
    46                 <TextBox x:Name="PacketII" Grid.Column="2" 
    47                     MinLines="10" Padding="0" BorderThickness="0" Width="110"
    48                     FontFamily="Consolas" TextWrapping="Wrap" Foreground="Green" Background="White"
    49                          Text=".1$2SEFTaQ4Q5.12SEFTaQ4Q5.1$2SEFTaQ4Q5.1$2SEFTaQ4Q5.1$2SEFTaQ4Q5.1$2SEFTaQ4Q5" IsReadOnly="True" CharacterCasing="Normal" BorderBrush="Black">
    50                     <!--.1$2SEFTaQ4Q5.12SEFTaQ4Q5.1$2SEFTaQ4Q5.1$2SEFTaQ4Q5.1$2SEFTaQ4Q5.1$2SEFTaQ4Q5-->
    51                     <TextBox.ContextMenu>
    52                         <ContextMenu Visibility="Collapsed">
    53                             <MenuItem Header="复制" Command="ApplicationCommands.Copy" x:Name="MenuItem01" />
    54                             <MenuItem Header="粘贴" Command="ApplicationCommands.Paste" x:Name="MenuItem11" />
    55                             <MenuItem Header="剪切" Command="ApplicationCommands.Cut" x:Name="MenuItem21" />
    56                         </ContextMenu>
    57                     </TextBox.ContextMenu>
    58                 </TextBox>
    59                 <TextBox x:Name="TB" Grid.Column="1"
    60                     TextChanged="TB_TextChanged" 
    61                     PreviewTextInput="TB_PreviewTextInput"
    62                     MinLines="10" Padding="0" BorderThickness="0" Width="315"
    63                     FontFamily="Consolas" TextWrapping="Wrap" AllowDrop="False">
    64                     aa bb cc dd ee ff 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff
    65                 </TextBox>
    66             </Grid>
    67         </ScrollViewer>
    68     </Grid>
    69 </UserControl>
    HexEditor.xaml.cs
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Windows;
      6 using System.Windows.Controls;
      7 using System.Windows.Data;
      8 using System.Windows.Documents;
      9 using System.Windows.Input;
     10 using System.Windows.Media;
     11 using System.Windows.Media.Imaging;
     12 using System.Windows.Navigation;
     13 using System.Windows.Shapes;
     14 
     15 namespace MeterGui_BlendView
     16 {
     17     /// <summary>
     18     /// HexEditor.xaml 的交互逻辑
     19     /// </summary>
     20     public partial class HexEditor : UserControl
     21     {
     22         public HexEditor()
     23         {
     24             InitializeComponent();
     25             Binding binding = new Binding();
     26             binding.Source = this;
     27             binding.Path = new PropertyPath("HexText");
     28             binding.Mode = BindingMode.TwoWay;
     29             binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
     30             TB.SetBinding(TextBox.TextProperty, binding);
     31         }
     32         public static readonly DependencyProperty HexTextProperty = DependencyProperty.Register(
     33         "HexText", typeof(string), typeof(HexEditor), new FrameworkPropertyMetadata(""));
     34 
     35         public string HexText
     36         {
     37             get { return (string)GetValue(HexTextProperty); }
     38             set { SetValue(HexTextProperty, value); }
     39         }
     40         public static readonly DependencyProperty AutoNumberProperty = DependencyProperty.Register(
     41         "AutoNumber", typeof(long), typeof(HexEditor), new FrameworkPropertyMetadata((long)2));
     42 
     43         public long AutoNumber
     44         {
     45             get { return (long)GetValue(AutoNumberProperty); }
     46             set { SetValue(AutoNumberProperty, value); }
     47         }
     48 
     49         private void TB_PreviewTextInput(object sender, TextCompositionEventArgs e)
     50         {
     51             int p = (sender as TextBox).SelectionStart;
     52             int pl = (sender as TextBox).SelectionLength;
     53             string s = (sender as TextBox).Text;
     54             if ("0123456789ABCDEF".Contains(e.Text.ToUpper()))
     55             {
     56                 //s=s.Remove(p+1,pl-1)
     57                 if (p == s.Length)
     58                 {
     59                     s = p == 0 ? s + e.Text + "0" : s + " " + e.Text + "0";
     60                     p = p == 0 ? p : p + 1;
     61                 }
     62                 else
     63                 {
     64                     //如果空格结尾会有索引越界情况
     65                     if (s.Substring((sender as TextBox).SelectionStart, 1) == " ")
     66                     {
     67                         p = p + 1;
     68                     }
     69                     s = s.Remove(p, 1);
     70                     s = s.Insert(p, e.Text);
     71                 }
     72             }
     73             (sender as TextBox).Text = s;
     74             (sender as TextBox).SelectionStart = p + 1;
     75             e.Handled = true;
     76         }
     77 
     78         private void TB_TextChanged(object sender, TextChangedEventArgs e)
     79         {
     80             int p = (sender as TextBox).SelectionStart;
     81             string s = (sender as TextBox).Text;
     82             FormatHex(ref s);
     83             (sender as TextBox).Text = s;
     84             (sender as TextBox).SelectionStart = p;
     85             HexText = TB.Text.Replace(" ", "");
     86             UpdateNo();
     87             UpdateAsiic();
     88         }
     89 
     90 
     91         private void FormatHex(ref string s)
     92         {
     93             s = s.Replace(" ", "");
     94             //s = ((s.Length % AutoNumber) == 0) ? s : (s + "0");
     95             //if (s.Length >= 128)
     96             //    s = s.Substring(0, 128);
     97             for (int i = ((int)AutoNumber - s.Length % (int)AutoNumber) % (int)AutoNumber; i > 0; i--)
     98             {
     99                 s = s + "0";
    100             }
    101             int l = s.Length;
    102             int n = (l - 2) / 2;
    103             for (int i = n; i > 0; i--)
    104             {
    105                 s = s.Insert(2 * i, " ");
    106             }
    107             s = s.ToUpper();
    108         }
    109 
    110         private void UpdateNo()//*
    111         {
    112             #region regresh No.
    113             int n = TB.LineCount;
    114             string s = "";
    115             string temp = "";
    116             for (int i = 0; i < TB.LineCount; i++)
    117             {
    118                 temp = "";
    119                 temp = (i * 16).ToString("x4");
    120                 switch (temp.Count<char>())
    121                 {
    122                     case 0:
    123                         temp = "0x0000";
    124                         break;
    125                     case 1:
    126                         temp = "0x000" + temp;
    127                         break;
    128                     case 2:
    129                         temp = "0x000" + temp;
    130                         break;
    131                     case 3:
    132                         temp = "0x0" + temp;
    133                         break;
    134                     default:
    135                         temp = "0x" + temp.Substring(0, 4);
    136                         break;
    137                 }
    138                 s += temp;
    139             }
    140             SeriseN.Text = s;
    141             #endregion
    142         }
    143         private void UpdateAsiic()
    144         {
    145             string s = TB.Text.Replace(" ", "");
    146             char[] tc = new char[s.Count<char>() / 2];
    147 
    148             for (int i = 0; i < (s.Count<char>() / 2); i++)
    149             {
    150                 byte b = byte.Parse(s.Substring(2 * i, 2), System.Globalization.NumberStyles.HexNumber);
    151                 if (
    152                     (
    153                     (b >= 0x30 & b <= 0x39)
    154                     | (b >= 0x41 & b <= 0x5a)
    155                     | (b >= 0x61 & b <= 0x7a)
    156                     )
    157                     //b > 0x20 & b < 0x7e
    158                     & b != 0x24//$
    159                     & b != 0x2a
    160                     & b != 0x2b//+
    161                     & b != 0x3b//;
    162                     & b != 0x40//@
    163                     & b != 0x5e//^
    164                     & b != 0x5b
    165                     & b != 0x7b
    166                     )
    167                     tc[i] = (char)b;
    168                 else
    169                     tc[i] = (char)0x2e;
    170 
    171             }
    172             string temp = new string(tc);
    173             PacketII.Text = temp;
    174         }
    175 
    176 
    177     }
    178 }
    <vi:HexEditor HexText="{Binding Padding, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

     

  • 相关阅读:
    utf8和utf8mb4的区别
    【JSP 标签】选择判断c:choose
    【JSP 标签】格式化日期
    【Spring 核心】AOP 面向切面编程
    【Spring 核心】高级装配
    【Spring 核心】装配bean(三)XML配置
    【Spring 核心】装配bean(二) JavaConfig装配
    【Spring 核心】装配Bean(一) 自动化装配
    【Js应用实例】图片预览
    【Js应用实例】限制上传图片大小
  • 原文地址:https://www.cnblogs.com/xiaoboz/p/2984179.html
Copyright © 2011-2022 走看看