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

📄 httpview.cpp

📁 Visual C++网络通信编程实用案例精逊配套源码 光盘中存放的是书中涉及的所有实例的源代码和经过编译后的应用程序。所有程序均经过测试
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	CListCtrl& list = GetListCtrl();
	list.SortItems( (PFNLVCOMPARE)CompareHitDocs, pNMListView->iSubItem );
	*pResult = 0;
}

void CHttpSvrView::OnGetDispInfo(NMHDR* pNMHDR, LRESULT* pResult)
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
	if ( pDispInfo->item.mask & LVIF_TEXT )
	{
		CHitDoc* pHitDoc = (CHitDoc*)(pDispInfo->item.lParam);
		switch( pDispInfo->item.iSubItem )
		{
		case COLUMN_FILE:
			lstrcpy( pDispInfo->item.pszText, pHitDoc->m_strFile );
			break;
		case COLUMN_PATH:
			lstrcpy( pDispInfo->item.pszText, pHitDoc->m_strFolder );
			break;
		case COLUMN_HITS:
			wsprintf( pDispInfo->item.pszText, "%d", pHitDoc->m_nHits );
			break;
		case COLUMN_LAST:
			if ( pHitDoc->m_nHits > 0 )
				lstrcpy( pDispInfo->item.pszText, (pHitDoc->m_nHits)
					? pHitDoc->m_timeLastHit.Format( IDS_TIMEFORMAT )
					: "" );
			break;
		case COLUMN_URL:
			lstrcpy( pDispInfo->item.pszText, pHitDoc->m_strURL );
			break;
		case COLUMN_CMD:
			lstrcpy( pDispInfo->item.pszText, pHitDoc->m_strCommand );
			break;
		}
	}
	*pResult = 0;
}

void CHttpSvrView::OnDeleteItem(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	CListCtrl& list = GetListCtrl();
	LV_ITEM lvi;
	lvi.mask = LVIF_PARAM;
	lvi.iItem = pNMListView->iItem;
	lvi.iSubItem = 0;
	if ( list.GetItem( &lvi ) == TRUE )
	{
		CHitDoc* pHit = (CHitDoc*)(lvi.lParam);
		delete pHit;
	}
	*pResult = 0;
}

int
CHttpSvrView::GetImage( CHitDoc* pHitDoc )
{
	// indexes into image maps for (status/100)....
	int aStatImg[] = { 2, 5, 5, 4, 3, 3 };
	int iImage;
	if ( pHitDoc->m_nStatus != 0 && pHitDoc->m_nStatus != 200 )
		iImage = aStatImg[ pHitDoc->m_nStatus/100]; // status image
	else if ( pHitDoc->m_dwExecute )
		iImage = 2; // CGI executables
	else if ( pHitDoc->m_nHits == 0 )
		iImage = 0; // no hits yet
	else if ( pHitDoc->m_bFolder )
		iImage = 1; // folder image
	else
		iImage = 0; // document

	return iImage;
}

void CHttpSvrView::RegisterHit( CListCtrl& list, CRequest* pRequest )
{
	CHitDoc* pHitDoc = new CHitDoc( pRequest );
	if ( pHitDoc )
		InsertHitDoc( pHitDoc );
}

void CHttpSvrView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	if ( lHint == HINT_DOCHIT )
	{
		CRequest* pRequest = (CRequest*)pHint;
		CListCtrl& list = GetListCtrl();
		RegisterHit( list, pRequest );
	}
}

void CHttpSvrView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
	// Get the control....
	CListCtrl& list = GetListCtrl();
	int nSelected = list.GetSelectedCount();
	// only proceed if one item selected....
	if ( nSelected == 1 )
	{
		// find the selected item....
		int ndx = 0;
		int nItems = list.GetItemCount();
		while ( ndx < nItems )
		{
			if ( list.GetItemState( ndx, LVIS_SELECTED ) == LVIS_SELECTED )
			{
				LV_ITEM lvi;
				lvi.mask = LVIF_PARAM;
				lvi.iItem = ndx;
				lvi.iSubItem = 0;
				if ( list.GetItem( &lvi ) )
				{
					// only do something for OK and non-executable hits...
					CHitDoc* pHitDoc = (CHitDoc*)(lvi.lParam);
					if ( pHitDoc->m_nStatus == IDS_STATUS_OK &&
						pHitDoc->m_dwExecute == 0 )
					{
						ShellExecute( GetSafeHwnd(), "open",
							pHitDoc->m_strFile, NULL,
							pHitDoc->m_strFolder, SW_SHOW );
					}
				}
				break;
			}
			++ndx;
		}
	}
	*pResult = 0;
}

void CHttpSvrView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
{
	// Get the control....
	CListCtrl& list = GetListCtrl();
	if ( list.GetSelectedCount() == 1 )
	{
		CPoint point;
		if ( GetCursorPos( &point ) )
			DoContextMenu( point );
	}

	*pResult = 0;
}

void CHttpSvrView::DoContextMenu( const CPoint& point )
{
	m_phdPopup = NULL;
	// Get the control....
	CListCtrl& list = GetListCtrl();
	CPoint ptList = point;
	list.ScreenToClient( &ptList );
	int ndx = list.HitTest( ptList );
	if ( ndx != -1 )
	{
		LV_ITEM lvi;
		lvi.mask = LVIF_PARAM;
		lvi.iItem = ndx;
		lvi.iSubItem = 0;
		if ( list.GetItem( &lvi ) )
		{
			CHitDoc* pHitDoc = (CHitDoc*)(lvi.lParam);
			// get the popup menu type index....
			int nType = 0;
			if ( pHitDoc->m_dwExecute )
				nType = 1;
			else if ( pHitDoc->m_nStatus != 200 )
				nType = 2;

			// load the menus....
			CMenu menuPopups;
			if ( menuPopups.LoadMenu( IDM_POPUPS ) )
			{
				// get the popup....
				CMenu* pMenu = menuPopups.GetSubMenu( nType );
				if ( pMenu != NULL )
				{
					m_phdPopup = pHitDoc;
					pMenu->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON,
						point.x, point.y, this );
				}
			}
		}
	}
}

void CHttpSvrView::OnPopupClear()
{
	if ( m_phdPopup != NULL )
	{
		// Get the control....
		CListCtrl& list = GetListCtrl();
		LV_FINDINFO lvfi;
		lvfi.flags = LVFI_PARAM;
		lvfi.lParam = (LPARAM)m_phdPopup;
		int ndx = list.FindItem( &lvfi );
		if ( ndx != -1 )
			list.DeleteItem( ndx );
	}
}

void CHttpSvrView::OnPopupEdit()
{
	if ( m_phdPopup != NULL )
	{
		ShellExecute( GetSafeHwnd(), "edit",
			m_phdPopup->m_strFile, NULL,
			m_phdPopup->m_strFolder, SW_SHOW );
	}
}

void CHttpSvrView::OnPopupOpen()
{
	if ( m_phdPopup != NULL )
	{
		ShellExecute( GetSafeHwnd(), "open",
			m_phdPopup->m_strFile, NULL,
			m_phdPopup->m_strFolder, SW_SHOW );
	}
}

BOOL CHttpSvrView::InsertHitDoc( CHitDoc* pHitDoc )
{
	CListCtrl& list = GetListCtrl();
	// look for a match on the file name....
	LV_FINDINFO lvfi;
	lvfi.flags = LVFI_STRING;
	lvfi.psz = pHitDoc->m_strFile;
	int ndx = list.FindItem( &lvfi );
	while ( ndx != -1 )
	{
		// match found; see if folder matches....
		CString strFolder = list.GetItemText( ndx, COLUMN_PATH );
		if ( strFolder.CompareNoCase( pHitDoc->m_strFolder ) == 0 )
		{
			// matched; get the old HitDoc....
			LV_ITEM lvi;
			lvi.mask = LVIF_PARAM;
			lvi.iItem = ndx;
			lvi.iSubItem = 0;
			if ( list.GetItem( &lvi ) )
			{
				// get the old count
				CHitDoc* pOldHit = (CHitDoc*)(lvi.lParam);
				// add in the previous hits....
				pHitDoc->m_nHits += pOldHit->m_nHits;
				// delete the old hit....
				delete pOldHit;
				// update with new HitDoc....
				lvi.mask = LVIF_IMAGE|LVIF_PARAM;
				lvi.iImage = GetImage( pHitDoc );
				lvi.lParam = (LPARAM)pHitDoc;
				list.SetItem( &lvi );
				// indicate we've made changes....
				list.Update( lvi.iItem );
				break;
			}
		}
		// get next item....
		ndx = list.FindItem( &lvfi, ndx );
	}
	// if we didn't find anything....
	if ( ndx == -1 )
	{
		// didn't find a match; add it to the list....
		LV_ITEM lvi;
		lvi.mask = LVIF_TEXT|LVIF_PARAM|LVIF_IMAGE;
		lvi.iItem = 0;
		lvi.iSubItem = 0;
		lvi.pszText = LPSTR_TEXTCALLBACK;
		lvi.lParam = (LPARAM)pHitDoc;
		lvi.iImage = GetImage( pHitDoc );
		if ( (ndx=list.InsertItem( &lvi )) != -1 )
		{
			// add all the callback sub items....
			lvi.mask = LVIF_TEXT;
			lvi.pszText = LPSTR_TEXTCALLBACK;
			lvi.iItem = ndx;
			for( int subNdx=1; subNdx < C_COLUMNS; ++subNdx )
			{
				lvi.iSubItem = subNdx;
				list.SetItem( &lvi );
			}
		}
		else
		{
			// insert failed; kill it....
			delete pHitDoc;
			pHitDoc = NULL;
		}
	}
	return (pHitDoc != NULL);
}

void CHttpSvrView::OnViewClear()
{
	CListCtrl& list = GetListCtrl();
	list.DeleteAllItems();
}

⌨️ 快捷键说明

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