生成MAC地址的小工具:
{*------------------------------------------------
生成mac地址
@author
@version 2015.7.2
2015.10.22修改
步长最大长度为256,必须为2的n次方
长度不超过5位数
-------------------------------------------------}
unit frmSaveMac; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, math; type
TSaveMacForm = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
SaveDialog1: TSaveDialog;
SaveButton: TButton;
ExitButton: TButton;
Edit7: TEdit;
Label2: TLabel;
Label3: TLabel;
Edit8: TEdit;
Memo1: TMemo;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
btn_clear: TButton;
btn_show: TButton;
Memo2: TMemo;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure ExitButtonClick(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Edit7KeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure btn_clearClick(Sender: TObject);
procedure btn_showClick(Sender: TObject);
private
{ Private declarations }
procedure CheckIsEmpty;
function CheckStepOrLen: Boolean;
procedure ShowData();
Procedure SaveData();
public
{ Public declarations }
end; var
SaveMacForm: TSaveMacForm; implementation {$R *.dfm}
{*------------------------------------------------
文本框只能输入十六进制数、删除键和回车键
@param
@param
-------------------------------------------------}
procedure TSaveMacForm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9', 'A'..'F', 'a'..'f', #8, #13]) then
begin
Key := #0;
Application.MessageBox ('只能输入十六进制的数字!', '提示消息', MB_OK + MB_ICONERROR);
end;
end; procedure TSaveMacForm.ExitButtonClick(Sender: TObject);
begin
Close;
end; {*------------------------------------------------
文本框按键事件
@param
@param
-------------------------------------------------}
procedure TSaveMacForm.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i,j : integer;
begin
/// 输入最大长度
for i := 0 to self.ComponentCount -1 do begin // 循环每个Edit控件 设置最大长度为2 2015.10.22修改
if self.Components[i] is TEdit then begin
(self.Components[i] as TEdit).MaxLength := 2;
end;
if Sender = Edit7 then Edit7.MaxLength := 3;
if Sender = Edit8 then Edit8.MaxLength := 5; // 长度不超过5位数 2015.10.22修改
end; /// 将光标移到最后
for j := 0 to ComponentCount - 1 do begin
if Components[j] is TEdit then begin
(Components[j] as TEdit).SelStart := Length((Components[j] as TEdit).Text);
end;
end; /// 按回车触发
if key = 13 then
begin
if (sender = Edit1) and (length(Edit1.Text) >= 2) then
Edit2.SetFocus;
if (Sender = Edit2) and (length(Edit2.Text) >= 2) then
Edit3.SetFocus;
if (Sender = Edit3) and (length(Edit3.Text) >= 2) then
Edit4.SetFocus;
if (Sender = Edit4) and (length(Edit4.Text) >= 2) then
Edit5.SetFocus;
if (Sender = Edit5) and (length(Edit5.Text) >= 2) then
Edit6.SetFocus;
if (Sender = Edit6) then
Edit7.SetFocus ;
if (Sender = Edit7) then
Edit8.SetFocus;
end;
ExitButton.Enabled := True;
SaveButton.Enabled := True;
end; {*------------------------------------------------
步长只能输入0到9的数字
@param
@param
-------------------------------------------------}
procedure TSaveMacForm.Edit7KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9', #8]) then
Key := #0;
end; procedure TSaveMacForm.FormCreate(Sender: TObject);
var
j: integer;
C: Double;
begin
SaveButton.Enabled := False;
ExitButton.Enabled := False;
RadioButton1.Checked := True; /// 默认输出格式 memo2.Lines.Add('256以内的2的n次方:');
for J := 1 to 8 do
begin
C := power(2, J); /// 2的I次方
memo2.Lines.Add(FloatToStr(C));
end;
end; procedure TSaveMacForm.CheckIsEmpty;
begin
if (Edit1.Text = '') or (Edit2.Text = '') or (Edit3.Text = '') or (Edit4.Text = '') or (Edit5.Text = '') or (Edit6.Text = '') then
begin
Application.MessageBox('输入框必须是12个16进制的数!', '提示', mb_OK + MB_ICONSTOP);
exit;
end; if (length(Edit1.Text )< 2) or (length(Edit1.Text) < 2) or (length(Edit1.Text )< 2) or (length(Edit1.Text) < 2) or (length(Edit1.Text) < 2) or (length(Edit1.Text) < 2) then
begin
Application.MessageBox('每个输入框必须是2个数字!', '提示', MB_OK + MB_ICONERROR);
exit;
end; if (Edit7.Text = '') then
begin
Application.MessageBox('步长不能为空!', '提示', mb_OK);
Edit7.SetFocus;
exit;
end;
if (Edit8.Text = '') then
begin
Application.MessageBox('长度不能为空!', '提示', mb_OK);
Edit8.SetFocus;
exit;
end;
end; function TSaveMacForm.CheckStepOrLen: Boolean;
var
step, L, code, J : integer;
isnot: Boolean;
C: Extended;
begin
result := False;
if Edit7.Text <> '' then begin // 2015/10/24 增加,之前没判断,若为空时 会出现系统错误
step := StrToInt(Edit7.Text);
L := StrToInt(Edit8.Text);
if step > 256 then
begin
Application.MessageBox('步长最大长度为256,请重新输入!', '提示', mb_OK);
Edit7.SetFocus;
exit;
end;
end; // 判断长度
if L >= 100000 then
begin
Application.MessageBox('长度太长,请重新输入!', '提示', mb_OK);
Edit8.SetFocus;
exit;
end; for J := 1 to 8 do
begin
C := power(2, J); /// 2的I次方
if (step = C) or (step = 1) then
begin
result := True;
end;
end; /// 判断步长是否满足条件
if result = False then
begin
Application.MessageBox('步长只能为2的n次方,请重新输入', '提示消息', mb_OK + MB_ICONERROR);
Edit7.SetFocus;
exit;
end;
end; procedure TSaveMacForm.btn_showClick(Sender: TObject);
begin
ShowData();
end; procedure TSaveMacForm.ShowData;
var
SaveFile: string;
value: Integer;
temp, S: string;
I, M1, M2, M3, M4, M5, M6, J : Integer;
C: Extended;
begin
CheckIsEmpty; // 判断是否为空 if CheckStepOrLen = True then begin
Self.Memo1.Lines.Clear;
M1 := StrToInt('$' + Edit1.Text); /// 直接把edit里的数据变成16进制 (是由十进制To十六进制)
M2 := StrToInt('$' + Edit2.Text);
M3 := StrToInt('$' + Edit3.Text);
M4 := StrToInt('$' + Edit4.Text);
M5 := StrToInt('$' + Edit5.Text);
M6 := StrToInt('$' + Edit6.Text);
if RadioButton1.Checked then
Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2)); /// 将十六进制To十进制
if RadioButton2.Checked then
Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + ':' + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));
if RadioButton3.Checked then
Self.Memo1.Lines.Add(IntToHex(M1, 2) + '-' + IntToHex(M2, 2) + '-' +IntToHex(M3, 2) + '-' + IntToHex(M4, 2) + '-' + IntToHex(M5, 2) + '-' + IntToHex(M6, 2));
for I := 1 to StrToInt(Edit8.Text) do
begin
temp := '0' ;
if M6 < 16 then /// 小于F,则加个0
begin
M6 := StrToInt('$' + IntToStr(M6));
M6 := StrToInt(temp + IntToHex(m6, 2));
end;
if M5 < 16 then
begin
M5 := StrToInt('$' + IntToStr(M5));
M5 := StrToInt(temp + IntToHex(m5, 2));
end;
if M4 < 16 then
begin
M4 := StrToInt('$' + IntToStr(M4));
M4 := StrToInt(temp + IntToHex(m4, 2));
end;
if M3 < 16 then
begin
M3 := StrToInt('$' + IntToStr(M3));
M3 := StrToInt(temp + IntToHex(m3, 2));
end;
if M2 < 16 then
begin
M2 := StrToInt('$' + IntToStr(M2));
M2 := StrToInt(temp + IntToHex(m2, 2));
end;
if M1 < 16 then
begin
M1 := StrToInt('$' + IntToStr(M1));
M1 := StrToInt(temp + IntToHex(m1, 2));
end;
M6 := M6 + StrToInt(Edit7.Text); /// 加步长
if M6 >= 256 then /// 若大于等于FF则进位
begin
M5 := M5 + 1;
M6 := M6 - 256;
end;
if M5 >= 256 then
begin
M4 := M4 + 1;
M5 := M5 - 256;
end;
if M4 >= 256 then
begin
M3 := M3 + 1;
M4 := M4 - 256;
end;
if M3 >= 256 then
begin
M2 := M2 + 1;
M3 := M3 - 256 ;
end;
if M2 >= 256 then
begin
M1 := M1 + 1;
M2 := M2 - 256;
end;
if M1 >= 256 then
Application.MessageBox('超出范围', '提示消息', mb_OK + MB_ICONERROR);
if RadioButton1.Checked then
Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));
if RadioButton2.Checked then
Self.Memo1.Lines.Add(IntToHex(M1, 2) + IntToHex(M2, 2) + IntToHex(M3, 2) + ':' + IntToHex(M4, 2) + IntToHex(M5, 2) + IntToHex(M6, 2));
if RadioButton3.Checked then
Self.Memo1.Lines.Add(IntToHex(M1, 2) + '-' + IntToHex(M2, 2) + '-' +IntToHex(M3, 2) + '-' + IntToHex(M4, 2) + '-' + IntToHex(M5, 2) + '-' + IntToHex(M6, 2));
end;
end;
end; procedure TSaveMacForm.SaveData;
var
SaveFile: string;
begin
if CheckStepOrLen = True then begin
/// 保存
SaveDialog1.Filter := '*.txt|*.txt'; //'txtFile( *.txt)|*.txt'; /// txt格式
SaveDialog1.DefaultExt := '*.txt'; /// 默认格式
SaveDialog1.Title := '保存MAC地址';
if SaveDialog1.Execute then
begin
SaveFile := SaveDialog1.FileName;
Memo1.Lines.SaveToFile(SaveFile); /// 保存memo里数据
end;
end;
end; procedure TSaveMacForm.SaveButtonClick(Sender: TObject);
begin
ShowData();
end; procedure TSaveMacForm.Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i: integer;
begin
if length(Edit1.Text) >= 2 then
Edit2.SetFocus;
if length(Edit2.Text ) >= 2 then
Edit3.SetFocus;
if length(Edit3.Text ) >= 2 then
Edit4.SetFocus;
if length(Edit4.Text ) >= 2 then
Edit5.SetFocus;
if Length(Edit5.Text ) >= 2 then
Edit6.SetFocus;
(* for i := 0 to ComponentCount - 1 do begin
if Components[i] is TEdit then
if Length((Components[i] as TEdit).Text) >= 2 then
end;*)
end; procedure TSaveMacForm.btn_clearClick(Sender: TObject);
var
i: integer;
begin
for i := 0 to self.ComponentCount -1 do begin
if self.Components[i] is TEdit then begin
(self.Components[i] as TEdit).Text := '';
end;
end;
Edit1.SetFocus;
end; end.