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

📄 filesvw.cpp

📁 API经典入门
💻 CPP
字号:
// filesvw.cpp : implementation file
//

#include "stdafx.h"
#include "wingrep.h"
#include "filesvw.h"
#include "wingrdoc.h"
#include "malloc.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFilesView

IMPLEMENT_DYNCREATE(CFilesView, CScrollView)

CFilesView::CFilesView()
{
}

CFilesView::~CFilesView()
{
}


BEGIN_MESSAGE_MAP(CFilesView, CScrollView)
	//{{AFX_MSG_MAP(CFilesView)
	ON_WM_LBUTTONDOWN()
	ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CFilesView drawing

void CFilesView::OnInitialUpdate()
{
	// Make this window into a drop target
	// for File Manager files.
	DragAcceptFiles(TRUE);
	
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

void CFilesView::OnDraw(CDC* pDC)
{
	CWingrepDoc* pDoc = (CWingrepDoc*)GetDocument();
	POSITION pos = pDoc->m_lstFiles.GetHeadPosition();
	
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	int nCharHeight = tm.tmHeight + tm.tmExternalLeading;
	int nCharWidth = tm.tmAveCharWidth;
	int x = nCharWidth*LEFT_MARGIN;
	int y = nCharHeight*TOP_MARGIN;
	
	if (pos == NULL)
		pDC->TextOut(x, y, "No files are selected.");

	while (pos != NULL)
	{
		pDC->TextOut(x, y, pDoc->m_lstFiles.GetNext(pos));
		y += nCharHeight;
	}
	
}

/////////////////////////////////////////////////////////////////////////////
// CFilesView message handlers 

BOOL CFilesView::HitTestFiles (CPoint point, CString& strHit)
{
	CWingrepDoc* pDoc = (CWingrepDoc*)GetDocument();
	ASSERT_VALID(pDoc);
	
	// Construct and initialize a device context based 
	// upon the view.
	CClientDC dc(this);
	OnPrepareDC(&dc);
		
	// Calculate the character extents.
	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	int nCharHeight = tm.tmHeight + tm.tmExternalLeading;
	int nCharWidth = tm.tmAveCharWidth;

	int index;
	// Convert device coordinates (point) into
	// character coordinates.
	index = point.y/nCharHeight - TOP_MARGIN;

	if ( index < 0 )
		return FALSE;

	// Translate the character index into a list position.
	POSITION pos = pDoc->m_lstFiles.FindIndex(index);
	
	if (pos == NULL)
		return FALSE;
	
	// Return the file name that was hit via
	// the strHit variable.
	strHit = pDoc->m_lstFiles.GetAt(pos);
	
	return TRUE;
}

void CFilesView::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWingrepDoc* pDoc = (CWingrepDoc*)GetDocument();
	ASSERT_VALID(pDoc);

	// Get the file name that was clicked on.  If no
	// file name was clicked on then there is nothing
	// to do!
	CString strFilename;
	if (!HitTestFiles(point, strFilename))
		return;
		
	int nLen = strFilename.GetLength() + 1;
	
	// create a storage structure and fill it
	// with data (a file name).
	STGMEDIUM stgMedium;
	stgMedium.tymed = TYMED_HGLOBAL;
	stgMedium.hGlobal = GlobalAlloc(GHND, nLen);
	BYTE FAR* lpText = (BYTE FAR*)GlobalLock(stgMedium.hGlobal);
	if (lpText == NULL)
		TRACE0("OnLButtonDown : GlobalLock failed.\n");
	_fmemcpy(lpText, (const char*)strFilename, nLen);
	TRACE1("Global Text is %Fs.\n", (char FAR*)lpText);
	GlobalUnlock(stgMedium.hGlobal);
	stgMedium.pUnkForRelease = NULL;
	
	// Transfer the storage structure into the
	// data source's cache.
	m_dataSource.CacheData(CF_TEXT, &stgMedium, 
			&pDoc->m_fetcTransferProtocol);
    
	/*
	// Alternate method of sending a global memory block.
	HGLOBAL hgData;
	hgData = GlobalAlloc(GHND, nLen);
	char FAR* lpText = (char FAR*)GlobalLock(hgData);
	_fmemcpy(lpText, (const char*)strFilename, nLen);
	TRACE1("Global Text is %Fs.\n", (char FAR*)lpText);
	//TRACE1("Global Text is %Fs.\n", lpFilename);
	GlobalUnlock(hgData);
	DWORD dwSize = GlobalSize(hgData);
	TRACE1("Size of global memory block = %ld.\n",dwSize);
	m_dataSource.CacheGlobalData(CF_TEXT, hgData, 
			&pDoc->m_fetcTransferProtocol);
	*/
	
	if ( m_dataSource.DoDragDrop(DROPEFFECT_COPY) 
			== DROPEFFECT_NONE )
	{
		TRACE0("DoDragDrop was incomplete.\n");
		m_dataSource.Empty();
	}
	else
	{
		TRACE0("DoDragDrop completed a transfer.\n");
	}
	
	//CScrollView::OnLButtonDown(nFlags, point);
}

void CFilesView::OnDropFiles(HDROP hDropInfo)
{
	CWingrepDoc* pDoc = (CWingrepDoc*)GetDocument();
	ASSERT_VALID(pDoc);

	// Activate this view window.
	SetActiveWindow();
	
	// Determine how many files are being dragged.
	UINT nFiles = ::DragQueryFile(hDropInfo, -1, NULL, 0);
	
	char strFilename[_MAX_PATH];
	CString S;
	for (UINT i = 0; i < nFiles; i++)
	{
		::DragQueryFile(hDropInfo, i, strFilename, _MAX_PATH);
		S = strFilename;
		pDoc->m_lstFiles.AddTail(S);
	}
	
	// Tell Windows to clean up after the File
	// Manager drag/drop event.
	::DragFinish(hDropInfo);
	
	// Tell the view(s) to refresh.
	pDoc->UpdateAllViews(NULL);
	
	// Don't call the base class's OnDropFiles.  We don't
	// want these files "Open"ed in this application.  We
	// just want the file names themselves.
}

/*
// The previous function was based upon ...
void CFrameWnd::OnDropFiles(HDROP hDropInfo)
{
	SetActiveWindow();      // activate us first !
	UINT nFiles = ::DragQueryFile(hDropInfo, -1, NULL, 0);

	for (UINT iFile = 0; iFile < nFiles; iFile++)
	{
		char szFileName[_MAX_PATH];
		::DragQueryFile(hDropInfo, iFile, szFileName, _MAX_PATH);
		AfxGetApp()->OpenDocumentFile(szFileName);
	}
	::DragFinish(hDropInfo);
}
*/

⌨️ 快捷键说明

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