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

📄 mainfrm.cpp

📁 本程序是VC为平台开发的股票资讯系统
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(GetStockReferView()->CanActivateCopy());	
}

void CMainFrame::OnEditCopys() 
{
	// TODO: Add your command handler code here
	GetStockReferView()->EditCopys();
}

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	
	// TODO: Add your message handler code here
	if(HAS_REG)
	{
		pPopupMenu->DeleteMenu(ID_HELP_INPUTNUM,MF_BYCOMMAND);
		pPopupMenu->DeleteMenu(ID_HELP_REG,MF_BYCOMMAND);
	}
	else if(m_bHasPost)
		pPopupMenu->DeleteMenu(ID_HELP_REG,MF_BYCOMMAND);	
}

BOOL CMainFrame::OpenStockdatDb()
{
	try
	{
		m_StockDb.Open(STOCKDAT_DATABASE,TRUE,FALSE,_T(";PWD=新_NS_???_dat!30讯"));
	}
	catch(CDaoException *ex)
	{
		if(m_StockDb.IsOpen()) m_StockDb.Close();
		ex->Delete();
		
		return FALSE;
	}
	
	return TRUE;
}

CString CMainFrame::GetIndexContent(LPCTSTR lpszIndex)
{
	CSerialNum ser;
	CString content;
	CNewsNouse item(&m_StockDb);
	try
	{
		CString sql;
		sql.Format(_T("SELECT * FROM list1 WHERE col0 = '%s'"),ser.Eencrypt(lpszIndex));
		item.Open(dbOpenDynaset,sql,0);
		if(!item.IsEOF())
			content = ser.Decode(item.m_cut);
		item.Close();
		
		return content;
	}
	catch(CDaoException *ex)
	{
		if(item.IsOpen()) item.Close();
		ex->Delete();
		
		return _T("");
	}
}

BOOL CMainFrame::BeginStockSearch()
{
	CString content;
	CSerialNum ser;

	CNewsdat item(&m_StockDb);
	try
	{
		CString sql,scla;
		scla.Format(_T("%d"),iCategoryIndex[m_iCurItem]);
		sql.Format(_T("SELECT * FROM list0 WHERE col2 = '%s';"),ser.Eencrypt(scla));
		item.Open(dbOpenDynaset,sql,0);
		while(!item.IsEOF())
		{
			m_ThreadCount++;
			if(m_ThreadCount > m_MaxThreadCount)
			{
				m_ToBeURL += item.m_url;
				m_ToBeURL += _T("\n");
			}
			else
			{
				ThreadParams* pThreadParams = new ThreadParams;
				if(!pThreadParams) return FALSE;
				
				if(m_UseProxy)
				{
					pThreadParams->m_Proxy = m_Proxy;
					pThreadParams->m_Port = m_Port;
				}
				pThreadParams->m_TimeOut = m_TimeOut*1000;
				if(m_IsIngoreLong)
					pThreadParams->m_IngoreLegth = m_IngoreLegth*1024;
				else
					pThreadParams->m_IngoreLegth = -1;
				pThreadParams->m_MaxThreadCount = m_MaxThreadCount + 1;

				pThreadParams->m_Key = m_strKey;
				pThreadParams->m_BaseUrl = _T("http://") + ser.Decode(item.m_url);
				pThreadParams->m_StartPos = ser.Decode(item.m_start);
				pThreadParams->m_FinishPos = ser.Decode(item.m_finish);
				pThreadParams->m_LinkFilter = ser.Decode(item.m_link);
				pThreadParams->m_TitleFilter = ser.Decode(item.m_title);
				pThreadParams->m_ClearString = ser.Decode(item.m_more);
				pThreadParams->m_VerifyDate = ser.Decode(item.m_vdate);
				pThreadParams->m_SearchTime = m_Date;
				content = GetIndexContent(ser.Decode(item.m_index));
				if(content.IsEmpty()) 
				{
					item.Close();
					return FALSE;
				}
				pThreadParams->m_CutMore = content;

				CSearchThread *pThread;
				pThread = NULL;
				pThread = new CSearchThread(CSearchThread::ThreadFunc,pThreadParams);
				if (pThread == NULL)
				{
					delete pThreadParams;
					return FALSE;
				}    

				if (!pThread->CreateThread())
				{
					delete pThread;
					pThread = NULL;
					delete pThreadParams;
					return FALSE;
				}    

				pThreadParams->m_threadID = pThread->m_nThreadID;

				m_threadList.AddTail(pThread);
				CString string;
				::InterlockedIncrement(&lThreadCount);
			}
			item.MoveNext();
		}
		item.Close();
	}
	catch(CDaoException *ex)
	{
		if(item.IsOpen()) item.Close();
		ex->Delete();

		return FALSE;
	}
	if(m_ThreadCount > m_MaxThreadCount)
	{
		m_ThreadCount -= m_MaxThreadCount;
		m_ThreadCount++;
	}

	return TRUE;
}

BOOL CMainFrame::AnotherSearch()
{
	CString url;
	int pos;
	pos = m_ToBeURL.Find('\n');
	if(pos != -1)
	{
		url = m_ToBeURL.Left(pos);
		m_ToBeURL = m_ToBeURL.Mid(pos + 1);
		m_ThreadCount--;
	}
	else return FALSE;
	
	CSerialNum ser;
	CNewsdat item(&m_StockDb);
	try
	{
		CString sql,content;
		sql.Format(_T("SELECT * FROM list0 WHERE col0 = '%s';"),url);
		item.Open(dbOpenDynaset,sql,0);
		if(!item.IsEOF())
		{
			ThreadParams* pThreadParams = new ThreadParams;
			if(!pThreadParams) return FALSE;
			
			if(m_UseProxy)
			{
				pThreadParams->m_Proxy = m_Proxy;
				pThreadParams->m_Port = m_Port;
			}
			pThreadParams->m_TimeOut = m_TimeOut*1000;
			if(m_IsIngoreLong)
				pThreadParams->m_IngoreLegth = m_IngoreLegth*1024;
			else
				pThreadParams->m_IngoreLegth = -1;
			pThreadParams->m_MaxThreadCount = m_MaxThreadCount + 1;
			
			pThreadParams->m_Key = m_strKey;
			pThreadParams->m_BaseUrl = _T("http://") + ser.Decode(item.m_url);
			pThreadParams->m_StartPos = ser.Decode(item.m_start);
			pThreadParams->m_FinishPos = ser.Decode(item.m_finish);
			pThreadParams->m_LinkFilter = ser.Decode(item.m_link);
			pThreadParams->m_TitleFilter = ser.Decode(item.m_title);
			pThreadParams->m_ClearString = ser.Decode(item.m_more);
			pThreadParams->m_VerifyDate = ser.Decode(item.m_vdate);
			pThreadParams->m_SearchTime = m_Date;
			content = GetIndexContent(ser.Decode(item.m_index));
			pThreadParams->m_CutMore = content;
			
			CSearchThread *pThread;
			pThread = NULL;
			pThread = new CSearchThread(CSearchThread::ThreadFunc,pThreadParams);
			if (pThread == NULL)
			{
				delete pThreadParams;
				return FALSE;
			}    
			
			if (!pThread->CreateThread())
			{
				delete pThread;
				pThread = NULL;
				delete pThreadParams;
				return FALSE;
			}    
			
			pThreadParams->m_threadID = pThread->m_nThreadID;
			
			m_threadList.AddTail(pThread);
			CString string;
			::InterlockedIncrement(&lThreadCount);
		}
		item.Close();
	}
	catch(CDaoException *ex)
	{
		if(item.IsOpen())
			item.Close();
		ex->Delete();
		return FALSE;
	}
	
	return TRUE;
}

void CMainFrame::AfterStop(BOOL bEndProgram/* = FALSE*/)
{
	m_IsThreadRunning = FALSE;
	m_ThreadCount = 0;
	CString tip,string;
	m_wndAnimate.Stop();
	m_wndAnimate.Seek(0);
	string = _T("连接已经关闭");
	m_wndStatusBar.SetWindowText (string);
	m_wndStatusBar.SetPaneText (1,_T("搜索完成。"),TRUE);
	long m_count;
	
	m_ToBeURL.Empty();
		
	m_count = m_NewsCount;
	string.Format(_T("共搜索到资讯 %ld 条"),m_count);
	m_NewsCount = 0;
	
	m_wndStatusBar.SetPaneText (2,string,TRUE);
	tip.Format(_T("搜索完成。\n\n%s"),string);
	if(!bEndProgram)
		AfxMessageBox(tip,MB_ICONINFORMATION);
	
	m_wndStatusBar.SetWindowText(_T(""));
	m_wndStatusBar.SetPaneText(1,_T(""),TRUE);
	m_wndStatusBar.SetPaneText(2,_T(""),TRUE);
}

LRESULT CMainFrame::OnThreadDone(WPARAM wParam,LPARAM lParam) 
{
	EnterCriticalSection(&m_CritSect);
	
	CString string,tip;
	ThreadParams *lpThreadParams = NULL;
	lpThreadParams = (ThreadParams*)lParam;
	POSITION pos= NULL;

	if(m_ThreadCount > 0) AnotherSearch();

	::InterlockedDecrement(&lThreadCount);
	if(lThreadCount < 0) lThreadCount = 0;
	if(lThreadCount == 0)
	{
		AfterStop();

		LeaveCriticalSection(&m_CritSect);
		return 0;
	}
	else
	{
		int nThreadsLeft = m_threadList.GetCount();

		if(lpThreadParams != NULL )
		{
			for(int i = 0; i < nThreadsLeft; i++)
			{
				if((pos = m_threadList.FindIndex(i))!= NULL )
				{				
					CSearchThread* pThread = m_threadList.GetAt(pos);
					if(pThread != NULL)
					{
						if(lpThreadParams->m_threadID == pThread->m_nThreadID)
						{
							//A thread object is deleted when the last handle to the thread 
							//is closed. 
							m_threadList.RemoveAt(pos);

							delete  lpThreadParams;
	 						if(lThreadCount < m_MaxThreadCount) 
								ReleaseSemaphore(hConnection,1,NULL);
						
							break;
						}
					}
				}
			}
		}
		LeaveCriticalSection(&m_CritSect);
		return 0;
	}
}

LRESULT CMainFrame::OnServerStatus(WPARAM wParam,LPARAM lParam) 
{
	EnterCriticalSection(&m_CritSect);

	LPCTSTR pStatus;
	pStatus = (LPCTSTR)lParam;
	DWORD count = (DWORD)wParam;
    CString Status;
	if(count < 1000)
		Status.Format (_T("线程 %ld:%s"),count,pStatus);
	else
		Status.Format (_T("%s"),pStatus);
	m_wndStatusBar.SetWindowText(Status);

	LeaveCriticalSection(&m_CritSect);

	return 0;
}

LRESULT CMainFrame::OnSendBody(WPARAM wParam,LPARAM lParam) 
{
	EnterCriticalSection(&m_CritSect);

	LPCTSTR pbody;
	pbody = (LPCTSTR)lParam;
    CString body;
    body.Format (_T("%s"),pbody);

	if(GetContainView()->AcceptBody(m_iCurItem,body))
	{
		m_NewsCount++;
		if(!HAS_REG && m_NewsCount >= UNREG_SEARCH_NEWS)
		{
			KillThreads();
			AfterStop(TRUE);

			CString m_strUnreg;
			m_strUnreg.Format(_T("非常抱歉,未注册的软件只能搜索前 %ld 条资讯。"),
				UNREG_SEARCH_NEWS);
			AfxMessageBox(m_strUnreg);

			LeaveCriticalSection(&m_CritSect);
			return 0;
		}

		CString str;
		str.Format(_T("已接收资讯 %ld 条"),m_NewsCount);
		m_wndStatusBar.SetPaneText(2,str,TRUE);
	}
	
	LeaveCriticalSection(&m_CritSect);
	return 0;
}

void CMainFrame::KillThreads()
{
	int nCount,tCount,i;
	int nThreadsLeft;
	POSITION pos= NULL;
	DWORD dwStatus;
	
	m_ThreadCount = 0;
	m_ToBeURL.Empty();
	
	nCount = 0,tCount=0;
	nThreadsLeft = m_threadList.GetCount();
	
	for(i=0; i<nThreadsLeft; i++)
	{
		if((pos = m_threadList.FindIndex(i))!= NULL )
		{				
			CSearchThread *pThread = m_threadList.GetAt(pos);
			if(pThread->m_hThread != NULL)
				::TerminateThread(pThread->m_hThread, 0);
		}
	}
	
	Sleep(200);
	nThreadsLeft = m_threadList.GetCount();
	
	for(i=0; i<nThreadsLeft; i++)
	{
		if((pos = m_threadList.FindIndex(i))!= NULL )
		{				
			CSearchThread *pThread = m_threadList.GetAt(pos);
			if(pThread->m_hThread != NULL)
			{
				tCount++;
				::TerminateThread(pThread->m_hThread, 0);
				::GetExitCodeThread(pThread->m_hThread, &dwStatus);
				if (dwStatus == STILL_ACTIVE)
					nCount++;
				else 
					m_threadList.RemoveAt(pos);
			}
		}
	}
	
	CString string;
	string.Format (_T("搜索停止"),nCount);
	m_wndStatusBar.SetWindowText(string);
	
	if(nCount > 0)
	{
		string.Format (_T("线程数:%d\n正在运行线程数:%d"),tCount,nCount );
		AfxMessageBox(string);
	}
	else
		lThreadCount = 0;
}

void CMainFrame::OnHelpMailto() 
{
	// TODO: Add your command handler code here
	CString path;
	path.Format(_T("%s"),HELP_PATH);
	::HtmlHelp(this->m_hWnd,(LPCSTR)path,HH_HELP_CONTEXT,1000);
}

void CMainFrame::OnHelpIndex() 
{
	// TODO: Add your command handler code here
	CString path;
	path.Format(_T("%s"),HELP_PATH);
	::HtmlHelp(this->m_hWnd,(LPCSTR)path,HH_DISPLAY_TOPIC,0);
}

void CMainFrame::OnHelpUpdate() 
{
	// TODO: Add your command handler code here
	if(m_StockDb.IsOpen()) m_StockDb.Close();
	CStockReferApp *App = (CStockReferApp *)AfxGetApp();
	App->HelpUpdate();
}

void CMainFrame::OnUpdateHelpUpdate(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!m_IsThreadRunning);
}

⌨️ 快捷键说明

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