📄 mydlg.cpp
字号:
////////////////////////////////////////////////////////////////
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0 for Windows XP and probably Windows 2000 too.
//
#include "StdAfx.h"
#include "MyDlg.h"
#include "FileDlgHelper.h"
#include "Resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// IDs of controls in Explorer-style dialog, from Spy
#define ID_FILESOFTYPE 0x0441
#define ID_FILENAME 0x0442
#define ID_LOOKIN 0x0443
IMPLEMENT_DYNAMIC(CMyOpenDlg, CFileDialog)
BEGIN_MESSAGE_MAP(CMyOpenDlg, CFileDialog)
ON_COMMAND(ID_SELECT_ALL,OnSelectAll)
ON_UPDATE_COMMAND_UI(ID_SELECT_ALL,OnUpdateSelectAll)
ON_WM_SIZE()
END_MESSAGE_MAP()
//////////////////
// does path ends in ".txt" ?
//
static BOOL IsTextFileName(CString fn)
{
fn.MakeLower();
return fn.Right(4)==".txt";
}
////////////////
// Constructor
//
CMyOpenDlg::CMyOpenDlg() : CFileDialog(TRUE,
NULL, // no default extension
NULL, // ..or file name
OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,
_T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"))
{
m_ofn.lpstrTitle = _T("请选择文件或文件夹");
}
//////////////////
// Initialize dialog.
// For Explorer, this is actually a child of the main dialog.
//
BOOL CMyOpenDlg::OnInitDialog()
{
// subclass controls...
m_edit1.SubclassDlgItem(IDC_MYDLGINFO1, this);
m_edit2.SubclassDlgItem(IDC_MYDLGINFO2, this);
// initialize helper
m_dlghelper.Init(this);
return CFileDialog::OnInitDialog();
}
//////////////////
// Select all text files. This won't work if user has checked "Hide
// extensions for known file types" because then the item name in the list
// box doesn't end in .txt !!
//
void CMyOpenDlg::OnSelectAll()
{
CFileDlgHelper& fdh = m_dlghelper;
CListCtrl* plc = fdh.GetListCtrl();
for (int i=0; i<plc->GetItemCount(); i++) {
CString fn = fdh.GetItemPathName(i);
if (IsTextFileName(fn)) {
plc->SetItemState(i,LVIS_SELECTED,LVIS_SELECTED);
}
}
plc->SetFocus();
}
//////////////////
// Update "Select All" button: disable if no .txt files. This won't work if
// user has checked "Hide extensions for known file types" because then the
// item name in the list box doesn't end in .txt !!
//
void CMyOpenDlg::OnUpdateSelectAll(CCmdUI* pCmdUI)
{
CFileDlgHelper& fdh = m_dlghelper;
CListCtrl* plc = fdh.GetListCtrl();
for (int i=0; i<plc->GetItemCount(); i++) {
CString fn = fdh.GetItemPathName(i);
if (IsTextFileName(fn)) {
pCmdUI->Enable(TRUE);
return;
}
}
pCmdUI->Enable(FALSE);
}
//////////////////
// DoModal override: use my template
//
int CMyOpenDlg::DoModal()
{
// Add my custom dialog to bottom
m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_MYOPENDLG);
m_ofn.Flags |= OFN_ENABLETEMPLATE;
return CFileDialog::DoModal();
}
//////////////////
// User selected new file (CDN_SELCHANGE)
//
void CMyOpenDlg::OnFileNameChange()
{
ShowFileInfo();
}
//////////////////
// User selected new folder (CDN_SELCHANGE)
//
void CMyOpenDlg::OnFolderChange()
{
ShowFileInfo();
}
//////////////////
// User selected new file type (from drop-down)
//
void CMyOpenDlg::OnTypeChange()
{
ShowFileInfo();
}
////////////////// CDM_GETFILEPATH
// Common helper: show information in the preview and debug panes
//
void CMyOpenDlg::ShowFileInfo()
{
CFileDlgHelper& fdh = m_dlghelper;
CString path = GetPathName();
CString fldr = GetFolderPath();
// Create debug message
//
CString s;
s.Format(_T("GetPathName=%s\nGetFolderPath=%s\n"),
(LPCTSTR)path, (LPCTSTR)fldr);
CListCtrl* plc = fdh.GetListCtrl();
s += _T("选中:\n");
int nSelected = 0;
POSITION pos = plc->GetFirstSelectedItemPosition();
while (pos) {
int i = plc->GetNextSelectedItem(pos);
CString temp;
temp.Format(_T(" %s %s = %s\n"),
(LPCTSTR)fdh.GetItemText(i),
fdh.IsItemFolder(i) ? _T("(FOLDER)") : _T(""),
(LPCTSTR)fdh.GetItemPathName(i));
s += temp;
nSelected++;
}
AddText(m_edit2, s, TRUE);
// Create preview text
//
s.Empty();
if (nSelected==1 && IsTextFileName(path)) {
s = GetTextPreview(path);
} else if (nSelected>1) {
s = _T("[选中多个]");
} else if (nSelected==0) {
s = _T("[没有选中]");
}
AddText(m_edit1,s,TRUE);
}
//////////////////
// Preview: Read first 256 characters of text file
//
CString CMyOpenDlg::GetTextPreview(LPCTSTR pszPath)
{
CString buf;
CFile f;
if (f.Open(pszPath,CFile::modeRead)) {
int len = f.Read(buf.GetBuffer(256),256);
buf.ReleaseBuffer();
if (len==256)
buf += "...";
}
return buf;
}
//////////////////
// Helper adds text to an edit control that I've added to the open dialog
//
void CMyOpenDlg::AddText(CEdit& wndEdit, LPCTSTR lpText, BOOL bClear)
{
if (wndEdit) {
// Convert \n to \n\r for Windows brain-damaged edit control !&#%!
// It's 2001, and I'm still writing code like this!
//
LPCTSTR src = lpText;
TCHAR buf[1024];
TCHAR* dst = buf;
TCHAR* endbuf = buf + sizeof(buf) - 1;
while (*src && dst < endbuf) {
if (*src == '\n')
*dst++ = '\r';
*dst++ = *src++;
}
*dst = 0;
wndEdit.SetSel(bClear ? 0 : -1, -1); // end of edit text
wndEdit.ReplaceSel(buf); // append string..
wndEdit.SendMessage(EM_SCROLLCARET); // ..and show caret
}
}
//////////////////
// Dialog was sized: reposition my controls. Zzzzz.
//
void CMyOpenDlg::OnSize(UINT nType, int cx, int cy)
{
CWnd* pDlg = GetParent();
CRect rcDlg;
pDlg->GetWindowRect(&rcDlg);
CWnd* pCancel = pDlg->GetDlgItem(IDCANCEL);
ASSERT(pCancel);
CRect rcCancel;
pCancel->GetWindowRect(&rcCancel);
int cxRightMargin = rcDlg.right-rcCancel.right;
CWnd* pSelAll = GetDlgItem(ID_SELECT_ALL);
ASSERT(pSelAll);
CRect rc;
pSelAll->GetWindowRect(&rc);
rc.left = rcDlg.right - cxRightMargin - rcCancel.Width();
rc.right = rc.left + rcCancel.Width();
rc.bottom= rc.top + rcCancel.Height();
ScreenToClient(&rc);
pSelAll->SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),0);
for (int i = IDC_MYDLGINFO1; i<= IDC_MYDLGINFO2; i++) {
CWnd* pInfoWnd = GetDlgItem(i);
ASSERT(pInfoWnd);
pInfoWnd->GetWindowRect(&rc);
rc.right = rcDlg.right - cxRightMargin;
ScreenToClient(&rc);
pInfoWnd->SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -