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

📄 bcgfiledialog.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This source code is a part of BCGControlBar library.
// You may use, compile or redistribute it as part of your application 
// for free. You cannot redistribute it as a part of a software development 
// library without the agreement of the author. If the sources are 
// distributed along with the application, you should leave the original 
// copyright notes in the source code without any changes.
// This code can be used WITHOUT ANY WARRANTIES on your own risk.
// 
// Stas Levin <stas@iet.co.il>
//*******************************************************************************

// BCGFileDialog.cpp : implementation file
//

#include "stdafx.h"
#include "BCGFileDialog.h"

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

class CNewItemInfo : public CObject
{
	friend class CBCGFileDialog;

	CNewItemInfo (LPCTSTR lpszName, int iIconIndex)
	{
		m_strName = lpszName;
		m_iIconIndex = iIconIndex;
	}

	CString m_strName;
	int		m_iIconIndex;
};

/////////////////////////////////////////////////////////////////////////////
// CBCGFileDialog

const int iTabCtrlId		= 200;
const int iNewListCtrlId	= 201;
const int iRecentListCtrlId	= 202;

WNDPROC CBCGFileDialog::m_wndProc;

IMPLEMENT_DYNAMIC(CBCGFileDialog, CFileDialog)

CBCGFileDialog::CBCGFileDialog (LPCTSTR lpszCaption, BOOL bNewPage, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
		DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
		CFileDialog (TRUE /*bOpenFileDialog*/, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd),
		m_pImagesNew (NULL),
		m_bNewPage (bNewPage),
		m_strCaption (lpszCaption),
		m_hIconBig (NULL),
		m_hIconSmall (NULL)
{
	m_iNewItemIndex = -1;
	m_pBmpLogo = NULL;

	CDocManager* pDocManager = AfxGetApp ()->m_pDocManager;
	if (pDocManager != NULL && lpszFilter == NULL)
	{
		static CString strFilter;
		static CString strDefault;

		BOOL bFirst = TRUE;
		for (POSITION pos = pDocManager->GetFirstDocTemplatePosition (); pos != NULL;)
		{
			CDocTemplate* pTemplate = pDocManager->GetNextDocTemplate (pos);
			ASSERT_VALID (pTemplate);

			CString strFilterExt, strFilterName;

			if (pTemplate->GetDocString (strFilterExt, CDocTemplate::filterExt) &&
				!strFilterExt.IsEmpty() &&
				pTemplate->GetDocString(strFilterName, CDocTemplate::filterName) &&
				!strFilterName.IsEmpty())
			{
				// a file based document template - add to filter list
				ASSERT(strFilterExt[0] == '.');
		
				if (bFirst)
				{
					strDefault = ((LPCTSTR)strFilterExt) + 1;  // skip the '.'
					m_ofn.lpstrDefExt = strDefault;
					m_ofn.nFilterIndex = m_ofn.nMaxCustFilter + 1;  // 1 based number
				}

				// add to filter
				strFilter += strFilterName;
				ASSERT(!strFilter.IsEmpty());  // must have a file type name
				strFilter += (TCHAR)'\0';  // next string please
				strFilter += (TCHAR)'*';
				strFilter += strFilterExt;
				strFilter += (TCHAR)'\0';  // next string please

				m_ofn.nMaxCustFilter++;
			}

			bFirst = FALSE;
		}

		CString allFilter;
		VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
		strFilter += allFilter;
		strFilter += (TCHAR)'\0';   // next string please
		strFilter += _T("*.*");
		strFilter += (TCHAR)'\0';   // last string
		m_ofn.nMaxCustFilter++;

		m_ofn.lpstrFilter = strFilter;
	}
}
//***************************************************************************************
CBCGFileDialog::~CBCGFileDialog ()
{
	while (!m_lstNewItems.IsEmpty ())
	{
		delete m_lstNewItems.RemoveHead ();
	}
}
//***************************************************************************************
static CBCGFileDialog* GetBCGFileDlg (HWND hwdParent)
{
	CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle (hwdParent);
	ASSERT (pDlg != NULL);

	CBCGFileDialog* pFD = (CBCGFileDialog*) pDlg->GetDlgItem(0);
	ASSERT (pFD != NULL);

	return pFD;
}
//***************************************************************************************
LRESULT CALLBACK CBCGFileDialog::WindowProcNew(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_NOTIFY:
		{
			CBCGFileDialog* pFD = GetBCGFileDlg (hwnd);

			LPNMHDR pHdr = (LPNMHDR) lParam;
			ASSERT (pHdr != NULL);

			if (wParam == iTabCtrlId && pHdr->code == TCN_SELCHANGE)
			{
				pFD->OnTabSelchange();
			}
			else if ((wParam == iNewListCtrlId || wParam == iRecentListCtrlId) 
				&& pHdr->code == NM_DBLCLK)
			{
				pFD->OnItemDblClick();
			}
		}
		break;

	case WM_COMMAND:
		{
			if ((int) LOWORD(wParam) == IDOK)
			{
				CBCGFileDialog* pFD = GetBCGFileDlg (hwnd);
				if (pFD->GetPage () != CBCGFileDialog::BCGFileOpen)
				{
					pFD->OnItemDblClick();
					return 0;
				}
			}
		}
		break;

	case WM_PAINT:
		{
			CBCGFileDialog* pFD = GetBCGFileDlg (hwnd);
			pFD->CollectControls ();
	
			if (pFD->m_pBmpLogo != NULL)
			{
				ASSERT_VALID (pFD->m_pBmpLogo);

				CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle (hwnd);
				ASSERT (pDlg != NULL);

				CPaintDC dc (pDlg); // device context for painting
				dc.DrawState (pFD->m_rectLogo.TopLeft (),
					pFD->m_rectLogo.Size (), pFD->m_pBmpLogo,
					DSS_NORMAL);

				CRect rectFrame = pFD->m_rectLogo;
				rectFrame.InflateRect (1, 1);

				dc.Draw3dRect (rectFrame, ::GetSysColor (COLOR_3DSHADOW),
					::GetSysColor (COLOR_3DLIGHT));
			}
		}
	}

	return CallWindowProc(CBCGFileDialog::m_wndProc, hwnd, message, wParam, lParam);
}

BEGIN_MESSAGE_MAP(CBCGFileDialog, CFileDialog)
	//{{AFX_MSG_MAP(CBCGFileDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//-----------------------------------------------------
// My "classic " trick - how I can access to protected
// member m_pRecentFileList?
//-----------------------------------------------------
class CBCGApp : public CWinApp
{
	friend class CBCGFileDialog;
};

void CBCGFileDialog::OnInitDone()
{
	const int iBorderWidth = 20;
	const int iBorderHeight = 40;

	CWnd* pFD = GetParent();
	ASSERT (pFD != NULL);

	CRect rectClient;
	pFD->GetClientRect (rectClient);

	int nNewDlgWidth = rectClient.Width () + iBorderWidth * 2;

	int iLogoAreaHeight = 0;
	if (m_pBmpLogo != NULL)
	{
		BITMAP bmp;
		m_pBmpLogo->GetBitmap (&bmp);

		m_rectLogo = CRect (CPoint ((nNewDlgWidth - bmp.bmWidth) / 2, 8),
							CSize (bmp.bmWidth, bmp.bmHeight));
		iLogoAreaHeight = bmp.bmHeight + 20;
	}

	//---------------------------
	// Adjust parent window size:
	//---------------------------
	pFD->SetWindowPos (NULL, -1, -1, nNewDlgWidth,
								rectClient.Height () + iBorderHeight * 2 + iLogoAreaHeight,
					SWP_NOMOVE | SWP_NOZORDER);

	//-------------------
	// Move all controls:
	//-------------------
	CWnd* pWndChild = pFD->GetWindow (GW_CHILD);
	while (pWndChild != NULL)
	{
		CRect rectCtl;
		pWndChild->GetClientRect (rectCtl);
		pWndChild->MapWindowPoints (pFD, rectCtl);
		pWndChild->SetWindowPos (NULL, 
			rectCtl.left + iBorderWidth, 
			rectCtl.top + iBorderHeight + iLogoAreaHeight,
			-1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

		pWndChild = pWndChild->GetNextWindow ();
	}

	//------------------------------------------
	// Create new and recent file list controls:
	//------------------------------------------
	CRect rectList (0, 0, 0, 0);
	m_wndNewList.Create (WS_BORDER | WS_TABSTOP | WS_CHILD | LVS_ICON | LVS_SINGLESEL, 
							rectList, pFD, iNewListCtrlId);
	m_wndNewList.ModifyStyleEx (0, WS_EX_CLIENTEDGE);

	if (m_pImagesNew != NULL)
	{
		m_wndNewList.SetImageList (m_pImagesNew, LVSIL_NORMAL);
	}

	int i = 0;
	for (POSITION pos = m_lstNewItems.GetHeadPosition (); pos != NULL; i ++)
	{
		CNewItemInfo* pInfo = (CNewItemInfo*) m_lstNewItems.GetNext (pos);
		ASSERT_VALID (pInfo);

		m_wndNewList.InsertItem (i, pInfo->m_strName, pInfo->m_iIconIndex);
	}

	m_wndRecentList.Create (WS_TABSTOP | WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT, 
							rectList, pFD, iRecentListCtrlId);
	m_wndRecentList.ModifyStyleEx (0, WS_EX_CLIENTEDGE);

	m_ImagesRecent.Create (	::GetSystemMetrics (SM_CXSMICON),
							::GetSystemMetrics (SM_CYSMICON),
							ILC_COLOR, 0, 10);
	m_wndRecentList.SetImageList (&m_ImagesRecent, LVSIL_SMALL);

	m_wndRecentList.InsertColumn (0, _T("File"), LVCFMT_LEFT, 100);

⌨️ 快捷键说明

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