www.pudn.com > seedesk.zip > EmployerSeverDlg.cpp


// EmployerSeverDlg.cpp : implementation file 
// 
/*************************************************************************** 
 *        Module               :                             * 
 *         
 *                                                                         * 
 *        Created by           : ??      Date  : 2004.9.12        * 
 *        Modified by          :                  Date  :                  * 
 *                                                                         *  
 *                   *  
 ***************************************************************************/ 
#include "stdafx.h" 
#include "EmployerSever.h" 
#include "EmployerSeverDlg.h" 
#include "EmployerSocket.h" 
#include "ListenlingSocket.h" 
#include  
#include  
#include  
 
#define WM_MYMOUSEHOVER (WM_USER+1) 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CEmployerSeverDlg dialog 
 
CEmployerSeverDlg::CEmployerSeverDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CEmployerSeverDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CEmployerSeverDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	lpData=NULL; 
	 
	m_pListenlingSocket=NULL; 
	size=0; 
 
} 
 
void CEmployerSeverDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CEmployerSeverDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CEmployerSeverDlg, CDialog) 
	//{{AFX_MSG_MAP(CEmployerSeverDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_CLOSE() 
	ON_COMMAND(ID_START_SEVER, OnStartSever) 
	ON_COMMAND(ID_SHOW_WIN, OnShowWin) 
	ON_BN_CLICKED(IDC_BUTTON1, OnHideWin) 
	ON_BN_CLICKED(IDC_BUTTON2, OnStartSever) 
	ON_WM_TIMER() 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(WM_MYMOUSEHOVER, OnMyMouseHover) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CEmployerSeverDlg message handlers 
 
BOOL CEmployerSeverDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	m_bShowWindow=false;  //初始的显示状态为hide 
    m_bSever=false;       //服务器在开始的时候为不服务状态 
	 
	// TODO: Add extra initialization here 
	HICON h=AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	NOTIFYICONDATA p; 
	p.hIcon=h; 
	p.cbSize=200; 
	p.hWnd=GetSafeHwnd(); 
	strcpy(p.szTip,"Employer server"); 
	p.uCallbackMessage=WM_MYMOUSEHOVER; 
	p.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP; 
	p.uID=IDR_MAINFRAME; 
	::Shell_NotifyIcon(NIM_ADD,&p);//创建托盘图标 
	SetTimer(1,1000,NULL);       //时间函数用于显示时间 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CEmployerSeverDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		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 CEmployerSeverDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		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; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
	if(m_bShowWindow==false) 
   	CDialog::ShowWindow(SW_HIDE);//用于处理窗口的显示和隐藏 
	else 
		ShowWindow(SW_SHOW); 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CEmployerSeverDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
 
 
void CEmployerSeverDlg::OnOK() //创建 侦听的套节字 
{                              //开始侦听 protect类型 只用于内部函数的调用 
	// TODO: Add extra validation here 
	m_pListenlingSocket=new CListenlingSocket(this); 
       m_pListenlingSocket->Create(2502); 
	   if(!m_pListenlingSocket->Listen()) 
	   {  
		   AfxMessageBox("FAILED CREATE LISTEN SOCKET"); 
		   delete m_pListenlingSocket; 
		   m_pListenlingSocket=NULL; 
	   } 
 
	 
	 
} 
 
void CEmployerSeverDlg::CatchScreen() 
{// protect类型 只用于内部函数的调用 
//截获屏幕位图信息和数据信息分别放在btm和lpdata 
	CDC dc; 
	dc.CreateDC("DISPLAY",NULL,NULL,NULL); 
	CBitmap bm; 
	int Width=GetSystemMetrics(SM_CXSCREEN); 
	int Height=GetSystemMetrics(SM_CYSCREEN); 
	bm.CreateCompatibleBitmap(&dc,Width,Height); 
	CDC tdc; 
	tdc.CreateCompatibleDC(&dc); 
	CBitmap*pOld=tdc.SelectObject(&bm); 
	tdc.BitBlt(0,0,Width,Height,&dc,0,0,SRCCOPY); 
	tdc.SelectObject(pOld); 
	 
	bm.GetBitmap(&btm); 
	 size=btm.bmWidthBytes*btm.bmHeight; 
 
	lpData=new char[size];  
///////////////////////////////////////////// 
	BITMAPINFOHEADER bih; 
	bih.biBitCount=btm.bmBitsPixel; 
	bih.biClrImportant=0; 
	bih.biClrUsed=0; 
	bih.biCompression=0; 
	bih.biHeight=btm.bmHeight; 
	bih.biPlanes=1; 
	bih.biSize=sizeof(BITMAPINFOHEADER); 
	bih.biSizeImage=size; 
	bih.biWidth=btm.bmWidth; 
	bih.biXPelsPerMeter=0; 
	bih.biYPelsPerMeter=0; 
/////////////////////////////////// 
	GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS); 
 
} 
 
void CEmployerSeverDlg::SendBITMAP(CEmployerSocket *pSendMapSocket) 
{//发送位图结构信息 
 CatchScreen(); 
				    
  pSendMapSocket->Send(&btm,sizeof(BITMAP),0); 
} 
 
void CEmployerSeverDlg::SendBitData(CEmployerSocket *pSendMapSocket ) 
{  //发送位图数据信息 
	int nBytesSent=0; 
	int nByetsThisTime; 
	char *pch=lpData; 
	do{       //发送大量的数据时 采用循环 直到发送完要发送的数据为止  
	nByetsThisTime=pSendMapSocket->Send(pch,size); 
	nBytesSent+=nByetsThisTime; 
	pch+=nByetsThisTime; 
	}while(nBytesSentAccept(*pClientSocket)) 
			delete pClientSocket; 
} 
 
void CEmployerSeverDlg::OnMyMouseHover(WPARAM wParam, LPARAM lParam) 
{//托盘图标的事件处理 
CMenu pop, *pMenu; 
	UINT uID;//发出该消息的图标的ID 
	UINT uMouseMsg;//鼠标动作 
	POINT pt; 
	uID=(UINT) wParam; 
	uMouseMsg=(UINT) lParam; 
	 
	if(uID == IDR_MAINFRAME){ 
		switch(uMouseMsg) 
		{ 
		case WM_RBUTTONDOWN: 
			//取得鼠标位置 
			GetCursorPos(&pt); 
			pop.LoadMenu(IDR_POPMENU); 
			pMenu = pop.GetSubMenu(0); 
			 
			 
			ClientToScreen(&pt); 
			//设置窗口为最前窗口 
			::SetForegroundWindow(this->GetSafeHwnd());	 
			if(m_bSever==TRUE) 
			pMenu->EnableMenuItem(ID_START_SEVER,MF_GRAYED ); 
			else  
            pMenu->EnableMenuItem(ID_START_SEVER,MF_ENABLED ); 
			pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, 
											this, NULL); 
			ScreenToClient(&pt); 
			::PostMessage( this->GetSafeHwnd(), WM_NULL, 0, 0 ); 
			break; 
		case WM_LBUTTONDOWN: 
			break; 
		case WM_LBUTTONDBLCLK: 
			//ShowWindow(SW_SHOWMAXIMIZED); 
			m_bShowWindow=true; 
			ShowWindow(SW_SHOW); 
			break; 
		default: 
			break; 
		} 
	} 
} 
 
 
 
 
 
void CEmployerSeverDlg::OnClose()  
{ 
	// TODO: Add your message handler code here and/or call default 
		NOTIFYICONDATA tnid; 
	tnid.cbSize=sizeof(NOTIFYICONDATA); 
	tnid.hWnd=this->m_hWnd; 
	//保证删除的是我们的图标 
	tnid.uID=IDR_MAINFRAME; 
 
	Shell_NotifyIcon(NIM_DELETE,&tnid); 
	KillTimer(1); 
	//AfxPostQuitMessage(0); 
	CDialog::OnClose(); 
} 
 
void CEmployerSeverDlg::OnStartSever()  
{  //点击开始服务时候的调用 
	// TODO: Add your command handler code here 
	m_bSever=true; 
	OnOK(); 
	CButton * pbutt=(CButton *)GetDlgItem(IDC_BUTTON2); 
	pbutt->EnableWindow(false); 
} 
 
void CEmployerSeverDlg::OnShowWin()  
{ 
	// TODO: Add your command handler code here 
   //显示窗体 
	m_bShowWindow=true; 
   ShowWindow(SW_SHOW); 
    
} 
 
void CEmployerSeverDlg::OnHideWin()  
{ 
	// TODO: Add your control notification handler code here 
	//隐藏窗体 
		m_bShowWindow=false; 
   ShowWindow(SW_HIDE); 
} 
 
 
 
 
 
void CEmployerSeverDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
  //显示系统现在的时间 
	//载自msdn 
 
	char tmpbuf[128]; 
   _tzset(); 
    _strtime( tmpbuf ); 
    printf( "\t\t\t\t%s\n", tmpbuf ); 
	CStatic* timeshow=(CStatic* )GetDlgItem(IDC_STATIC1); 
	  
   CString	strTime="现在时间:"; 
    strTime+=tmpbuf; 
	timeshow->SetWindowText(strTime); 
 
	CDialog::OnTimer(nIDEvent); 
}