📄 filelistbox.cpp
字号:
/*
Filename : FileListBox.cpp
Part of : File Browser
Description : Implementation of File Browser's file system browser list box
Version : 3.2
This example is only to be used with PC Connectivity API version 3.2.
Compability ("as is") with future versions is not quaranteed.
Copyright (c) 2007 Nokia Corporation.
This material, including but not limited to documentation and any related
computer programs, is protected by intellectual property rights of Nokia
Corporation and/or its licensors.
All rights are reserved. Reproducing, modifying, translating, or
distributing any or all of this material requires the prior written consent
of Nokia Corporation. Nokia Corporation retains the right to make changes
to this material at any time without notice. A copyright license is hereby
granted to download and print a copy of this material for personal use only.
No other license to any other intellectual property rights is granted. The
material is provided "as is" without warranty of any kind, either express or
implied, including without limitation, any warranty of non-infringement,
merchantability and fitness for a particular purpose. In no event shall
Nokia Corporation be liable for any direct, indirect, special, incidental,
or consequential loss or damages, including but not limited to, lost profits
or revenue,loss of use, cost of substitute program, or loss of data or
equipment arising out of the use or inability to use the material, even if
Nokia Corporation has been advised of the likelihood of such damages occurring.
*/
#include "stdafx.h"
#include "FileBrowser.h"
#include "FileListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//===================================================================
// CFileListBox
//===================================================================
// Constructor
//
//===================================================================
CFileListBox::CFileListBox():CListBox()
{
m_sCurrentPath = L"";
m_sCurrentFile = L"";
m_pLabel = NULL;
}
//===================================================================
// Destructor
//
//===================================================================
CFileListBox::~CFileListBox()
{
}
BEGIN_MESSAGE_MAP(CFileListBox, CListBox)
//{{AFX_MSG_MAP(CFileListBox)
ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
ON_CONTROL_REFLECT(LBN_DBLCLK, OnDblclk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//===================================================================
// GetCurrentFolder
//
// Returns currently selected folder.
// If no folder is selected, returns folder, the content of which is
// currently shown.
//
//===================================================================
CString CFileListBox::GetCurrentFolder()
{
CString strRetVal = m_sCurrentPath;
int index = GetCurSel();
if(index != LB_ERR)
{ // there is a selected item
CString strSelectedtxt;
GetText(index, strSelectedtxt);
int iLength = strSelectedtxt.GetLength();
if(iLength > 2 && strSelectedtxt[0] == '[')
{
if(iLength == 5 && strSelectedtxt[1] == '-' && strSelectedtxt[3] == '-')
{ // drive [-driveletter-] --> 'driveletter:\'
strRetVal = strSelectedtxt[2] + L":\\";
}
else
{ // folder [directoryname]
WCHAR lpszFullPath[MAX_PATH] = {0};
wcsncpy_s(lpszFullPath, MAX_PATH, m_sCurrentPath, MAX_PATH);
// removing '[' and ']'
strSelectedtxt = strSelectedtxt.Mid(1, iLength - 2);
::PathAppend(lpszFullPath, strSelectedtxt);
strRetVal = lpszFullPath;
}
}
}
return strRetVal;
}
//===================================================================
// GetCurrentFile
//
// Returns currently selected file.
// If no file is selected, returns empty string.
//
//===================================================================
CString CFileListBox::GetCurrentFile()
{
return CString(::PathFindFileName(m_sCurrentFile));
}
//===================================================================
// ShowFiles
//
// Adds folders, files and drives of current folder to listbox
//
//===================================================================
void CFileListBox::ShowFiles()
{
WCHAR lpszFullPath[MAX_PATH] = {0};
wcsncpy_s(lpszFullPath, MAX_PATH, m_sCurrentPath, MAX_PATH);
::PathAppend(lpszFullPath, L"*.*");
ResetContent();
Dir(DDL_EXCLUSIVE|DDL_DIRECTORY, lpszFullPath);
Dir(DDL_READWRITE, lpszFullPath);
if(m_sCurrentPath.GetLength() < 4)
{ // root directory e.g. 'c:\'
Dir(DDL_DRIVES, L"");
}
if(m_pLabel)
{
m_pLabel->SetWindowText(L"PC: " + m_sCurrentPath);
}
}
//===================================================================
// Init
//
// Performs initialization of CFileListBox.
// Must be called before using other methods.
//
//===================================================================
void CFileListBox::Init(CStatic* pLabel)
{
m_sCurrentPath = L"c:\\";
m_pLabel = pLabel;
ShowFiles();
}
//===================================================================
// CFileListBox message handlers
//===================================================================
// OnSelchange
//
// User has selected new item in the listbox.
//
//===================================================================
void CFileListBox::OnSelchange()
{
WCHAR lpszFullPath[MAX_PATH] = {0};
int index = GetCurSel();
CString strSelectedtxt;
GetText(index, strSelectedtxt);
if(!strSelectedtxt.IsEmpty() && strSelectedtxt[0] != '[')
{
wcsncpy_s(lpszFullPath, MAX_PATH, m_sCurrentPath, MAX_PATH);
::PathAppend(lpszFullPath, strSelectedtxt);
}
m_sCurrentFile = lpszFullPath;
}
//===================================================================
// OnDblclk
//
// User has doubleclicked in the listbox.
//
//===================================================================
void CFileListBox::OnDblclk()
{
WCHAR lpszFullPath[MAX_PATH] = {0};
CString strSelectedtxt;
int index = GetCurSel();
if(index != LB_ERR)
{
GetText(index, strSelectedtxt);
int iLength = strSelectedtxt.GetLength();
if(iLength > 2 && strSelectedtxt[0] == '[')
{
if(iLength == 5 && strSelectedtxt[1] == '-')
{ // drive [-driveletter-]
// getting drive letter
m_sCurrentPath = strSelectedtxt.Mid(2, 1) + ':';
}
else
{ // directory [directoryname]
wcsncpy_s(lpszFullPath, MAX_PATH, m_sCurrentPath, MAX_PATH);
// removing '[' and ']'
strSelectedtxt = strSelectedtxt.Mid(1, iLength - 2);
::PathAppend(lpszFullPath, strSelectedtxt);
m_sCurrentPath = lpszFullPath;
}
ShowFiles();
}
else
{
wcsncpy_s(lpszFullPath, MAX_PATH, m_sCurrentPath, MAX_PATH);
::PathAppend(lpszFullPath, strSelectedtxt);
}
}
else
{
TRACE(L"CFileListBox::OnDblclk(): No item doubleclicked\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -