📄 main.cpp
字号:
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
#include "Log.h"
#include "Global.h"
#include "Setting.h"
#include "About.h"
#include "ShowMessage.h"
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TMainForm *MainForm;
const int TIMERMODE_FLASH = 1;
const int TIMERMODE_TIMEOUT = 2;
const int TIMEOUT = 20000;
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
TLogForm *LogForm = new TLogForm(this);
LogForm->ShowModal();
if(!LogForm->m_bResult)
{ delete LogForm;
Application->ShowMainForm = false;
Application->Terminate();
return;
}
Global.SetLogin(LogForm->edt_User->Text.c_str(),LogForm->edt_Pwd->Text.c_str());
delete LogForm;
TrayIcon->Visible = true;
Width = img_BackGround->Width;
Height = img_BackGround->Height;
m_bDown = false;
mf_Init(); //初始化
Timer->Enabled = true; //开始监控
m_iTimerMode = TIMERMODE_TIMEOUT; //定时器工作方式
}
//定时发送身份信息到服务器,以获得OA新信息
void __fastcall TMainForm::TimerTimer(TObject *Sender)
{
if(!Global.AcceptConnect) return;
CUdpPackage Package;
CLogin Login = Global.Login;
Package.Header.BuffSize = sizeof(CLogin);
Package.Header.Type = IDENTIFY;
Package.SetBuff(&Login);
int iSize = Package.Header.HeadSize + Package.Header.BuffSize;
UdpServer->SendBuffer(Global.RemoteHost,Global.Port,&Package,iSize);
mf_Setting();
Timer1->Interval = TIMEOUT;
m_iTimerMode = TIMERMODE_TIMEOUT;
Timer1->Enabled = true;
}
void __fastcall TMainForm::UdpServerUDPRead(TObject *Sender,
TStream *AData, TIdSocketHandle *ABinding)
{ //若定时器工作模式是超时模式,说明没有超时
AnsiString FromIP = ABinding->PeerIP;
int NumberBytes = AData->Size;
if( FromIP == AnsiString(Global.RemoteHost) && ABinding->PeerPort == Global.Port)
{ if(m_iTimerMode == TIMERMODE_TIMEOUT) Timer1->Enabled = false;
if(TrayIcon->IconIndex == 2) TrayIcon->IconIndex = 0;
TrayIcon->Hint = Application->Title;
}
else
return;
//若已停止,不接收
if( NumberBytes < sizeof(CHeader) || Global.AcceptConnect == false)
return;
CUdpPackage Package;
//((TNMUDP*)Sender)->ReadBuffer((char*)&Package,NumberBytes,NumberBytes);
AData->ReadBuffer((char*)&Package,NumberBytes);
if(Package.Header.Version != Global.Version)
{ MessageBox(Handle,"服务器与客户端的版本不符!","提示",MB_ICONERROR);
btn_StopClick(this);
return;
}
if(Package.Header.RowCount <= 0) return;
//若不是最后一组数据,先放缓冲区
Global.PutToBuff(Package);
if(Package.Header.BlockID != -1) return;
switch(Package.Header.Type)
{ case NOTIFY: mf_Notify(Package); break;
case USERMSG: mf_UserMsg(Package); break;
case SHUTDOWN: SendMessage(Handle,WM_CLOSE,0,0);
return;
default: break;
}
Global.ResetPackBuff();
}
void __fastcall TMainForm::pm_RestoreClick(TObject *Sender)
{
Show();
TrayIcon->Restore();
}
void __fastcall TMainForm::str_CloseClick(TObject *Sender)
{
TrayIcon->Minimize();
}
//移动
void __fastcall TMainForm::str_CaptionMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
m_bDown = true;
m_iX = X;
m_iY = Y;
}
void __fastcall TMainForm::str_CaptionMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
m_bDown = false;
}
void __fastcall TMainForm::str_CaptionMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if(m_bDown)
{ Left += X - m_iX;
Top += Y - m_iY;
}
}
void __fastcall TMainForm::N31Click(TObject *Sender)
{
btn_SettingClick(this);
}
//关于
void __fastcall TMainForm::str_HelpClick(TObject *Sender)
{
TAboutForm *AboutForm = new TAboutForm(this);
AboutForm->ShowModal();
delete AboutForm;
}
//参数设置
void __fastcall TMainForm::btn_SettingClick(TObject *Sender)
{
TSettingForm *pForm = new TSettingForm(this);
pForm->ShowModal();
delete pForm;
mf_SetStatusBar();
}
//启动
void __fastcall TMainForm::btn_StartClick(TObject *Sender)
{
Global.AcceptConnect = true;
btn_Start->Enabled = false;
btn_Stop->Enabled = true;
mf_SetStatusBar();
}
//停止
void __fastcall TMainForm::btn_StopClick(TObject *Sender)
{
Global.AcceptConnect = false;
btn_Start->Enabled = true;
btn_Stop->Enabled = false;
mf_SetStatusBar();
if(Global.GetNotifyMode(NOTIFY_PLAYSOUND))
{ if(SoundCardExists()&&FileExists(Global.SoundFile))
PlaySound("", NULL, SND_ASYNC|SND_FILENAME);
}
}
void __fastcall TMainForm::btn_AboutClick(TObject *Sender)
{
ShellExecute(Handle,NULL,"使用方法.chm",NULL,NULL,SW_SHOWMAXIMIZED);
}
//重新登录
void __fastcall TMainForm::N2Click(TObject *Sender)
{
TLogForm *Log = new TLogForm(this);
Log->ShowModal();
if(!Log->m_bResult)
{ delete Log;
return;
}
Global.SetLogin(Log->edt_User->Text.c_str(),Log->edt_Pwd->Text.c_str());
delete Log;
mf_Setting();
mf_SetStatusBar();
m_bLogTips = false;
tv_Watch->Items->Clear();
tv_Main->Items->Clear();
}
void __fastcall TMainForm::btn_CloseClick(TObject *Sender)
{
if(MessageBox(Handle,"即将退出程序,请确认!",Application->Title.c_str(),MB_ICONWARNING|MB_YESNO) == IDYES)
Close();
}
void __fastcall TMainForm::TrayIconRestore(TObject *Sender)
{
Show();
Application->BringToFront();
if(m_iTimerMode == TIMERMODE_FLASH)
TrayIcon->IconIndex = 0;
m_iTimerMode = TIMERMODE_TIMEOUT;
Timer1->Enabled = false;
}
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
//若定时器工作在超时工作状态,超时后设置工作
if(m_iTimerMode == TIMERMODE_TIMEOUT)
{ Timer1->Enabled = false;
TrayIcon->IconIndex = 2;
TrayIcon->Hint = "超时未响应";
tv_Watch->Items->Clear();
}
else if(m_iTimerMode == TIMERMODE_FLASH) //图标闪烁
{ static bOn = false;
TrayIcon->IconIndex = bOn ? 0 : 1;
bOn = !bOn;
}
}
//=====================================//
// 以下自定义成员函数 //
//=====================================//
//全局变量初始化与运行初始化
bool __fastcall TMainForm::mf_Init()
{
AnsiString szIniName = ExtractFilePath(Application->ExeName) + ChangeFileExt(ExtractFileName(Application->ExeName),".ini");
if(!FileExists(szIniName))
{ MessageBox(Handle,"初始化文件不存在!\n\n请在设置对话框中重新设置运行参数。","提示",MB_ICONERROR);
Global.AcceptConnect = false;
btn_Start->Enabled = true;
btn_Stop->Enabled = false;
return false;
}
//读取配置文件
TIniFile *IniFile = new TIniFile(szIniName);
AnsiString szHostName = IniFile->ReadString("Config","HostName","192.168.0.1");
int iPort = IniFile->ReadInteger("Config","Port",8270);
int iNotifyMode = IniFile->ReadInteger("Config","NotifyMode",1);
AnsiString szSoundFile = IniFile->ReadString("Config","SoundFile","");
int iInterval = IniFile->ReadInteger("Config","TimerInterval",4);
bool bET = IniFile->ReadBool("Config","EncryptTrans",false);
delete IniFile;
//初始化全局变量
Global.SetRemoteHost(szHostName.c_str());
Global.Port = iPort;
Global.NotifyMode = iNotifyMode;
Global.SetSoundFile(szSoundFile.c_str());
Global.EncryptTrans = bET;
Global.TimerInterval = iInterval;
//获取指定计算机的IP地址
hostent *p;
char s[128];
char *p2;
gethostname(s, 128);
p = gethostbyname(s);
p2 = inet_ntoa(*((in_addr *)p->h_addr));
TIdSocketHandle* pp = UdpServer->Bindings->Add();
pp->IP = p2;
pp->Port = Global.Port;
UdpServer->Active = true;
UdpServer->DefaultPort = Global.Port;
Global.AcceptConnect = true;
btn_Start->Enabled = false;
btn_Stop->Enabled = true;
m_bLogTips = false;
mf_SetStatusBar();
return true;
}
//设置
void __fastcall TMainForm::mf_Setting()
{
Timer->Interval = Global.TimerInterval * 60000;
mf_SetStatusBar();
}
//状态栏设置
void __fastcall TMainForm::mf_SetStatusBar()
{
StatusBar->Panels->Items[0]->Text = "用户名:" + Global.Login.GetUserName();
if(Global.AcceptConnect)
StatusBar->Panels->Items[1]->Text = "启动";
else
StatusBar->Panels->Items[1]->Text = "停止";
if(Global.EncryptTrans)
StatusBar->Panels->Items[2]->Text = "加密传输";
else
StatusBar->Panels->Items[2]->Text = "";
StatusBar->Panels->Items[3]->Text = "刷新间隔:" + IntToStr(Global.TimerInterval) + " 分钟";
}
//显示信息
void __fastcall TMainForm::mf_UserMsg(CUdpPackage Pack)
{
CUserMsg UserMsg;
Global.GetFromBuff(&UserMsg);
int iIcon = 0;
AnsiString szCaption = "";
switch(UserMsg.Type)
{ case MSG_INFORMATION: iIcon = MB_ICONINFORMATION; szCaption = "提示"; break;
case MSG_WARNING: iIcon = MB_ICONWARNING; szCaption = "警告"; break;
case MSG_ERROR: iIcon = MB_ICONERROR; szCaption = "错误"; break;
case MSG_LOGFAULT: iIcon = MB_ICONERROR; szCaption = "登录错误"; break;
default: return;
}
if(UserMsg.Type == MSG_LOGFAULT)
{ TrayIcon->IconIndex = 2;
TrayIcon->Hint = "非法的用户或口令";
if(!m_bLogTips) MessageBox(Handle,UserMsg.Message,szCaption.c_str(),iIcon);
m_bLogTips = true;
return;
}
AnsiString szMsg = UserMsg.Message;
MessageBox(Handle,UserMsg.Message,szCaption.c_str(),iIcon);
/*
if(szMsg.Pos("控制台消息:"))
{ szCaption = "--------- 控制台消息 ---------";
szMsg = szMsg.SubString(15,szMsg.Length() - 14);
}
else
szCaption = "------------ " + szCaption + " ------------";
MessageForm->mm_Body->Lines->Add(szCaption);
MessageForm->mm_Body->Lines->Add(szMsg);
MessageForm->mm_Body->Lines->Add("");
if(!MessageForm->Visible)
{ MessageForm->Visible = true;
MessageForm->Update();
}
Application->BringToFront();
*/
}
void __fastcall TMainForm::mf_Notify()
{
if(Global.GetNotifyMode(NOTIFY_POPWIN))
{ TrayIcon->Restore();
}
if(Global.GetNotifyMode(NOTIFY_ICONFLASH))
{ Timer1->Enabled = false;
m_iTimerMode = TIMERMODE_FLASH; //闪动图标
Timer1->Interval = 500;
Timer1->Enabled = true;
}
if(Global.GetNotifyMode(NOTIFY_PLAYSOUND))
{ if(SoundCardExists()&&FileExists(Global.SoundFile))
PlaySound(Global.SoundFile, NULL, SND_ASYNC|SND_FILENAME);
}
}
//显示OA新信息
void __fastcall TMainForm::mf_Notify(CUdpPackage Pack)
{
if(int(sizeof(COANotify) * Pack.Header.RowCount) != Global.CurrentUsedSize) return;
TTreeNode *tn,*tn1,*tn2,*tnToDo,*tnNotice;
bool bCreated;
COANotify *Notify = (COANotify*)malloc(sizeof(COANotify) * Pack.Header.RowCount);
Global.GetFromBuff(Notify);
tv_Watch->Items->Clear();
tnToDo = tv_Watch->Items->Add(NULL,"待办事宜");
tnToDo->ImageIndex = 0;
tnToDo->SelectedIndex = 0;
tnNotice = tv_Watch->Items->Add(NULL,"提示栏");
tnNotice->ImageIndex = 1;
tnNotice->SelectedIndex = 1;
//m_SetNotify(Notify,Pack.Header.RowCount);
for(int i=0;i<Pack.Header.RowCount;i++)
{ if(Notify[i].Type == 1) tn = tnToDo;
if(Notify[i].Type == 2) tn = tnNotice;
//此模块是否已建立
bCreated = false;
for(int j=0;j<tv_Watch->Items->Count;j++)
{ if(tv_Watch->Items->Item[j]->Level == 1 && tv_Watch->Items->Item[j]->Text == Notify[i].GetModule())
{ tn = tv_Watch->Items->Item[j];
bCreated = true;
break;
}
}
if(!bCreated)
{ tn = tv_Watch->Items->AddChild(tn,Notify[i].GetModule());
tn->ImageIndex = tn->Level + 1;
tn->SelectedIndex = tn->Level + 1;
}
//子结点
if(Notify[i].Hierarchy == 0)
{ tn1 = tv_Watch->Items->AddChild(tn,Notify[i].Title);
tn1->ImageIndex = tn1->Level + 1;
tn1->SelectedIndex = tn1->Level + 1;
}
if(Notify[i].Hierarchy == 1)
{ tn2 = tv_Watch->Items->AddChild(tn1,Notify[i].Title);
tn2->ImageIndex = tn2->Level + 1;
tn2->SelectedIndex = tn2->Level + 1;
}
}
free(Notify);
//检查是否与已有内容相同
bool bNewNotify = false;
if(tv_Main->Items->Count != tv_Watch->Items->Count)
bNewNotify = true;
else
{ for(int i=0;i<tv_Main->Items->Count;i++)
{ if(tv_Main->Items->Item[i]->Text != tv_Watch->Items->Item[i]->Text)
{ bNewNotify = true;
break;
}
}
}
if(bNewNotify)
{ tv_Main->Items->Assign(tv_Watch->Items);
tv_Main->FullExpand();
mf_Notify();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -