📄 front.cpp
字号:
// front.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "front.h"
#include "frontDlg.h"
#include "InputBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFrontApp
BEGIN_MESSAGE_MAP(CFrontApp, CWinApp)
//{{AFX_MSG_MAP(CFrontApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFrontApp construction
CFrontApp::CFrontApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CFrontApp object
CFrontApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CFrontApp initialization
BOOL CFrontApp::InitInstance()
{
theApp.iTrayChange=0;
theApp.bTrayChange=false;
if (!FirstInstance())
return FALSE; //已经有实例存在了,退出
sServerAddr=ReadRegedit();
if(lstrlen(sServerAddr.GetBuffer(30))==0)
{
AfxMessageBox("IP 格式不对,请在设置中修改!");
}
// if(Login(1)==false)return false;
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CFrontDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
BOOL CFrontApp::FirstInstance()
{
CWnd *pWndPrev, *pWndChild;
//根据主窗口类名和主窗口名判断是否已经有实例存在了
if (pWndPrev = CWnd::FindWindow(NULL,"窗口"))
{//如果存在就将其激活,并显示出来
pWndChild = pWndPrev->GetLastActivePopup();
//如果是最小化的就还原窗口
if (pWndPrev->IsIconic())
pWndPrev->ShowWindow(SW_RESTORE);
pWndChild->SetForegroundWindow();
return FALSE;
}
else
return TRUE;
}
CString CFrontApp::ReadRegedit()
{
CString sResult;
HKEY hKEY;
CString data_Set="Software\\开放办办公系统";
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, &hKEY));
if(ret0!=ERROR_SUCCESS)
{
AfxMessageBox("请输入后前程序地址,否则无法发送数据!");
return "";
}
DWORD type_2=REG_SZ; DWORD cbData_2=100;
long ret2=RegQueryValueEx(hKEY, "前台服务程序IP地址", NULL,&type_2,(unsigned char *)sResult.GetBuffer(100), &cbData_2);
if(ret2!=ERROR_SUCCESS)
{
RegCloseKey(hKEY);
return "";
}
//检验IP地址是否正确
int len=lstrlen(sResult);
char *temp=sResult.GetBuffer(100);
for(int i=0,j=0;i<len;i++)
if(temp[i]=='.')j++;
if(j!=3)
{
AfxMessageBox("IP地址格式不对!");
RegCloseKey(hKEY);
return "";
}
RegCloseKey(hKEY);
return sResult;
}
bool CFrontApp::Login(int Up_Down)
{
//检验IP地址是否正确
int len=lstrlen(sServerAddr.GetBuffer(100));
char *temp=sServerAddr.GetBuffer(100);
for(int i=0,j=0;i<len;i++)
if(temp[i]=='.')j++;
if(j!=3)
{
AfxMessageBox("IP地址格式不对!");
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=250;
dlgTemp->iHight=120;
dlgTemp->ClientFlag=2;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<设置>>");
dlgTemp->DoModal();
return false;
}
SOCKET SendSocket;
WSADATA wsd;
SOCKADDR_IN server;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
AfxMessageBox("WSAStartup failed!\n");
return false;
}
SendSocket=socket(AF_INET, SOCK_DGRAM, 0);
if (SendSocket == INVALID_SOCKET)
{
AfxMessageBox("socket() failed");
return false;
}
server.sin_family = AF_INET;
server.sin_port = htons(5000);
server.sin_addr.s_addr = inet_addr(sServerAddr);
//产生登陆框
if(Up_Down==1) //上线时产生
{
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=260;
dlgTemp->iHight=100;
dlgTemp->ClientFlag=0;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<登陆框>> 建议24位真彩色以上");
dlgTemp->DoModal();
if(dlgTemp->bIsClickOkButton) //按OK键结束
{
char *password,*windowNo;
windowNo=((CMyLabel *)((CMySheetpage *)dlgTemp->ClientWnd->Wizardcontrol->arPages.GetAt(0))->arLabels.GetAt(3))->cName;
password=((CMyLabel *)((CMySheetpage *)dlgTemp->ClientWnd->Wizardcontrol->arPages.GetAt(0))->arLabels.GetAt(2))->cName;
HostNo=atoi(windowNo);
//验证密码
SOCKET TcpSendSocket;
SOCKADDR_IN Tcpserver;
TcpSendSocket = socket(AF_INET, SOCK_STREAM, 0);
if (TcpSendSocket == INVALID_SOCKET)
{
AfxMessageBox("socket() failed");
return false;
}
Tcpserver.sin_family = AF_INET;
Tcpserver.sin_port = htons(5001);
Tcpserver.sin_addr.s_addr = inet_addr(sServerAddr);
if (connect(TcpSendSocket, (struct sockaddr *)&Tcpserver, sizeof(Tcpserver)) == SOCKET_ERROR)
{
AfxMessageBox("connect() failed");
if(strcmp(password,"@")==0)
{
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=250;
dlgTemp->iHight=120;
dlgTemp->ClientFlag=2;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<设置>>");
dlgTemp->DoModal();
}
return false;
}
CONTROL_INFO *controlinfo=new CONTROL_INFO;
memset(controlinfo,'\0',sizeof(*controlinfo));
sprintf(controlinfo->cSqlstate,"%s%d and password='%s'","select windowName,personName from logintable where windowNo=",HostNo,password);
controlinfo->iTransferForm=0;
send(TcpSendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
recv(TcpSendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
TRANSFER_DATA *transferdata=new TRANSFER_DATA;
COLCONTENT *colContent=new COLCONTENT;
send(TcpSendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
recv(TcpSendSocket,(char *)transferdata,sizeof(*transferdata),0);
if(controlinfo->iRecordcount==0||HostNo==0)
{
dlgTemp->bIsClickOkButton=false;
AfxMessageBox("登录失败,请重新进入程序!");
}
else
{
//声明UDP消息
UDP_MSG *msg;
msg=new UDP_MSG;
char *hostname;
hostname=new char[20];
gethostname(hostname,20);
sprintf(msg->flag,"%d",Up_Down);
sprintf(msg->content,"%d$%s$",HostNo,hostname);
sendto(SendSocket,(char *)msg, 300, 0, (SOCKADDR *)&server, sizeof(server));
delete msg;
delete hostname;
//取得用户名
UserName=new char[20];
TranslateMyMsg(transferdata->cMsg[0],2,colContent);
sprintf(UserName,"%s",colContent->content[1]);
}
//结束主线程
controlinfo->iTransferForm=4;
send(TcpSendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
delete controlinfo;
closesocket(TcpSendSocket);
delete colContent;
delete transferdata;
}
if(!dlgTemp->bIsClickOkButton)
{
closesocket(SendSocket);
return false;
}
}
if(Up_Down==0)//下线
{
//声明UDP消息
UDP_MSG *msg;
msg=new UDP_MSG;
char *hostname;
hostname=new char[20];
gethostname(hostname,20);
sprintf(msg->flag,"%d",Up_Down);
sprintf(msg->content,"%d$%s$",HostNo,hostname);
sendto(SendSocket,(char *)msg, 300, 0, (SOCKADDR *)&server, sizeof(server));
delete msg;
delete hostname;
}
closesocket(SendSocket);
return true;
}
void CFrontApp::TranslateMyMsg(char *msg, int colNo,COLCONTENT *colContent)
{
int i,j,k;
for(i=0,j=0;i<colNo;i++)
{
k=0;
while(msg[j]!='$')
{
colContent->content[i][k]=msg[j];
j++;
k++;
}
colContent->content[i][k]='\0';
j++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -