⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 account.~pas

📁 求是科技出版的《Delphi串口通信工程开发实例导航》所有的源代码。是一本很好的书。拿出来与大家共享。
💻 ~PAS
字号:
unit Account;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Buttons, Contnrs, CoreData, iniFiles;

type
  TfrmAccount = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    edtAccount: TEdit;
    Label2: TLabel;
    edtServer: TEdit;
    Label3: TLabel;
    edtPort: TEdit;
    Label4: TLabel;
    edtUser: TEdit;
    Label5: TLabel;
    edtPwd: TEdit;
    Label6: TLabel;
    edtMobile: TEdit;
    btnAdd: TButton;
    btnModify: TButton;
    btnDelete: TBitBtn;
    btnClose: TBitBtn;
    lstAccount: TListView;
    chkSendSMS: TCheckBox;
    procedure btnAddClick(Sender: TObject);
    procedure btnModifyClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure lstAccountChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure FormShow(Sender: TObject);
    procedure lstAccountClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    procedure ReadAccountInfo();
    procedure SaveAccountInfo();
    procedure ReleaseAll();
  public
    { Public declarations }
  end;
var
  frmAccount: TfrmAccount;      // Account 对话话框
  m_lstAccount : TList;         // 用来存储当前账号的链表
  m_IsChanged : Boolean;        // 标志账号信息是否改变

implementation

{$R *.dfm}

//
// 添加按钮单击事件处理程序
//
procedure TfrmAccount.btnAddClick(Sender: TObject);
var
        pAccount : ^TAccountInfo;
        Item : TListItem;
begin
        //
        // 分配空间
        //
        if m_lstAccount = nil then
                m_lstAccount := TList.Create();

                
        // 确保各文本框已经添写内容
        if (edtAccount.Text = '')  or (edtServer.Text = '')
           or (edtport.Text = '')  or (edtUser.Text = '')
           or (edtPwd.Text = '') or (edtMobile.Text = '') then
                exit;

        //
        // 初始化pAccount,并加入链表 m_lstAccount 中
        //
        new(pAccount);
        pAccount^.m_Account := edtAccount.Text;
        pAccount^.m_Server := edtServer.Text;
        pAccount^.m_Port := edtPort.Text;
        pAccount^.m_User := edtUser.Text;
        pAccount^.m_Pwd := edtPwd.Text;
        pAccount^.m_Mobile := edtMobile.Text;
        pAccount^.m_SendToMobile := chkSendSMS.Checked;
        pAccount^.m_lstEmail := TList.Create();

        m_lstAccount.Add(pAccount);

        //
        // 加入账号显示列表框中
        //
        item := lstAccount.Items.Add();
        with pAccount^ do
        begin
                item.Caption := m_Account;
                item.SubItems.Clear();
                item.SubItems.Add(m_Server);
                item.SubItems.Add(m_User);
                item.SubItems.Add(m_Mobile);
        end;

        //
        // 设置修改标志
        //
        m_IsChanged := true;
end;


//
// 修改按钮单击事件处理程序
//
procedure TfrmAccount.btnModifyClick(Sender: TObject);
var
        pAccount : ^TAccountInfo;
        Item : TListItem;
begin
        // 找到选中的列表项
        Item := lstAccount.Selected;
        if Item = nil then
                exit;
        // 确保各文本框已经添写内容
        if (edtAccount.Text = '')  or (edtServer.Text = '')
           or (edtport.Text = '')  or (edtUser.Text = '')
           or (edtPwd.Text = '') or (edtMobile.Text = '') then
                exit;

        //
        // 获取 m_lstAccount 中对应的pAccount,并更新之
        //
        pAccount := m_lstAccount.Items[lstAccount.Selected.Index];
        pAccount^.m_Account := edtAccount.Text;
        pAccount^.m_Server := edtServer.Text;
        pAccount^.m_Port := edtPort.Text;
        pAccount^.m_User := edtUser.Text;
        pAccount^.m_Pwd := edtPwd.Text;
        pAccount^.m_SendToMobile := chkSendSMS.Checked;
        pAccount^.m_Mobile := edtMobile.Text;

        //
        // 更新账号显示列表框的显示
        //
        with pAccount^ do
        begin
                item.Caption := m_Account;
                item.SubItems.Clear();
                item.SubItems.Add(m_Server);
                item.SubItems.Add(m_User);
                item.SubItems.Add(m_Mobile);
        end;
        //
        // 设置修改标志
        //
        m_IsChanged := true;
end;

//
// 删除按钮单击事件处理程序
//
procedure TfrmAccount.btnDeleteClick(Sender: TObject);
var
        pAccount : ^TAccountInfo;
        Item : TListItem;
        SelIndex : integer;
begin
        // 找到选中的列表项
        Item := lstAccount.Selected;
        SelIndex := lstAccount.ItemIndex;
        if Item = nil then
                exit;

        //
        // 获取 m_lstAccount 中对应的pAccount,并删除之
        //
        pAccount := m_lstAccount.Items[lstAccount.Selected.Index];
        m_lstAccount.Delete(lstAccount.Selected.Index);
        dispose(pAccount);
        //
        // 更新账号显示列表框的显示
        //
        //lstAccount.DeleteSelected();
        //if SelIndex  > 0 then
        //        lstAccount.ItemIndex := SelIndex - 1;


        lstAccount.Items.Count;
        lstAccount.Items[SelIndex].Delete;

        //lstAccount.Items.Delete(SelIndex);
        //
        // 设置修改标志
        //
        m_IsChanged := true;
end;

//
// 列表选择发生改变时处理程序
//
procedure TfrmAccount.lstAccountChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
var
        pAccount : ^TAccountInfo;
begin
        //
        // 获取 m_lstAccount 中对应的pAccount,并删除之
        //
        if lstAccount.Selected = nil then
                exit;

        pAccount := m_lstAccount.Items[lstAccount.Selected.index];
        //
        // 更新账号显示列表框的显示
        //
        with pAccount^ do
        begin
                edtAccount.Text := m_Account;
                edtServer.Text := m_Server;
                edtPort.Text := m_Port;
                edtUser.Text := m_User;
                edtPwd.Text := m_Pwd;
                edtMobile.Text := m_Mobile;
                chkSendSMS.Checked := m_SendToMobile;
        end;
end;

//
// 窗体显示时处理事件
//
procedure TfrmAccount.FormShow(Sender: TObject);
var
        pAccount : ^TAccountInfo;
        Item : TListItem;
        i : integer;
begin
        //
        // 设置修改标志
        //
        m_IsChanged := false;
        
        if m_lstAccount = nil then
                exit;
        if lstAccount.Items.Count > 0 then
                exit;
        //
        // 遍历 m_lstAccount 所有账号
        //
        for i := 0 to m_lstAccount.Count - 1 do
        begin
                pAccount := m_lstAccount.Items[i];
                //
                // 更新账号显示列表框的显示
                //
                item := lstAccount.Items.Add();
                with pAccount^ do
                begin
                        item.Caption := m_Account;
                        item.SubItems.Clear();
                        item.SubItems.Add(m_Server);
                        item.SubItems.Add(m_User);
                        item.SubItems.Add(m_Mobile);
                end;
        end;
end;

//
// 列表框单击处理事件
//
procedure TfrmAccount.lstAccountClick(Sender: TObject);
var
        pAccount : ^TAccountInfo;
begin
        if lstAccount.Selected = nil then
                exit;
        //
        // 获取 m_lstAccount 中对应的pAccount,并删除之
        //
        pAccount := m_lstAccount.Items[lstAccount.Selected.index];
        //
        // 更新账号显示列表框的显示
        //
        with pAccount^ do
        begin
                edtAccount.Text := m_Account;
                edtServer.Text := m_Server;
                edtPort.Text := m_Port;
                edtUser.Text := m_User;
                edtPwd.Text := m_Pwd;
                edtMobile.Text := m_Mobile;
                chkSendSMS.Checked := m_SendToMobile;
        end;
end;

//
// 读取配置文件中的账号和邮件信息
//
procedure TfrmAccount.ReadAccountInfo;
var
        Configini: TIniFile;
        Account_num : integer;
        i : integer;
        stream : TStringStream;
        FilePath : string;
        pAccount : ^TAccountInfo;

        pEmail : ^TEmailInfo;
        DataFile : File of TEmailInfo;
begin
        FilePath := ExtractFilePath(ParamStr(0));
        if not FileExists(FilePath + 'MailConfig.ini') then
                exit;
        ConfigIni := TIniFile.Create(FilePath + 'MailConfig.ini');
        with Configini do
        begin
                //
                // 账号数据
                //
                Account_num := ReadInteger('Account', 'Account Number', 0);
                for i := 0 to Account_num - 1 do
                begin
                        Stream := TStringStream.Create('');
                        ReadBinaryStream('Account' + IntToStr(i), 'Id', Stream);
                        new(pAccount);
                        pAccount^.m_Pwd := Stream.DataString;
                        pAccount^.m_Account := ReadString('Account' + IntToStr(i), 'Account', 'account');
                        pAccount^.m_Server := ReadString('Account' + IntToStr(i), 'Server', 'server');
                        pAccount^.m_Port := ReadString('Account' + IntToStr(i),'Port', '110');
                        pAccount^.m_User := ReadString('Account' + IntToStr(i), 'User', 'User');
                        pAccount^.m_Mobile := ReadString('Account' + IntToStr(i),'Mobile', '');
                        pAccount^.m_SendToMobile := ReadBool('Account' + IntToStr(i), 'SendToMobile', True);

                        if m_lstAccount = nil then
                                m_lstAccount := TList.Create();
                        m_lstAccount.Add(pAccount);
                        //
                        // 读取邮件信息
                        //
                        if not FileExists(FilePath + pAccount^.m_Account + '.emi') then
                                continue;
                        pAccount^.m_lstEmail := TList.Create();

                        AssignFile(DataFile, FilePath + pAccount^.m_Account + '.emi');
                        Reset(DataFile);
                        try
                                while not Eof(DataFile) do
                                begin
                                        new(pEmail);
                                        Read(DataFile, pEmail^);
                                        pAccount^.m_lstEmail.Add(pEmail);
                                end;
                        Finally
                                CloseFile(DataFile);
                        end;
                end;  // end of for ...
        end;  // end of with Configini
        ConfigIni.Free;
end;

//
// 将当前的账号和邮件信息保存到配置文件中
//
procedure TfrmAccount.SaveAccountInfo;
var
        Configini: TIniFile;
        stream : TStringStream;
        i : integer;
        pAccount : ^TAccountInfo;
        FilePath : string;

        pEmail : ^TEmailInfo;
        DataFile : File of TEmailInfo;
        mail_index : integer;
begin
        if m_lstAccount = nil then
        begin
                exit;
        end;
        FilePath := ExtractFilePath(ParamStr(0));
        ConfigIni :=TIniFile.Create(FilePath + 'MailConfig.ini');
        with Configini do
        begin
                //
                // 账号数据
                //
                WriteInteger('Account', 'Account Number', m_lstAccount.Count);
                for i:= 0 to m_lstAccount.Count - 1 do
                begin
                        pAccount := m_lstAccount.Items[i];
                        stream := TStringStream.Create(pAccount^.m_Pwd);
                        WriteBinaryStream('Account' + IntToStr(i), 'Id', stream);
                        WriteString('Account' + IntToStr(i), 'Account', pAccount^.m_Account);
                        WriteString('Account' + IntToStr(i), 'Password', '@@@@@@@@@');
                        WriteString('Account' + IntToStr(i), 'Server', pAccount^.m_Server);
                        WriteString('Account' + IntToStr(i), 'Port', pAccount^.m_Port);
                        WriteString('Account' + IntToStr(i), 'User', pAccount^.m_User);
                        WriteString('Account' + IntToStr(i), 'Mobile', pAccount^.m_Mobile);
                        WriteBool('Account' + IntToStr(i), 'SendToMobile', pAccount^.m_SendToMobile);
                        //
                        // 存储邮件信息
                        //
                        if pAccount^.m_lstEmail = nil then
                                continue;
                        AssignFile(DataFile, FilePath + pAccount^.m_Account + '.emi');
                        Rewrite(DataFile);
                        try
                                for mail_index := 0 to pAccount^.m_lstEmail.Count - 1 do
                                begin
                                        pEmail := pAccount^.m_lstEmail.Items[mail_index];
                                        write(DataFile, pEmail^);
                                end;
                        finally
                                CloseFile(DataFile);
                        end;
                end;  // end of for...
        end;  // end of with configini
        ConfigIni.Free;
end;

//
// 窗体创建时触发此事件
//
procedure TfrmAccount.FormCreate(Sender: TObject);
begin
        ReadAccountInfo();
end;

//
// 窗体销毁时触发此事件
//
procedure TfrmAccount.FormDestroy(Sender: TObject);
begin
        //
        // 保存账号信息
        //
        SaveAccountInfo();
        //
        // 释放内存
        //
        ReleaseAll();
end;

//
// 释放内存
//
procedure TfrmAccount.ReleaseAll;
var
        i, j : integer;
        pAccount : ^TAccountInfo;
        pEmail : ^TEmailInfo;
begin
        if m_lstAccount = nil then
                exit;
        //
        // 释放m_lstAccount链表
        //
        for i := 0 to m_lstAccount.Count - 1 do
        begin
                pAccount := m_lstAccount.Items[i];
                //
                // 释放m_lstEmail链表
                //
                if pAccount^.m_lstEmail <> nil then
                begin
                        for j := 0 to pAccount^.m_lstEmail.Count - 1 do
                        begin
                                pEmail := pAccount^.m_lstEmail.Items[j];
                                Dispose(pEmail);
                        end;
                        pAccount^.m_lstEmail.Free;
                end;
                Dispose(pAccount);
        end;
        m_lstAccount.Free();
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -