📄 filedlgex.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002 by F.R.Wen
// All rights reserved
//
// If this code works, it was written by F.R.Wen
// If not, I don't know who wrote it
// You can distribute and use freely
//
// Send bug reports, bug fixes, enhancements, etc. to:
// frwen@sohu.com
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FileDlgEx.h"
#include <DLGS.H>
#define MAX_NUM 10
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFileDlgEx
const int iTabCtrlId = 200;
const int iNewListCtrlId = 201;
const int iRecentListCtrlId = 202;
WNDPROC CFileDlgEx::m_wndProc;
IMPLEMENT_DYNAMIC(CFileDlgEx, CFileDialog)
CFileDlgEx::CFileDlgEx (LPCTSTR lpszCaption,
BOOL bOpenFileDialog,
BOOL bNewPage,
LPCTSTR lpszDefExt,
LPCTSTR lpszFileName,
DWORD dwFlags,
LPCTSTR lpszFilter,
CWnd* pParentWnd) :
CFileDialog (bOpenFileDialog,
lpszDefExt,
lpszFileName,
dwFlags,
lpszFilter,
pParentWnd),
m_pImagesNew (NULL),
m_strCaption (lpszCaption),
m_hIconBig (NULL),
m_hIconSmall (NULL),
m_iLogoAreaHeight (0)
{
if(bOpenFileDialog)
m_bNewPage=bNewPage;
else
m_bNewPage=FALSE;
m_iNewItemIndex = -1;
m_pBmpLogo = NULL;
nNum = 0;
}
//***************************************************************************************
CFileDlgEx::~CFileDlgEx ()
{
while (!m_lstNewItems.IsEmpty ())
{
delete m_lstNewItems.RemoveHead ();
}
}
//***************************************************************************************
static CFileDlgEx* GetBCGFileDlg (HWND hwdParent)
{
CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle (hwdParent);
ASSERT (pDlg != NULL);
CFileDlgEx* pFD = (CFileDlgEx*) pDlg->GetDlgItem(0);
ASSERT (pFD != NULL);
return pFD;
}
//***************************************************************************************
LRESULT CALLBACK CFileDlgEx::WindowProcNew(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_NOTIFY:
{
CFileDlgEx* 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)
{
CFileDlgEx* pFD = GetBCGFileDlg (hwnd);
if (pFD->GetPage () != CFileDlgEx::FileDlgExOpen)
{
pFD->OnItemDblClick();
return 0;
}
}
}
break;
case WM_PAINT:
{
CFileDlgEx* 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));
}
}
break;
case WM_SIZE:
{
CFileDlgEx* pFD = GetBCGFileDlg (hwnd);
ASSERT_VALID (pFD);
CWnd* pFDParent = pFD->GetParent();
ASSERT (pFDParent != NULL);
CRect rectTabs;
pFDParent->GetClientRect (rectTabs);
rectTabs.DeflateRect (4, 4);
rectTabs.top += pFD->m_iLogoAreaHeight;
pFD->m_wndTab.SetWindowPos (NULL, -1, -1,
rectTabs.Width (), rectTabs.Height (),
SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
pFD->m_lstFDControls.RemoveAll ();
pFD->CollectControls ();
}
break;
}
return CallWindowProc(CFileDlgEx::m_wndProc, hwnd, message, wParam, lParam);
}
//****************************************************************
BEGIN_MESSAGE_MAP(CFileDlgEx, CFileDialog)
//{{AFX_MSG_MAP(CFileDlgEx)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//****************************************************************
void CFileDlgEx::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;
if(m_pBmpLogo != NULL)
{
BITMAP bmp;
m_pBmpLogo->GetBitmap(&bmp);
m_rectLogo=CRect(CPoint((nNewDlgWidth-bmp.bmWidth)/2,8),
CSize(bmp.bmWidth, bmp.bmHeight));
m_iLogoAreaHeight=bmp.bmHeight+20;
}
//-----------------------------------
// Adjust parent window size:
//-----------------------------------
pFD->ModifyStyle (WS_THICKFRAME, WS_DLGFRAME | WS_BORDER);
pFD->ModifyStyleEx (WS_EX_WINDOWEDGE, 0);
pFD->SetWindowPos (NULL, -1, -1, nNewDlgWidth,
rectClient.Height()+iBorderHeight*2+m_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 + m_iLogoAreaHeight,
-1, -1, SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
pWndChild = pWndChild->GetNextWindow ();
}
//------------------------------------------
// Create new 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_strFileDescription,pInfo->m_iIconIndex);
}
//------------------------------------------
// Create recent file list controls:
//------------------------------------------
m_wndRecentList.Create(WS_TABSTOP|WS_CHILD|LVS_SINGLESEL|LVS_REPORT,
rectList, pFD, iRecentListCtrlId);
m_wndRecentList.ModifyStyleEx(0,WS_EX_CLIENTEDGE);
m_pImagesRecent.Create(::GetSystemMetrics(SM_CXSMICON),
::GetSystemMetrics(SM_CYSMICON),
ILC_COLOR, 0, 10);
m_wndRecentList.SetImageList(&m_pImagesRecent, LVSIL_SMALL);
m_wndRecentList.InsertColumn(0,"名称",LVCFMT_LEFT,100);
m_wndRecentList.InsertColumn(1,"所在文件夹",LVCFMT_LEFT);
// collect recently files list
CollectRecentFiles();
// add file to recent filelist control
int iNumOfFiles = 0;
pos=m_FileList.GetHeadPosition();
while(pos!=NULL)
{
CString strFile=m_FileList.GetNext(pos);
if(!strFile.IsEmpty())
{
CString strPath;
CString strName;
int iImage=-1;
int iIndex=strFile.ReverseFind(_T('\\'));
if(iIndex!=-1)
{
strPath=strFile.Left(iIndex);
strName=strFile.Mid(iIndex+1);
}
else
{
strName=strFile;
}
SHFILEINFO sfi;
HIMAGELIST himlSmall=(HIMAGELIST)SHGetFileInfo(strFile,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
if(himlSmall!=NULL)
{
CImageList* pImages=CImageList::FromHandle(himlSmall);
ASSERT (pImages != NULL);
iImage=m_pImagesRecent.Add(pImages->ExtractIcon(sfi.iIcon));
}
iIndex=m_wndRecentList.InsertItem(iNumOfFiles++,strName,iImage);
m_wndRecentList.SetItemText(iIndex,1,strPath);
int iWidth2=m_wndRecentList.GetStringWidth(strPath)+20;
if(iWidth2>m_wndRecentList.GetColumnWidth(1))
m_wndRecentList.SetColumnWidth(1,iWidth2);
}
}
//-----------------------------------------
// Create tabs control:
//-----------------------------------------
CRect rectTabs;
pFD->GetClientRect(rectTabs);
rectTabs.DeflateRect(4,4);
rectTabs.top+=m_iLogoAreaHeight;
m_wndTab.Create(WS_TABSTOP|WS_CHILD|WS_VISIBLE,
rectTabs,pFD,iTabCtrlId);
m_wndTab.SetFont(GetFont());
m_wndTab.SetOwner(this);
TC_ITEM tc;
tc.mask=TCIF_TEXT;
int iPage=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -