📄 choosedirdlg.cpp
字号:
/****************************************************************************
File: ChooseDirDlg.cpp
Author: Valerie L. Bradley (val@synthcom.com)
Date Created: 08 January 1999
Version: 1.1
Purpose: Demonstrates how the Directory Tree control and the
Drive Combo Box control can be used in an application.
?Copyright 1999 Valerie Bradley.
All rights reserved.
This source code is public domain. I give full permission to all
developers to freely use this code in their own applications, commercial
or otherwise.
This source code is provided "AS IS" without warranty of any kind,
including, but not limited to, any warranty of merchantability, fitness
for any particular purpose, or any warranty otherwise arising out of any
proposal, specification or sample. In no event shall I be liable for
any damages whatsoever arising out of the use of or inability to use
this source code.
****************************************************************************/
#include "stdafx.h"
#include "ChooseDir.h"
#include "ChooseDirDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CChooseDirDlg 对话框
CChooseDirDlg::CChooseDirDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChooseDirDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChooseDirDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChooseDirDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChooseDirDlg)
DDX_Control(pDX, IDC_DIRECTORY_NAME, m_DirName);
DDX_Control(pDX, IDC_DIRECTORY_TREE, m_DirTree);
DDX_Control(pDX, IDC_DRIVE_COMBO, m_DriveList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChooseDirDlg, CDialog)
//{{AFX_MSG_MAP(CChooseDirDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_DRIVE_COMBO, OnSelchangeDriveCombo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CChooseDirDlg 消息映射函数
BOOL CChooseDirDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// 在树视控件中创建图象列表
CImageList * pImageList;
//CImageList pImageList;
pImageList = new CImageList();
pImageList->Create(16, 16, ILC_COLOR, 4, 4);
//pImageList.Create(16, 16, ILC_COLOR, 4, 4);
// 导入位图,必须在CDirectoryTree对象初始化之前进行
CBitmap bBitmap;
for (int nID = IDB_FIRST_TREE_ICON; nID <= IDB_LAST_TREE_ICON; nID++)
{
bBitmap.LoadMappedBitmap(nID);
pImageList->Add(&bBitmap, (COLORREF)0x000000);
//pImageList.Add(&bBitmap, (COLORREF)0x000000);
bBitmap.DeleteObject();
}
m_DirTree.SetBitmapList(pImageList);
//m_DirTree.SetBitmapList(&pImageList);
// 初始化
m_DriveList.Initialize();
m_DirTree.Initialize();
m_DirName.SetWindowText(m_DirTree.GetCurrentDir());
return TRUE; // return TRUE unless you set the focus to a control
}
void CChooseDirDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CChooseDirDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CChooseDirDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CChooseDirDlg::PreTranslateMessage(MSG* pMsg)
{
if (WM_USER_PATHCHANGED == pMsg->message)
{
m_DirName.SetWindowText(m_DirTree.GetCurrentDir());
//OnPathChanged();
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
void CChooseDirDlg::OnSelchangeDriveCombo()
{
int nSelected = 0;
CString szText;
// 确定新盘符,判断是否有效
nSelected = m_DriveList.GetCurSel();
if (!m_DriveList.IsDriveReady(nSelected))
{
// 盘符无效,回复到前一个
m_DriveList.ResetDrive(m_DirTree.GetCurrentDrive());
return;
}
// 获取盘符,并重新设置目录树
m_DriveList.GetLBText(nSelected, szText);
m_DirTree.SetCurrentDrive(szText[0]);
// 重新初始化目录树
m_DirTree.Initialize();
m_DirName.SetWindowText(m_DirTree.GetCurrentDir());
}
//DEL void CChooseDirDlg::OnPathChanged()
//DEL {
//DEL m_DirName.SetWindowText(m_DirTree.GetCurrentDir());
//DEL }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -