📄 smsinputform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using SMS;
namespace EncrySMS
{
public struct SettingData
{
const string SETTING_FILE_NAME = "EncryptSMSetting";
public bool AutoClose;
public static bool IsSetted()
{
return File.Exists(SETTING_FILE_NAME);
}
public bool Save()
{
try
{
Stream stream = new FileStream(SETTING_FILE_NAME, FileMode.Create, FileAccess.Write, FileShare.None);
XmlSerializer sr = new XmlSerializer(typeof(SettingData));
sr.Serialize(stream, this);
stream.Close();
return true;
}
catch
{
SMSInputForm.NoticeMessage("保存配置信息失败");
return false;
}
}
public bool Load()
{
if (!IsSetted())
{
AutoClose = true;
return false;
}
try
{
Stream stream = new FileStream(SETTING_FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read);
XmlSerializer sr = new XmlSerializer(typeof(SettingData));
this = (SettingData)sr.Deserialize(stream);
stream.Close();
return true;
}
catch
{
return false;
}
}
};
public partial class SMSInputForm : Form
{
private SMSSender smsSender = new SMSSender();
SettingData setting;
public SMSInputForm()
{
InitializeComponent();
setting = new SettingData();
setting.Load();
// lblLength.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(149)))), ((int)(((byte)(213)))), ((int)(((byte)(254)))));
// context.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(149)))), ((int)(((byte)(213)))), ((int)(((byte)(254)))));
// lblMobile.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(149)))), ((int)(((byte)(213)))), ((int)(((byte)(254)))));
//edtMobile
}
private void edtContent_TextChanged(object sender, EventArgs e)
{
lblLength.Text = edtContent.Text.Length.ToString() ;
}
private void SMSInputForm_Closing(object sender, CancelEventArgs e)
{
// setting.Save();
Cursor.Current = Cursors.WaitCursor;
}
private void SMSInputForm_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.Default;
}
private void copy_Click(object sender, EventArgs e)
{
if (edtContent.Text != null && edtContent.Text.Trim().Length != 0)
Clipboard.SetDataObject(edtContent.Text);
}
private void paste_Click(object sender, EventArgs e)
{
//取剪贴板对象
IDataObject iData = Clipboard.GetDataObject();
//判断是否是Text
if(iData.GetDataPresent(DataFormats.Text))
{
//取数据
edtContent.Text = (String)iData.GetData(DataFormats.Text);
}
}
private bool Validate()
{
if (edtMobile.Text.Trim()=="")
{
NoticeMessage("手机号码不能为空");
edtMobile.Focus();
return false;
}
if (edtContent.Text.Trim()=="")
{
NoticeMessage("短信内容不能为空");
edtContent.Focus();
return false;
}
return true;
}
private void btnGeneral_Click(object sender, EventArgs e)
{
if (!Validate())
return;
//textBox2.Text = DecryptText(textBox1.Text);
//return;
if (!smsSender.OpenDevice())
{
ErrorMessage("打开短信发送设备失败,终止短信群发。");
return;
}
Cursor.Current = Cursors.WaitCursor;
if (smsSender.SendMessage(edtMobile.Text, Encoding.Unicode.GetBytes(edtContent.Text)))
NoticeMessage("发送成功!");
else
ErrorMessage("发送失败!");
smsSender.CloseDevice();
Cursor.Current = Cursors.Default;
}
public static void ErrorMessage(String msg)
{
MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
public static void NoticeMessage(String msg)
{
MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
private void edtMobile_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)Keys.Back || e.KeyChar == '+')
return;
e.Handled = true;
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
if (!Validate())
return;
if (edtContent.Text.Length>66)
{
NoticeMessage("加密发送允许最大发送长度为66。");
edtContent.Focus();
return;
}
//textBox1.Text = EncryptToText(Encrypt(edtContent.Text));
//return;
if (!smsSender.OpenDevice())
{
ErrorMessage("打开短信发送设备失败,终止短信群发。");
return;
}
Cursor.Current = Cursors.WaitCursor;
if(smsSender.SendMessage(edtMobile.Text, Encrypt(edtContent.Text)))
NoticeMessage("发送成功!");
else
ErrorMessage("发送失败!");
smsSender.CloseDevice();
Cursor.Current = Cursors.Default;
}
private byte[] Encrypt(string content)
{
char[] tmp = content.ToCharArray();
byte[] encryptedbyte=new byte[2];
byte[] encrypedchars = new byte[tmp.Length*2+8];
for (int i = 0; i < tmp.Length; i++)
{
byte[] charbyte = BitConverter.GetBytes(tmp[i]);
if (charbyte[1] == 0)
{
encryptedbyte[1] = 0;
if (charbyte[0] >= 0x20 && charbyte[0] <= 0x7e)//visible char
{
encryptedbyte[0] = (byte)(charbyte[0] + (byte)0x5);
if (encryptedbyte[0] > 0x7e)
encryptedbyte[0] = (byte)(encryptedbyte[0] + (byte)(0xff - 0x7e + 0x20));
}
else
encryptedbyte[0] = charbyte[0];
}
else
{
encryptedbyte[0] = (byte)((charbyte[0])+0x7);
encryptedbyte[1] = (byte)((charbyte[1]) + 0x7);
}
//encrypedchars[i] = BitConverter.ToChar(encryptedbyte, 0);
encrypedchars[i * 2+8] = encryptedbyte[0];
encrypedchars[i * 2+8+1] = encryptedbyte[1];
}
if (encrypedchars[9]==0)
Buffer.BlockCopy( Encoding.Unicode.GetBytes("zzzz"),0,encrypedchars,0,8);
else
{
Buffer.BlockCopy(Encoding.Unicode.GetBytes("
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -