📄 mainform.pas
字号:
unit mainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,CoolTrayIcon, ImgList, Menus, IdBaseComponent,IdComponent,
IdTCPConnection, IdTCPClient, IdMessageClient, IdPOP3, ExtCtrls, StdCtrls,
IdMessage, ComCtrls,INIFiles, ToolWin, ActnList;
type
TinitForm = class(TForm)
CoolTrayIcon1: TCoolTrayIcon;
PopupMenu1: TPopupMenu;
ImageList2: TImageList;
menu_showMainFrorm: TMenuItem;
menu_abort: TMenuItem;
Timer1: TTimer;
POP: TIdPOP3;
menu_exit: TMenuItem;
Msg: TIdMessage;
lvHeaders: TListView;
MainMenu1: TMainMenu;
N_system: TMenuItem;
N_exit: TMenuItem;
StatusBar1: TStatusBar;
N_helpmenu: TMenuItem;
N_abort: TMenuItem;
N_help: TMenuItem;
menu_refurbish: TMenuItem;
Panel1: TPanel;
N_refurbish: TMenuItem;
ToolBar1: TToolBar;
ToolButton_checkMail: TToolButton;
ToolButton_disconnect: TToolButton;
ToolButton_setup: TToolButton;
ImageList1: TImageList;
ActionList1: TActionList;
CheckMail: TAction;
Disconnect: TAction;
setup: TAction;
Panel2: TPanel;
ToolButton1: TToolButton;
N_config: TMenuItem;
menu_config: TMenuItem;
Memo1: TMemo;
ToolButton2: TToolButton;
refurbish: TAction;
procedure FormCreate(Sender: TObject);
procedure menu_exitClick(Sender: TObject);
procedure menu_abortClick(Sender: TObject);
procedure menu_showMainFrormClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure menu_refurbishClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure lvHeadersDblClick(Sender: TObject);
procedure CheckMailExecute(Sender: TObject);
procedure DisconnectExecute(Sender: TObject);
procedure setupExecute(Sender: TObject);
procedure N_configClick(Sender: TObject);
private
//----检查Email----//
procedure CheckEmail();
//----控制托盘图标是否闪烁----//
procedure TrayIconFlash(isFlash : Boolean);
//----建立pop3连接----//
function ConnectPop3():Integer;
//----断开pop3连接----//
procedure DisconnectPop3();
//----Base64编码转换----//
function DecodeBase64(s: string): String;
//----读取Config.ini配置信息----//
procedure ReadConfigInfo();
//----根据连接状态改变参数----//
procedure ToggleStatus(const Status: Boolean);
public
end;
var
//****Form Object****//
initForm: TinitForm; //主窗体 现实信息列表
Pop3ServerName : String; //pop3服务名
Pop3ServerUser : String; //pop3用户名
Pop3ServerPassword : String; //pop3密码
Pop3ServerPort : Integer; //pop3端口号
UserEmail : String; //个人Email
TimerNum : Integer; //定时刷新间隔分钟数
srtHitInfo:String; //系统提示信息
FMsgCount : Integer; //邮件数
implementation
{$R *.dfm}
uses messageBox,config;
//****私有方法****//
//----检查Email,将Email信息写入ListView----//
procedure TinitForm.CheckEmail();
var
itm: TListItem; //列表对象
intIndex: integer; //Email索引值
begin
//showmessage(inttostr(FMsgCount));
srtHitInfo:= ' 您好,信箱中没有您的Email!';
if FMsgCount > 0 then
begin
lvHeaders.Clear; //清空ListView
//----循环取Email信息----//
for intIndex:=FMsgCount downto 1 do
begin
Msg.Clear;
POP.RetrieveHeader(intIndex, Msg); //获取相应索引的Email信息
itm:= lvHeaders.Items.Add;
itm.ImageIndex := 3;
itm.Caption := DecodeBase64(Msg.Subject);
itm.SubItems.Add(Msg.From.Text);
itm.SubItems.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Msg.Date));
itm.SubItems.Add(IntToStr(POP.RetrieveMsgSize(intIndex)));
itm.SubItems.Add('n/a-'+IntToStr(Msg.MessageParts.Count));
end; // for end
//----托盘图标闪烁----//
self.TrayIconFlash(True);
//----更新系统提示信息----//
srtHitInfo:= ' 您好,您信箱中有"'+intTostr(FMsgCount)+'"封Email!,注意接收!';
MsgBox.Label_msgInfo.Caption:=srtHitInfo;
MsgBox.FormStyle:=fsStayOnTop;
CoolTrayIcon1.Hint:= srtHitInfo;
StatusBar1.Panels[0].Text:= srtHitInfo;
//----控制提示窗口----//
//ShowMessage(booltostr(Assigned(Application.FindComponent('MsgBox'))));
if Assigned(Application.FindComponent('MsgBox')) then
begin
if not initForm.Showing then
begin
MsgBox.Show;
end
else
begin
MsgBox.Hide;
end;
end;
end; //"if FMsgCount > 0" End
end;
//----控制托盘图标闪烁----//
procedure TinitForm.TrayIconFlash(isFlash: Boolean);
begin
CoolTrayIcon1.CycleIcons := False;
if isFlash then
begin
CoolTrayIcon1.IconList := ImageList1;
CoolTrayIcon1.CycleInterval := 400;
CoolTrayIcon1.CycleIcons := True;
end
else
begin
CoolTrayIcon1.CycleIcons := False;
CoolTrayIcon1.IconList := ImageList1;
end;
end;
function TinitForm.ConnectPop3: Integer;
var
msgCount:Integer;
begin
try
//----建立pop3连接----//
StatusBar1.Panels[1].Text:= '连接中...';
StatusBar1.Refresh;
if POP.Connected then
begin
POP.Disconnect;
end;
POP.Host := Pop3ServerName;
POP.Port := Pop3ServerPort;
POP.Username := Pop3ServerUser;
POP.Password := Pop3ServerPassword;
POP.Connect;
msgCount := POP.CheckMessages; //邮件数量
self.ToggleStatus(true);
result := msgCount;
except
self.ToggleStatus(false);
result:=-1;
end;
end;
//----断开pop3连接----//
procedure TinitForm.DisconnectPop3;
begin
if POP.Connected then
begin
POP.Disconnect;
self.ToggleStatus(false);
end;
end;
//----从Config.ini文件读取配置信息----//
procedure TinitForm.ReadConfigInfo;
var
SYSINI: TINIFile;
begin
SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'\Config.ini');
Pop3ServerName := SYSINI.ReadString('POP3ServerINFO', 'Pop3ServerName', '');
Pop3ServerPort := strtoint(SYSINI.ReadString('POP3ServerINFO', 'Pop3ServerPort', ''));
Pop3ServerUser := SYSINI.ReadString('POP3ServerINFO', 'Pop3ServerUser', '');
Pop3ServerPassword := SYSINI.ReadString('POP3ServerINFO', 'Pop3ServerPassword', '');
TimerNum:=strtoint(SYSINI.ReadString('Config', 'Timer', ''));
UserEmail := SYSINI.ReadString('Email', 'UserEmail', '');
SYSINI.Free;
end;
//----Base64编码转换----//
function TinitForm.DecodeBase64(s: string): String;
var
s1,s2,s3: integer;
t,v: string;
Encoding: char;
hex,step: integer;
a1: array[1..4] of byte;
b1: array[1..3] of byte;
j: integer;
byte_ptr,real_bytes: integer;
begin
s1:=Pos('=?',s);
s2:= 1 ;
hex:= 0 ;
if s1>0 then
begin
for s2:=Length(s)-1 downto 1 do
begin
if Copy(s,s2,2)='?=' then Break;
end;
end;
if (s1=0) or (s2=1) then
begin
Result:=s;
Exit;
end;
t:=Copy(s,s1+2,s2-2-s1);
s3:=Pos('?',t);
Delete(t,1,s3);
if(t='')then
begin
Result:= s;
Exit ;
end ;
Encoding:=t[1];
Delete(t,1,2);
v:='';
step:=0;
case Encoding of
'Q':
while t<>'' do
begin
case step of
0:
begin
case t[1] of
'_': v:=v+' ';
'=': step:=1;
else v:=v+t[1];
end;
end;
1:
begin
if t[1]<='9' then hex:=(Ord(t[1])-Ord('0'))*16
else hex:=(Ord(t[1])-55)*16;
step:=2;
end;
2:
begin
if t[1]<='9' then hex:=hex+(Ord(t[1])-Ord('0'))
else hex:=hex+Ord(t[1])-55;
v:=v+Chr(hex);
step:=0;
end;
end;
Delete(t,1,1);
end;
'B':
begin
byte_ptr:=0;
for j:=1 to Length(t) do
begin
Inc(byte_ptr);
case t[j] of
'A'..'Z': a1[byte_ptr]:=Ord(t[j])-65;
'a'..'z': a1[byte_ptr]:=Ord(t[j])-71;
'0'..'9': a1[byte_ptr]:=Ord(t[j])+4;
'+': a1[byte_ptr]:=62;
'/': a1[byte_ptr]:=63;
'=': a1[byte_ptr]:=64;
end;
if byte_ptr=4 then
begin
byte_ptr:=0;
real_bytes:=3;
if a1[1]=64 then real_bytes:=0;
if a1[3]=64 then
begin
a1[3]:=0;
a1[4]:=0;
real_bytes:=1;
end;
if a1[4]=64 then
begin
a1[4]:=0;
real_bytes:=2;
end;
b1[1]:=a1[1]*4+(a1[2] div 16);
b1[2]:=(a1[2] mod 16)*16+(a1[3]div 4);
b1[3]:=(a1[3] mod 4)*64 +a1[4];
if(real_bytes>0)then
v:= v + chr(b1[1]) ;
if(real_bytes>1)then
v:= v + chr(b1[2]) ;
if(real_bytes>2)then
v:= v + chr(b1[3]) ;
end;
end;
end;
end;
Result := v;
end;
procedure TinitForm.FormCreate(Sender: TObject);
begin
CoolTrayIcon1.Refresh;
try
self.ReadConfigInfo;
except
MessageDlg('获取系统配置信息失败!',mtInformation,[mbOK],0);
self.setupExecute(self);
end;
FMsgCount := self.ConnectPop3();
end;
procedure TinitForm.menu_exitClick(Sender: TObject);
begin
self.DisconnectPop3;
self.Close;
end;
procedure TinitForm.menu_abortClick(Sender: TObject);
begin
showmessage('提醒程序!');
end;
//----定时刷新----//
procedure TinitForm.Timer1Timer(Sender: TObject);
begin
Timer1.Interval:=60000*TimerNum; //分钟
self.menu_refurbishClick(self);
end;
procedure TinitForm.menu_showMainFrormClick(Sender: TObject);
begin
self.Show;
end;
procedure TinitForm.FormShow(Sender: TObject);
begin
MsgBox.Hide;
self.TrayIconFlash(false);
end;
//----手动刷新----//
procedure TinitForm.menu_refurbishClick(Sender: TObject);
begin
//----建立pop3连接----//
FMsgCount := self.ConnectPop3();
if FMsgCount=-1 then
MessageDlg('Pop3服务器连接失败!',mtInformation,[mbOK],0)
else
self.CheckEmail();
end;
procedure TinitForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caMinimize;
CoolTrayIcon1.HideMainForm;
end;
//----双击标题----//
procedure TinitForm.lvHeadersDblClick(Sender: TObject);
var
i:Integer;
begin
try
POP.Retrieve(lvHeaders.Selected.Index + 1, Msg);
for i := 0 to Pred(Msg.MessageParts.Count) do
begin
//showmessage(inttostr(Msg.MessageParts.Count)+'#'+Msg.MessageParts.Items[i].Boundary+'#'+inttostr(Pred(Msg.MessageParts.Count))+'#'+inttostr(Msg.MessageParts.Count));
if (Msg.MessageParts.Items[i] is TIdAttachment) then
begin
//Memo1.Lines.Clear;
//Memo1.Text:=TIdAttachment(Msg.MessageParts.Items[i]).Filename+TIdAttachment(Msg.MessageParts.Items[i]).ContentType;
end;
if Msg.MessageParts.Items[i] is TIdText then
begin
Panel2.Visible:=True;
Memo1.Lines.Clear;
Memo1.Lines.AddStrings(TIdText(Msg.MessageParts.Items[i]).Body);
end;
end;
except
end;
end;
procedure TinitForm.CheckMailExecute(Sender: TObject);
begin
//----建立pop3连接----//
FMsgCount := self.ConnectPop3();
if FMsgCount=-1 then
MessageDlg('Pop3服务器连接失败!',mtInformation,[mbOK],0)
else
self.CheckEmail();
end;
procedure TinitForm.DisconnectExecute(Sender: TObject);
begin
self.DisconnectPop3;
end;
procedure TinitForm.setupExecute(Sender: TObject);
begin
Application.CreateForm(TConfigForm, ConfigForm);
ConfigForm.ShowModal;
end;
procedure TinitForm.ToggleStatus(const Status: Boolean);
begin
self.CheckMail.Enabled := not Status;
self.refurbish.Enabled:= Status;
self.Disconnect.Enabled := Status;
if Status then
begin
StatusBar1.Panels[1].Text:= '已连接';
end
else
begin
StatusBar1.Panels[1].Text:= '已断开';
end;
end;
procedure TinitForm.N_configClick(Sender: TObject);
begin
Application.CreateForm(TConfigForm, ConfigForm);
ConfigForm.ShowModal;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -