interfaceview.cpp

来自「接口封装 封装 我的 电动 的哦 得得」· C++ 代码 · 共 252 行

CPP
252
字号
// InterfaceView.cpp : Implementierung der Klasse CInterfaceView
//

#include "stdafx.h"
#include "Interface.h"

#include "InterfaceDoc.h"
#include "InterfaceView.h"
#include <afxadv.h>

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

/////////////////////////////////////////////////////////////////////////////
// CInterfaceView

IMPLEMENT_DYNCREATE(CInterfaceView, CFormView)

BEGIN_MESSAGE_MAP(CInterfaceView, CFormView)
	//{{AFX_MSG_MAP(CInterfaceView)
	ON_COMMAND(ID_FILE_MODELESS, OnModeless)
	ON_NOTIFY(LVN_BEGINDRAG, IDC_LIST1, OnBeginDragList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInterfaceView Konstruktion/Destruktion

CInterfaceView::CInterfaceView()
	: CFormView(CInterfaceView::IDD)
{
	//{{AFX_DATA_INIT(CInterfaceView)
		// HINWEIS: Der Klassenassistent f黦t hier Member-Initialisierung ein
	//}}AFX_DATA_INIT
	m_bInitiated=false;
	m_pModeless=NULL;
}

CInterfaceView::~CInterfaceView()
{
	if (m_pModeless)
	{
		if (::IsWindow(m_pModeless->GetSafeHwnd()))
			m_pModeless->EndDialog(IDCANCEL);

		delete m_pModeless;
	}
}

void CInterfaceView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInterfaceView)
	DDX_Control(pDX, IDC_LIST1, m_Table);
	//}}AFX_DATA_MAP
}

BOOL CInterfaceView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CFormView::PreCreateWindow(cs);
}

void CInterfaceView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	if (!m_bInitiated)
	{
		// I know, I know, this could be easear
		CString t1 = "Item\nSubitem1\nSubitem2\nSubitem3\nSubitem4\n",
				t2;

		int idx = t1.Find('\n'), i = 0;

		while (idx != -1 && i<5)
		{	
			t2 = t1.Left(idx);
			t1 = t1.Mid(idx+1);
			m_Table.InsertColumn(i, t2, LVCFMT_LEFT, 75);
			i++;
			idx = t1.Find('\n');
		}
		m_bInitiated = true;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CInterfaceView Diagnose

#ifdef _DEBUG
void CInterfaceView::AssertValid() const
{
	CFormView::AssertValid();
}

void CInterfaceView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CInterfaceDoc* CInterfaceView::GetDocument() // Die endg黮tige (nicht zur Fehlersuche kompilierte) Version ist Inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CInterfaceDoc)));
	return (CInterfaceDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CInterfaceView Message-Handler

void CInterfaceView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	m_Table.DeleteAllItems();

	int count = GetDocument()->m_Strings.GetUpperBound();

	for (int i=0; i<=count; i++)
		InsertRow(GetDocument()->m_Strings[i]);
}

int CInterfaceView::InsertRow(CString TabSeparated)
{
	CString t1(TabSeparated), t2;
	int idx = t1.Find('\t'), i = 1, row=LB_ERR;

	if (idx !=-1)
	{
		t2 = t1.Left(idx);
		t1 = t1.Mid(idx+1);
		row = m_Table.InsertItem(m_Table.GetItemCount()+1, t2);
		idx = t1.Find('\t');
	}

	while (idx != -1 && i < 5 && row != LB_ERR)
	{	
		t2 = t1.Left(idx);
		t1 = t1.Mid(idx+1);
		m_Table.SetItemText(row, i, t2);
		CString deb = m_Table.GetItemText(row, i);
		i++;
		idx = t1.Find('\t');
	}
	return row;
}

void CInterfaceView::OnModeless() 
{
	// Display the modal dialog box
	if (!m_pModeless)
		m_pModeless = new CDropDialog;
	
	if (!::IsWindow(m_pModeless->GetSafeHwnd()))
		m_pModeless->Create(IDD_DIALOG1, this);

	m_pModeless->ShowWindow(SW_SHOW);	
}

void CInterfaceView::OnBeginDragList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
//	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	UNUSED(pNMHDR);

	*pResult = 0;

	// Create the drag&drop source and data objects
	COleDropSource *pDropSource = new COleDropSource;
	COleDataSource *pDataSource = new COleDataSource;

	// now determine which rows are selected
	// here it's a dirty copy from the CodeGurus's 
	// CListCtrl section
	int idx = GetSelectedItem(); 

	// nothing selected (must be for dragging)
	if (idx == -1) 
	{
		ASSERT(FALSE);
		return;
	}

	// now grab the data (here: the count and text)
	// and serialize it into an clipboard archive
	CString Data;

	// getting the column count, thanks Zafir!
	CHeaderCtrl* pHeader = (CHeaderCtrl*)m_Table.GetDlgItem(0);

	int colCou = pHeader?pHeader->GetItemCount():0;

	TRY
	{
		CSharedFile file(GMEM_ZEROINIT|GMEM_DDESHARE|GMEM_MOVEABLE);
		TRY
		{
			CArchive ar(&file, CArchive::store);
			TRY
			{
				CString format = AfxGetApp()->GetProfileString("DragDrop", "Clipformat", "Common");
				// for CF_TEXT use is a flat ASCII-stream
				// using tabs as delimiter will cause Excel and others to
				// understand us that we have a table
				do 
				{
					Data.Empty();
					for (int i=0; i<colCou; i++)
						Data += m_Table.GetItemText(idx, i) + "\t";
					ar.WriteString(Data + "\n");
					idx = GetSelectedItem(idx); 
				} while (idx != -1);

				ar.Close();
			}
			CATCH_ALL(eInner)
			{
				// exception while writing into or closing the archive
				ASSERT(FALSE);
			}
			END_CATCH_ALL;
		}
		CATCH_ALL(eMiddle)
		{
			// exception in the destructor of ar
			ASSERT(FALSE);
		}
		END_CATCH_ALL;

		// put the file object into the data object
		pDataSource->CacheGlobalData(CF_TEXT, file.Detach());
		pDataSource->DoDragDrop(DROPEFFECT_MOVE|DROPEFFECT_COPY, NULL, pDropSource);
	}
	CATCH_ALL(eOuter)
	{
		// exception while destructing the file
		ASSERT(FALSE);
	}
	END_CATCH_ALL;

	delete pDropSource;
	delete pDataSource;
}

int CInterfaceView::GetSelectedItem(int StartAfter)
{
	return m_Table.GetNextItem(StartAfter, LVNI_SELECTED);
}

⌨️ 快捷键说明

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