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

📄 clientdlg.cpp

📁 网吧管理系统VC源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ClientDlg.cpp : implementation file
//
#include "Windows.h"
#include "winuser.h"

#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "ReceiveSocket.h"
#include "SendMsg.h"


#include "AboutDilg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define LLKHF_ALTDOWN      0x00000020
#define WH_KEYBOARD_LL     13

typedef struct tagKBDLLHOOKSTRUCT {
    DWORD   vkCode;
    DWORD   scanCode;
    DWORD   flags;
    DWORD   time;
    DWORD   dwExtraInfo;
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

LRESULT  CALLBACK  LowLevelKeyboardProc(int  nCode,  WPARAM  wParam,  LPARAM  lParam);

static HHOOK glhHook;

/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog

CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClientDlg)
	m_CardPassWord = _T("");
	m_CardID = _T("");
	m_AdminPassWord = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	hmenu.LoadMenu(IDM_CONTEXTMAIN);
}

CClientDlg::~CClientDlg()
{
	if(m_pReceiveSocket != NULL)
		delete m_pReceiveSocket;
}

void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDlg)
	DDX_Control(pDX, IDC_EDIT_CARD_NUM, m_CtrlCardNum);
	DDX_Text(pDX, IDC_EDIT_CARD_PWD, m_CardPassWord);
	DDV_MaxChars(pDX, m_CardPassWord, 12);
	DDX_Text(pDX, IDC_EDIT_CARD_NUM, m_CardID);
	DDV_MaxChars(pDX, m_CardID, 12);
	DDX_Text(pDX, IDC_EDIT_ADMIN_PWD, m_AdminPassWord);
	DDV_MaxChars(pDX, m_AdminPassWord, 12);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
	//{{AFX_MSG_MAP(CClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TURN_ON, OnCardButtonTurnOn)
	ON_MESSAGE(WM_ACCEPT_SERVER_ADDR, OnAcceptServerAddr)
	ON_MESSAGE(WM_START_WORK, OnStartWork)
    ON_MESSAGE(WM_LOCK, OnLockWindow)
	ON_MESSAGE(WM_LEAGUER_CHECK_OUT,OnLeaguerCheckOut)
	ON_MESSAGE(WM_LEAGUER_INFORMATION,OnDisplayLeaguerInformation)
	ON_MESSAGE(WM_NOT_CHECK_OUT,OnNotCheckOut)
	ON_MESSAGE(WM_TIME_REPORT,OnTimeReport)
	ON_MESSAGE(WM_RESET, OnReSet)
	ON_MESSAGE(WM_SHUTDOWNCOMPUTER,OnShutDown)
    ON_MESSAGE(WM_DISPLAY_MESSAGE,OnDisplayMessage)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_UNLOCK, OnAdminButtonUnlock)
	ON_WM_CLOSE()
	ON_MESSAGE(WM_SYSTRAY,OnSysTray)
	//}}AFX_MSG_MAP
	//ON_MESSAGE(WM_SYSTRAY,OnSysTray)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

BOOL CClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	//glhHook=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)LowLevelKeyboardProc,(HINSTANCE)AfxGetApp()->m_hInstance,0);

	m_pReceiveSocket = new CReceiveSocket();

	PHOSTENT hostinfo;
	CString strLocalip;
	char name[256];
	//取得服务器端机器名
	//
	gethostname(name, sizeof(name));
	hostinfo = gethostbyname(name);
	//由机器名转换为IP地址
	strLocalip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
	if(!m_pReceiveSocket->Create(6830,SOCK_DGRAM, strLocalip)) //建立用于接受的CSocket
	{
		AfxMessageBox("网络连接错误!", MB_ICONINFORMATION | MB_OK);
		delete m_pReceiveSocket;
		m_pReceiveSocket = NULL;
		return FALSE;
	}	
	CFileStatus filestatus;
	//从ServerIP.txt文件中读取服务器IP
	if(CFile::GetStatus("ServerIp.txt",filestatus)) 
	{
		CFile SFile("ServerIp.txt",CFile::modeRead);
		SFile.SeekToBegin();
		char str[15];
		int Length = SFile.Read(str,15);
		str[Length] = '\0';
		strServerIp = CString(str);

		//连接服务器
		m_commSocket.Create();
		m_commSocket.Connect(strServerIp, 6810); 

		SMessage m_Msg;
	    if (!IsWIN32_NT()) 
		{
			DWORD length = 256;
			char* strName = new char[length];
			GetComputerName(strName, &length);
			strcpy(m_Msg.msg, strName);
		}
		else
			gethostname(m_Msg.msg, 255);
		
		m_Msg.code = CLIENT_IP_SEND;
		m_commSocket.Send(&m_Msg, sizeof(SMessage));
	}

	SetTimer(4,5000,NULL); //20秒

	LockWindow();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	CDialog::OnSysCommand(nID, lParam);
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CClientDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}


HCURSOR CClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//会员从客户端登陆上网
void CClientDlg::OnCardButtonTurnOn() 
{
	UpdateData(true);

	this->m_CardID.TrimLeft();
	this->m_CardID.TrimRight();

	this->m_CardPassWord.TrimLeft();
	this->m_CardPassWord.TrimRight();

	if(!(m_CardID == "" || m_CardPassWord == "" || m_CardPassWord.GetLength() < 5))
	{
		//撤消钩子函数
		::UnhookWindowsHookEx(glhHook);

		SMessage pMsg;
		if (!IsWIN32_NT()) 
		{
			DWORD length = 256;
			char* strName = new char[length];
			GetComputerName(strName, &length);
			strcpy(pMsg.msg, strName);
		}
		else
			gethostname(pMsg.msg, 255);
		
		strcpy(pMsg.cardID, this->m_CardID);
		strcpy(pMsg.cardPass, EncryptText(this->m_CardPassWord));
	
		strLeaguerCard = this->m_CardID;
	
		this->m_CardID.Empty();
		this->m_CardPassWord.Empty();

		UpdateData(false);
		this->m_CtrlCardNum.SetFocus();
		pMsg.code = CLIENT_CARD_PASS;
		m_commSocket.Send(&pMsg, sizeof(SMessage));
	}
	else
		AfxMessageBox("会员卡号或密码不能为空!");
}


void CClientDlg::OnSysTray(WPARAM wParam,LPARAM lParam)
{
	CPoint pt;
	CAboutDilg Dlg;
	DWORD length = 256;
	char* strName = new char[length];
	switch (lParam)
	{
		case WM_RBUTTONUP:
		{	
			GetCursorPos(&pt);

			HMENU hpopup = GetSubMenu(hmenu, 0);
			//弹出菜单
			switch (TrackPopupMenu(hpopup,           
				TPM_RETURNCMD |    
				TPM_RIGHTBUTTON,   
				pt.x, pt.y,        
				0,                
				m_hWnd,          
				NULL))            
			{
			case ID_APP_EXIT: 
				PostQuitMessage(0);
				Shell_NotifyIcon(NIM_DELETE, &pnid);
				break;

			//关于
			case ID_APP_ABOUT:
				Dlg.DoModal();
				break;
			
			//会员结账
			case ID_CHECK_OUT:
				if (MessageBox("是否真的要结账吗?", "提示", MB_YESNO|MB_ICONQUESTION) == IDYES)
				{
					SMessage pMsg;
					if (!IsWIN32_NT()) 
					{
						GetComputerName(strName, &length);
						strcpy(pMsg.msg, strName);
					}
					else
						gethostname(pMsg.msg, 255);

					pMsg.code = LEAGUER_CHECK_OUT;
					m_commSocket.Send(&pMsg, sizeof(SMessage));		
					SetTimer(1,5000,NULL);
				}
				break;

			//显示使用信息
			case ID_DISPLAYTIME:
				SMessage pMsg;
				if (!IsWIN32_NT()) 
				{
					GetComputerName(strName, &length);
					strcpy(pMsg.msg, strName);
				}
				else
					gethostname(pMsg.msg, 255);		
				pMsg.code = CLIENT_TIME_REPORT;
				m_commSocket.Send(&pMsg, sizeof(SMessage));
				break;

			//显示会员信息
			case ID_LEAGUER_INFORMATION:
			    if (!IsWIN32_NT()) 
				{
					GetComputerName(strName, &length);
					strcpy(pMsg.msg, strName);
				}
				else
					gethostname(pMsg.msg, 255);
				
				pMsg.code = LEAGUER_INFORMATION;
				m_commSocket.Send(&pMsg, sizeof(SMessage));
				break;

			//给服务器发送信息
			case ID_SEND_MESSAGE:
				CSendMsg dlg;
				if(dlg.DoModal() == IDOK)
				{
					SMessage pMsg;
					
				    if (!IsWIN32_NT()) 
					{
						GetComputerName(strName, &length);
						strcpy(pMsg.msg, strName);
					}
					else
						gethostname(pMsg.msg, 255);

					if(dlg.m_strMsg != "")
					{
						strcpy(pMsg.msgSend, dlg.m_strMsg);
						pMsg.code = CLIENT_SEND_MESSAGE;
						m_commSocket.Send(&pMsg, sizeof(SMessage));
					}
					else
					{
						AfxMessageBox("不能发送空消息!", MB_ICONINFORMATION | MB_OK);
					}
				}
				break;
			}
		}
	}
}


//接收服务器的广播信息
LRESULT CClientDlg::OnAcceptServerAddr(WPARAM wParam, LPARAM lParam)
{
	SMessage* pMsg = (SMessage*)lParam;
	
	CFileStatus filestatus;

	strServerIp = CString(pMsg->msg);
	int Length = strServerIp.GetLength();

	SMessage m_Msg;
	if (!IsWIN32_NT()) 
	{
		DWORD length = 256;
		char* strName = new char[length];
		GetComputerName(strName, &length);
		strcpy(m_Msg.msg, strName);
	}
	else
		gethostname(m_Msg.msg, 255);

	
	//新建文件,将服务器IP写入文件,然后连接服务器
	CFile SFile("ServerIp.txt",CFile::modeCreate);
	SFile.Close();
	CFile DFile("ServerIp.txt",CFile::modeWrite);
	DFile.Write(strServerIp,Length);

⌨️ 快捷键说明

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