// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "LYFZIPManage.h"
#include "resource.h"
#include "MainFrm.h"
#include "Psw.h"
#include "TakeFrom.h"
#include "ClientIPForm.h"
#include "LogForm.h"
#include <Mmsystem.h>

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

#define IPC_CUSTOM_MSG2	_T("{a4Fa76E2-w78F-2aD5-q98A-Y0B0D078u2}")
UINT g_wmClose = RegisterWindowMessage(IPC_CUSTOM_MSG2);

#define WM_ICON_NOTIFY  WM_USER+12
extern HWND g_hConnectWnd;
CRect g_rc;
CMainFrame *g_pMainWnd=NULL;
HWND g_hSendWnd=NULL;
extern CString newGUID();
BOOL g_bNeedCoon=1;
BOOL g_bAutoRun=0;
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
/////////////////////////////////////////////////////////////////////////////
float m_WidthScale;
float m_HeightScale;
BOOL g_bWork = 0;

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
	CRect rtCtrl,rtWnd;	
	if(hwnd)
	{
		::GetWindowRect(hwnd,&rtCtrl);
		::GetWindowRect(GetParent(hwnd),&rtWnd);
		rtCtrl.OffsetRect(-rtWnd.left,-rtWnd.top);
		float temp;
		temp= (float)rtCtrl.left*m_WidthScale;
		rtCtrl.left  = (int)temp;
		temp= (float)rtCtrl.top*m_HeightScale;
		rtCtrl.top =  (int)temp;
		temp = (float)rtCtrl.right*m_WidthScale;
		rtCtrl.right = (int)temp;
		temp =(float)rtCtrl.bottom*m_HeightScale;
		rtCtrl.bottom  =(int)temp;
		::MoveWindow(hwnd,rtCtrl.left,rtCtrl.top,rtCtrl.Width(),rtCtrl.Height(),TRUE);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
IMPLEMENT_DYNCREATE(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	ON_WM_TIMER()
	ON_COMMAND(IDM_TODAY, OnToday)
	ON_COMMAND(IDM_DINDAN, OnDindan)
	ON_COMMAND(IDM_MSGREG, OnMsgreg)
	ON_COMMAND(IDM_EXIT, OnExit)
	ON_COMMAND(IDM_OPEN, OnOpen)
	ON_COMMAND(IDM_AUTORUN, OnAutorun)
	ON_COMMAND(IDM_CLIENTIP, OnClientip)
	ON_COMMAND(IDM_LOG, OnLog)
	ON_WM_NCLBUTTONDBLCLK()
	ON_WM_NCHITTEST()
	ON_COMMAND(IDM_CLIENTIP2, OnClientip2)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
	ON_REGISTERED_MESSAGE(g_wmClose, OnAbortClose)
END_MESSAGE_MAP()
static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

LRESULT CMainFrame::OnAbortClose(WPARAM wParam, LPARAM lParam)
{ 
	if(g_bWork)return 0;

	m_TrayIcon.RemoveIcon();
	if(m_Mdi.MainClose())
		CMDIFrameWnd::OnClose(); 
	return 1;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

#if defined(VC70) || defined(VC60)
UINT CMainFrame::OnNcHitTest(CPoint point)
{
	if (CWnd::OnNcHitTest(point) == HTRIGHT || CWnd::OnNcHitTest(point) == HTLEFT || CWnd::OnNcHitTest(point) == HTTOP || CWnd::OnNcHitTest(point) == HTBOTTOM)
		return HTCLIENT;
	return CWnd::OnNcHitTest(point);
}
#else
LRESULT CMainFrame::OnNcHitTest(CPoint point)
{
	if (CWnd::OnNcHitTest(point) == HTRIGHT || CWnd::OnNcHitTest(point) == HTLEFT || CWnd::OnNcHitTest(point) == HTTOP || CWnd::OnNcHitTest(point) == HTBOTTOM)
		return HTCLIENT;
	return CWnd::OnNcHitTest(point);
}
#endif

void CMainFrame::OnNcLButtonDblClk(UINT nFlags, CPoint point)
{
	if(nFlags != HTCAPTION) 
		CMDIFrameWnd::OnNcLButtonDblClk(nFlags, point); 
}

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	CString version = _T ("Version 1.0");
	g_bAutoRun=AfxGetApp()->GetProfileInt (version, "brun", 0);

}

CMainFrame::~CMainFrame()
{
}
void LoadImageFromFile(Bitmap **img, CString path)
{
	try
	{
		if(!::PathFileExists (path))return;
		if(*img)delete *img;
		*img=NULL;
		BSTR bstr= path.AllocSysString();   
		*img= Bitmap::FromFile(bstr);
		SysFreeString(bstr);
	}
	catch(...)
	{
	}
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if(!m_wndClient.SubclassWindow (m_hWndMDIClient))
	{
		return -1;
	}

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	m_wndToolBar.LoadTrueColorToolBar(90, IDB_NORMAL, 0, IDB_DISABLE);

	// second toolbar must have different ID
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	::GetWindowRect (this->m_hWndMDIClient, g_rc);
	g_pMainWnd=this;


	m_WidthScale = (float)g_rc.Width ()/722;
	m_HeightScale = (float)g_rc.Height ()/533;

	SetTimer(1, 5*60000, NULL);
	m_TrayIcon.Create(this,WM_ICON_NOTIFY,"����������" ,m_hIcon,IDR_MENUtray); //����
	OnClose();  

	g_pMainWnd->SetTimer (3, 20000, NULL);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.style&=~WS_MAXIMIZEBOX; 
	cs.style&=~WS_THICKFRAME; 



	RECT   rect;   
	HWND   hWnd   =   ::FindWindow("Shell_TrayWnd",   0);
	::GetWindowRect(hWnd,&rect);


	cs.x=0;
	cs.y=0;
	cs.cx=GetSystemMetrics(SM_CXSCREEN);
	cs.cy=GetSystemMetrics(SM_CYSCREEN)-(rect.bottom -rect.top)+3; 


	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnDestroy() 
{

	m_Mdi.DeleteAll();
	CMDIFrameWnd::OnDestroy();
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default


	ShowWindow(SW_HIDE); //���ش���
}

LRESULT CMainFrame::OnTrayNotification(WPARAM wParam,LPARAM lParam)
{
	return m_TrayIcon.OnTrayNotification(wParam,lParam);
}

///////////////////////////////////////////////////////
//���ٴ���
#define OPEN_MENU(x) m_Mdi.Append(pName, RUNTIME_CLASS(x), IDR_MAINFRAME, this, nID)
//���ٴ���
///////////////////////////////////////////////////////////////////////////////
DWORD CMainFrame::GetConnectionID()
{
	return m_dwConnectionID;
}

void CMainFrame::SetConnectionID(DWORD dwConnection)
{
	m_dwConnectionID = dwConnection;
}

void CMainFrame::ProcessChatMessageRequest2(BYTE code)
{
	
}

void CMainFrame::ProcessChatMessageRequest2(CString sql)
{

}
void CMainFrame::ProcessChatMessageRequest2(BYTE *pData, int length)
{

}

BOOL CMainFrame::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
{
	return 1;
}


//ACEESS+TCPIPʵ�������

BOOL CMainFrame::OnConnect()
{
	return 1;
}
void CMainFrame::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
{
	///////
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
	}
	else if(nIDEvent==3)
	{
		if(g_bWork)return;
		KillTimer(3);   
		g_pMainWnd->SetTimer (3, 20000, NULL);
	}
	else if(nIDEvent==4)
	{
		//	m_Mdi.CloseAll();
	}
}


void CMainFrame::ProcessLoginResponse(void *pLoginResult)
{
}

BOOL CMainFrame::ProcessLoginRequest()
{
	return 1;
}

void CMainFrame::OnOutbarNotify(CString strMsg)
{
}

void CMainFrame::AutoUpdate()
{
}

BOOL g_bInSkin=0;
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_F3:
			ChangePsw();
			break;

		}
	}
	return CMDIFrameWnd::PreTranslateMessage(pMsg);
}

void CMainFrame::GetSkin()
{ 
}


void CMainFrame::PrintDinDan(CString m_id)
{
}

void CMainFrame::SetSelFolder(int pos)
{

}

void CMainFrame::ToPhotoPrint()
{
}

void CMainFrame::GetOcx()
{
}

CArray<CStringArray, CStringArray>g_List1array;
DWORD g_nLeng=0;
BYTE *g_pData=NULL;
void CMainFrame::ProcessChatMessageResponse(void *pResponse)
{

}


void CMainFrame::AddHistoryMenu(CArray<CStringArray, CStringArray>*hisyeararray)
{

}

void CMainFrame::OnHistory(UINT nID)
{
}
void CMainFrame::PrintDinDan2(CString m_id)
{
}
void CMainFrame::OnToday() 
{
	// TODO: Add your command handler code here
	// TODO: Add your command handler code here
	UINT nID=1;
	LPSTR pName ;
	pName = (LPSTR)(LPCSTR)"�ͻ�����";
	OPEN_MENU(TakeFrom);
}

void CMainFrame::OnDindan() 
{
	UINT nID=1;
	LPSTR pName ;
	pName = (LPSTR)(LPCSTR)"�ҵ��˻�";
	
}

void CMainFrame::ChangePsw()
{
	Psw dlg;
	dlg.DoModal ();
}

void CMainFrame::OnMsgreg() 
{
	// TODO: Add your command handler code here
	UINT nID=1;
	LPSTR pName ;
	pName = (LPSTR)(LPCSTR)"���ͼ�¼";
	//OPEN_MENU(DesignFrom);
}
extern DWORD FindAppProcessID(CString path);
void CMainFrame::OnExit() 
{
	// TODO: Add your command handler code here
	m_TrayIcon.RemoveIcon();
	if(m_Mdi.MainClose())
		CMDIFrameWnd::OnClose();	
	DWORD id=FindAppProcessID("AutoRun3.exe");
	if(id!=-1)
	{
		HANDLE ProcessHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id);
		if(ProcessHandle)TerminateProcess(ProcessHandle,0);
	}
	// exit(-1);
}

void CMainFrame::OnOpen() 
{
	// TODO: Add your command handler code here
	ShowWindow(SW_SHOW); 
}

void CMainFrame::OnAutorun() 
{
	// TODO: Add your command handler code here
	g_bAutoRun=!g_bAutoRun;
	CString version = _T ("Version 1.0");
	AfxGetApp()->WriteProfileInt (version, "brun", g_bAutoRun);	
	TCHAR szKey[MAX_PATH];
	HKEY hKey=0; 
	DWORD disp=0;
	lstrcpy(szKey,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 
	if(g_bAutoRun)
	{
		int lResult=RegCreateKeyEx(HKEY_LOCAL_MACHINE,szKey,0,NULL,REG_OPTION_VOLATILE, KEY_ALL_ACCESS,NULL,&hKey,&disp); 
		if(lResult==ERROR_SUCCESS)
		{ 
			TCHAR szDir[MAX_PATH];
			::GetModuleFileName (NULL, szDir, MAX_PATH);
			lResult=RegSetValueEx(hKey,"YLGLMSGMANAGE",0,REG_SZ,(const unsigned char*)szDir,lstrlen(szDir)); 
			RegCloseKey(hKey); 
		}
	}
	else
	{
		int lResult=RegCreateKeyEx(HKEY_LOCAL_MACHINE,szKey,0,NULL,REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,NULL,&hKey,&disp); 
		if(lResult==ERROR_SUCCESS)
		{
			RegDeleteValue(hKey,"YLGLMSGMANAGE"); 
			RegDeleteKey (hKey,"YLGLMSGMANAGE"); 
			RegCloseKey(hKey); 
		}
	}	
}

void CMainFrame::OnExit2()
{

}

void CMainFrame::OnClientip() 
{
	// TODO: Add your command handler code here
	UINT nID=1;
	LPSTR pName ;
	pName = (LPSTR)(LPCSTR)"��������";
	OPEN_MENU(ClientIPFrom);	
}

void CMainFrame::OnLog() 
{
	// TODO: Add your command handler code here
	UINT nID=1;
	LPSTR pName ;
	pName = (LPSTR)(LPCSTR)"ϵͳ��־";
	OPEN_MENU(LogFrom);	
}

void CMainFrame::OnClientip2() 
{
	// TODO: Add your command handler code here
	UINT nID=1;
	LPSTR pName ;
	pName = (LPSTR)(LPCSTR)"��������2";
	//OPEN_MENU(ClientIPFrom2);	
}