1 Star 0 Fork 1

mycls/GB645

forked from cherishforgo/GB645 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
txt hex ascii .cs 9.80 KB
一键复制 编辑 原始数据 按行查看 历史
Admin 提交于 2022-03-12 14:44 . Signed-off-by: Admin Admin@DESKTOP-EP3TRFC
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace LeafSoft.Units
{
/// <summary>
/// Hex/ASCII输入文本框
/// 作者:一叶知秋
/// 日期:2013年7月11日
/// 可输入Hex,ASCII
/// 可切换显示Hex与ASCII的输入文本框
/// Hex输入时可自动每2个字符之间添加空格
/// </summary>
public class BytesBox:TextBox
{
ContextMenuStrip CMenu = new ContextMenuStrip();
ToolStripMenuItem CM_Type = new ToolStripMenuItem();
ToolStripMenuItem CM_Clear = new ToolStripMenuItem();
public BytesBox()
{
#region 快捷菜单
CM_Type.Name = "CM_Type";
CM_Type.Size = new System.Drawing.Size(127, 22);
CM_Type.Text = "ASCII";
CM_Type.Click += new System.EventHandler(CM_Type_Click);
CM_Clear.Name = "CM_Clear";
CM_Clear.Size = new System.Drawing.Size(127, 22);
CM_Clear.Text = "清空";
CM_Clear.Click += new System.EventHandler(CM_Clear_Click);
CMenu.Name = "CMenu";
CMenu.ShowImageMargin = false;
CMenu.Size = new System.Drawing.Size(128, 48);
CMenu.Items.AddRange(new ToolStripItem[] {
CM_Type,CM_Clear});
this.ContextMenuStrip = CMenu;
#endregion
}
#region 属性
bool _IsHex = true;
[Description("True:Hex;False:ASCII"), Category("输入格式设置")]
public bool IsHex
{
set {
_IsHex = value;
if (_IsHex)
{
CM_Type.Text = "ASCII";
}
else
{
CM_Type.Text = "Hex";
}
}
get {
return _IsHex;
}
}
#endregion
#region 菜单操作
/// <summary>
/// HEX/ASCII 格式切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CM_Type_Click(object sender, EventArgs e)
{
if (IsHex)
{//切换到ASCII格式
IsHex = false;
if (this.Text.Length > 0)
{
string[] HexStr = this.Text.Trim().Split(' ');
byte[] data = new byte[HexStr.Length];
for (int i = 0; i < HexStr.Length; i++)
{
data[i] = (byte)(Convert.ToInt32(HexStr[i], 16));
}
this.Text = new ASCIIEncoding().GetString(data);
}
}
else
{//切换到Hex格式
IsHex = true;
if (this.Text.Length > 0)
{
byte[] data = new ASCIIEncoding().GetBytes(this.Text.Trim());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.AppendFormat("{0:x2}", data[i]);
}
this.Text = sb.ToString();
}
}
}
/// <summary>
/// 清空数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CM_Clear_Click(object sender, EventArgs e)
{
this.Text = "";
}
#endregion
#region 输入控制
protected override void OnTextChanged(EventArgs e)
{
if (_IsHex)
{//Hex输入
string Content = this.Text.Replace(" ", "");//获取去除空格后的字符内容
int SpaceCount = Content.Length / 2;
int StartIndex = 2;
for (int i = 0; i < SpaceCount; i++)
{
Content = Content.Insert(StartIndex, " ");
StartIndex = StartIndex + 3;
}
this.Text = Content.TrimEnd().ToUpper();
}
this.SelectionStart = this.Text.Length;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (_IsHex)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9')//数字0-9键
|| (e.KeyChar >= 'A' && e.KeyChar <= 'F')//字母A-F
|| (e.KeyChar >= 'a' && e.KeyChar <= 'f')//字母a-f
|| e.KeyChar == 0x08//退格键
|| e.KeyChar == 0x03//拷贝
|| e.KeyChar == 0x18)//剪切
{
e.Handled = false;
return;
}
}
else
{
if ((e.KeyChar >= 0x20 && e.KeyChar <= 0x7E)
|| e.KeyChar == 0x08//退格键
|| e.KeyChar == 0x0D//回车键
|| e.KeyChar == 0x03//拷贝
|| e.KeyChar == 0x18)//剪切
{
e.Handled = false;
return;
}
}
if (e.KeyChar == 0x16)//粘贴
{//粘贴前数据格式检查
if (CheckPaste())
{
e.Handled = false;
return;
}
}
e.Handled = true;
}
/// <summary>
/// 粘贴数据格式检查
/// </summary>
/// <returns></returns>
private bool CheckPaste()
{
try
{
char[] PasteChar = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString().ToCharArray();
if (_IsHex)
{
foreach (char data in PasteChar)
{
if (!((data >= '0' && data <= '9')//数字0-9键
|| (data >= 'A' && data <= 'F')//字母A-F
|| (data >= 'a' && data <= 'f')//字母a-f
|| data == 0x20))//空格
{
MessageBox.Show("粘贴数据含有非法字符,只能包含数字0-9,大写英文字母A-F,小写英文字母a-f以及空格!", "非法的粘贴", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
else
{
foreach (char data in PasteChar)
{
if (!((data >= 0x20 && data <= 0x7E)
|| data == 0x0A
|| data == 0x0D))//回车键
{
MessageBox.Show("粘贴数据含有非法字符,只能包含ASCII码字符!", "非法的粘贴", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
#endregion
#region 公共方法
/// <summary>
/// 获取命令对象
/// </summary>
/// <returns></returns>
public Model.Command GetCMD()
{
try
{
if (this.Text.Trim() == string.Empty)
{
MessageBox.Show("不允许内容为空!");
return null;
}
Model.Command Cmd = new Model.Command();
Cmd.IsHex = _IsHex;
if (Cmd.IsHex)
{//Hex
string[] HexStr = this.Text.Trim().Split(' ');
Cmd.DataBytes = new byte[HexStr.Length];
for (int i = 0; i < HexStr.Length; i++)
{
Cmd.DataBytes[i] = (byte)(Convert.ToInt32(HexStr[i], 16));
}
}
else
{ //ASCII
Cmd.DataBytes = new ASCIIEncoding().GetBytes(this.Text.Trim());
}
return Cmd;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
/// <summary>
/// 设置命令对象
/// </summary>
/// <param name="Cmd"></param>
public void SetCMD(Model.Command Cmd)
{
try
{
this.IsHex = Cmd.IsHex;
if (this.IsHex)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Cmd.DataBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", Cmd.DataBytes[i]);
}
this.Text = sb.ToString();
}
else
{
this.Text = new ASCIIEncoding().GetString(Cmd.DataBytes);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
/// <summary>
/// 命令对象
/// </summary>
public class Command
{
bool _IsHex = true;
byte[] _DataBytes = null;
/// <summary>
/// 是否16进制数据
/// </summary>
public bool IsHex
{
set {
_IsHex = value;
}
get {
return _IsHex;
}
}
/// <summary>
/// byte数据
/// </summary>
public byte[] DataBytes
{
set
{
_DataBytes = value;
}
get
{
return _DataBytes;
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/vmycls/gb645.git
git@gitee.com:vmycls/gb645.git
vmycls
gb645
GB645
master

搜索帮助