📄 favtreectrl.cpp
字号:
SetItemData(hnode, GetItemData(m_hDragItem));
}
DeleteItem(m_hDragItem);
if(source+"\\" == ((CMainFrame*)pMainFrame)->m_strStartFavFolder)
{
m_hStartFolder = hnode;
SetItemState(hnode,TVIS_BOLD,TVIS_BOLD);
}
//update menu order
source = source.Left(source.GetLength()-strName.GetLength());
hParent = GetParentItem(m_hDragItem);
BuildMenuOrder(source, hParent);
target = target.Left(target.GetLength()-strName.GetLength());
if(bTargetFolder && m_bMoveIntoFolder)
hParent = m_hDropItem;
else
hParent = GetParentItem(m_hDropItem);
BuildMenuOrder(target, hParent);
//move sub order
CString str;
if(((CMainFrame*)pMainFrame)->GetFavoriteFolder(str))
{
source = source.Mid(str.GetLength()+1);
target = target.Mid(str.GetLength()+1);
MoveSubMenuOrder(source+strName, target+strName);
}
}
else
//not update menu
((CMainFrame*)pMainFrame)->m_bFavDirty = FALSE;
}
else
{
//only sort the order
GetItemImage(m_hDragItem, img, selimg);
if(!bTargetFolder)
{
hnode = InsertItem(strName, img, selimg, hParent, m_hDropItem);
}
else
hnode = InsertItem(strName, img, selimg, m_hDropItem, TVI_FIRST);
SetItemData(hnode, GetItemData(m_hDragItem));
DeleteItem(m_hDragItem);
if(source+"\\" == ((CMainFrame*)pMainFrame)->m_strStartFavFolder)
{
m_hStartFolder = hnode;
SetItemState(hnode,TVIS_BOLD,TVIS_BOLD);
}
//update menu order
source = source.Left(source.GetLength()-strName.GetLength());
if(bTargetFolder && m_bMoveIntoFolder)
hParent = m_hDropItem;
else
hParent = GetParentItem(m_hDropItem);
BuildMenuOrder(source, hParent);
}
}
}
CTreeCtrl::OnLButtonUp(nFlags, point);
}
#define SCROLL_BORDER 10
#define SCROLL_SPEED_ZONE_WIDTH 20
void CFavTreeCtrl::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if( nIDEvent != m_nTimerID )
{
if( nIDEvent == m_idTimer )
{
HTREEITEM htiFloat = GetDropHilightItem();
if( htiFloat && htiFloat == m_hDropItem )
{
int img, selimg;
GetItemImage(htiFloat, img, selimg);
if(img == 0 || img == 5)
{
ExpandNode(htiFloat);
Expand( htiFloat, TVE_EXPAND );
}
}
}
CTreeCtrl::OnTimer(nIDEvent);
return;
}
HTREEITEM hitem;
// Doesn't matter that we didn't initialize m_timerticks
m_timerticks++;
POINT pt;
GetCursorPos( &pt );
CRect rect;
GetClientRect( &rect );
ClientToScreen( &rect );
// NOTE: Screen coordinate is being used because the call
// to DragEnter had used the Desktop window.
CImageList::DragMove(pt);
int iMaxV = GetScrollLimit(SB_VERT);
int iPosV = GetScrollPos(SB_VERT);
// The cursor must not only be SOMEWHERE above/beneath the tree control
// BUT RIGHT above or beneath it
// i.e. the x-coordinates must be those of the control (+/- SCROLL_BORDER)
if ( pt.x < rect.left - SCROLL_BORDER )
; // Too much to the left
else if ( pt.x > rect.right + SCROLL_BORDER )
; // Too much to the right
else if( (pt.y < rect.top + SCROLL_BORDER) && iPosV )
{
hitem = GetFirstVisibleItem();
// We need to scroll up
// Scroll slowly if cursor near the treeview control
int slowscroll = 6 - (rect.top + SCROLL_BORDER - pt.y) / SCROLL_SPEED_ZONE_WIDTH;
if( 0 == ( m_timerticks % (slowscroll > 0? slowscroll : 1) ) )
{
CImageList::DragShowNolock(FALSE);
SendMessage( WM_VSCROLL, SB_LINEUP);
SelectDropTarget(hitem);
m_hDropItem = hitem;
CImageList::DragShowNolock(TRUE);
}
}
else if( (pt.y > rect.bottom - SCROLL_BORDER) && (iPosV!=iMaxV) )
{
// We need to scroll down
// Scroll slowly if cursor near the treeview control
int slowscroll = 6 - (pt.y - rect.bottom + SCROLL_BORDER ) / SCROLL_SPEED_ZONE_WIDTH;
if( 0 == ( m_timerticks % (slowscroll > 0? slowscroll : 1) ) )
{
CImageList::DragShowNolock(FALSE);
SendMessage( WM_VSCROLL, SB_LINEDOWN);
int nCount = GetVisibleCount();
hitem = GetFirstVisibleItem();
for ( int i=0; i<nCount-1; ++i )
hitem = GetNextVisibleItem(hitem);
if( hitem )
SelectDropTarget(hitem);
m_hDropItem = hitem;
CImageList::DragShowNolock(TRUE);
}
}
// The cursor must be in a small zone IN the treecontrol at the left/right
int iPosH = GetScrollPos(SB_HORZ);
int iMaxH = GetScrollLimit(SB_HORZ);
if ( !rect.PtInRect(pt) ) return; // not in TreeCtrl
else if ( (pt.x < rect.left + SCROLL_BORDER) && (iPosH != 0) )
{
// We need to scroll to the left
CImageList::DragShowNolock(FALSE);
SendMessage(WM_HSCROLL, SB_LINELEFT);
CImageList::DragShowNolock(TRUE);
}
else if ( (pt.x > rect.right - SCROLL_BORDER) && (iPosH != iMaxH) )
{
// We need to scroll to the right
CImageList::DragShowNolock(FALSE);
SendMessage(WM_HSCROLL, SB_LINERIGHT);
CImageList::DragShowNolock(TRUE);
}
}
void CFavTreeCtrl::OnDestroy()
{
try{
CTreeCtrl::OnDestroy();
// TODO: Add your message handler code here
CImageList* pImage = GetImageList( TVSIL_NORMAL );
delete pImage;
if( m_idTimer )
{
KillTimer( m_idTimer );
m_idTimer = 0;
}
}catch(...)
{
}
}
void CFavTreeCtrl::GetItemPath(HTREEITEM hti, CString &path)
{
HTREEITEM hItem = GetParentItem(hti);
HTREEITEM hParent = GetParentItem(hItem);
HTREEITEM hRoot = GetRootItem();
while(hParent !=NULL)
{
path = GetItemText(hItem)+"\\"+path;
hItem = hParent;
hParent = GetParentItem(hParent);
}
}
void CFavTreeCtrl::BuildMenuOrder(CString &path, HTREEITEM hParent)
{
//read objects in a folder
CString strPath;
CString str,str2;
WIN32_FIND_DATA wfd;
HANDLE h;
CStringArray astrFavorites;
CStringArray astrDirs;
CStringArray astrTree;
CArray<int, int> anType;
int num = 0;
int slen;
try{
//load order in the tree
if(ItemHasChildren(hParent))
{
int img, selimg;
HTREEITEM hchild = GetChildItem(hParent);
while(hchild!=NULL)
{
astrTree.Add(GetItemText(hchild));
GetItemImage(hchild, img, selimg);
if(img==0|| img == 5)
anType.Add(1); //folder;
else
anType.Add(2);
hchild = GetNextSiblingItem(hchild);
}
}
else
return;
//construct cmenuorder
BOOL found;
num = astrTree.GetSize();
CMenuOrder mo;
try
{
mo.mois = new CMenuOrderItem[num];
}
catch(CMemoryException* e)
{
mo.mois = NULL;
// if(e!=NULL)e->Delete();
}
if(mo.mois==NULL)
return;
mo.len = num;
// make sure there's a trailing backslash
strPath = path;
if(strPath[strPath.GetLength() - 1] != _T('\\'))
strPath += _T('\\');
strPath += "*.*";
// CString strcha = "频道";
// CString strcha2; strcha2.LoadString(IDS_CHANNEL_E);
// now scan the directory, first for .URL files and then for subdirectories
// that may also contain .URL files
int i=0;
h = FindFirstFile(strPath, &wfd);
if(h != INVALID_HANDLE_VALUE)
{
do
{
if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
{
str = wfd.cFileName;
str.MakeLower();
if(str.Right(4) == _T(".url") || str.Right(4) == _T(".lnk"))
{
str=str.Left(str.GetLength()-4);
found = FALSE;
for(int j=0; j<astrTree.GetSize() && !found; j++)
{
if(anType[j]==2 && str.CompareNoCase(astrTree[j])==0)
{
found = TRUE;
if(i<num)
mo.mois[i].order = j;
}
}
if(found && i<num)
{
mo.mois[i].itemtype = 0x32;
FileTimeToDosDateTime(&wfd.ftLastWriteTime, &mo.mois[i].filedate, &mo.mois[i].filetime);
mo.mois[i].filesize = wfd.nFileSizeLow;
mo.mois[i].filetype = 0x20;
slen = strlen(wfd.cFileName);
try
{
mo.mois[i].longname=new char[slen+2];
}
catch(CMemoryException* e)
{
mo.mois[i].longname=NULL;
// if(e!=NULL)e->Delete();
}
if(mo.mois[i].longname)
strcpy(mo.mois[i].longname, wfd.cFileName);
try
{
mo.mois[i].shortname=new char[14];
}
catch(CMemoryException* e)
{
mo.mois[i].shortname = NULL;
// if(e!=NULL)e->Delete();
}
if(mo.mois[i].shortname)
strcpy(mo.mois[i].shortname, wfd.cAlternateFileName);
i++;
}
}
}
else if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// ignore the current and parent directory entries
if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName, _T("..")) == 0)
continue;
str = wfd.cFileName;
found = FALSE;
for(int j=0; j<astrTree.GetSize() && !found; j++)
{
if(anType[j]==1 && str.CompareNoCase(astrTree[j])==0)
{
found = TRUE;
if(i<num)
mo.mois[i].order = j;
}
}
if(found && i<num)
{
mo.mois[i].itemtype = 0x31;
FileTimeToDosDateTime(&wfd.ftLastWriteTime, &mo.mois[i].filedate, &mo.mois[i].filetime);
mo.mois[i].filesize = wfd.nFileSizeLow;
mo.mois[i].filetype = 0x10;
try
{
mo.mois[i].longname=new char[strlen(wfd.cFileName)+2];
}
catch(CMemoryException* e)
{
mo.mois[i].longname=NULL;
// if(e!=NULL)e->Delete();
}
if(mo.mois[i].longname)
strcpy(mo.mois[i].longname, wfd.cFileName);
try
{
mo.mois[i].shortname=new char[14];
}
catch(CMemoryException* e)
{
mo.mois[i].shortname=NULL;
// if(e!=NULL)e->Delete();
}
if(mo.mois[i].shortname)
strcpy(mo.mois[i].shortname, wfd.cAlternateFileName);
i++;
}
}
} while(FindNextFile(h, &wfd));
FindClose(h);
}
else
return;
//save it
mo.len = i;
if(((CMainFrame*)pMainFrame)->GetFavoriteFolder(str))
{
strPath = path;
strPath = strPath.Mid(str.GetLength()+1);
mo.WriteMenuOrder(strPath);
}
}catch(...)
{
}
}
void CFavTreeCtrl::MoveSubMenuOrder(LPCSTR source, LPCSTR target)
{
HKEY hSrcKey;
HKEY hTarKey;
CString lpSubKey = MENU_ORDER_KEY, lpSubKey2;
lpSubKey += source;
HRESULT hr = RegOpenKey(HKEY_CURRENT_USER, lpSubKey, &hSrcKey);
if(hr == ERROR_SUCCESS)
{
BYTE* lpBuf = NULL;
DWORD size = 0;
DWORD type = 0;
hr = RegQueryValueEx(hSrcKey, "Order", NULL, &type, NULL, &size);
if(size>0)
{
try
{
lpBuf = new BYTE[size];
}
catch(CMemoryException* e)
{
RegCloseKey(hSrcKey);
// if(e!=NULL)e->Delete();
return;
}
hr = RegQueryValueEx(hSrcKey, "Order", NULL, &type, lpBuf, &size);
//copy it
lpSubKey = MENU_ORDER_KEY;
lpSubKey += target;
hr = RegCreateKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hTarKey, NULL);
if(hr == ERROR_SUCCESS)
{
RegSetValueEx(hTarKey, "Order", 0, REG_BINARY, lpBuf, size);
}
RegCloseKey(hTarKey);
//release
delete[] lpBuf;
}
//sub keys
int i =0;
char Name[MAX_PATH+1];
hr = RegEnumKey(hSrcKey, i, Name, MAX_PATH);
while(hr == ERROR_SUCCESS)
{
lpSubKey = source; lpSubKey += "\\"; lpSubKey += Name;
lpSubKey2 = target; lpSubKey2 += "\\"; lpSubKey2+= Name;
MoveSubMenuOrder(lpSubKey, lpSubKey2);
i++;
hr = RegEnumKey(hSrcKey, i, Name, MAX_PATH);
}
RegCloseKey(hSrcKey);
//delete key
lpSubKey = MENU_ORDER_KEY;
lpSubKey += source;
RegDeleteKey(HKEY_CURRENT_USER, lpSubKey);
}
}
void CFavTreeCtrl::OnAddtoGroup(UINT nID)
{
int d = GetItemData(m_hItem);
if(d>=0)
{
CString url, name;
POSITION pos = m_astrFav.FindIndex(d);
if(pos!=NULL)
GetURL(m_astrFav.GetAt(pos), url);
name = GetItemText(m_hItem);
((CMainFrame*)pMainFrame)->AddtoGroup(name, url, nID);
}
}
void CFavTreeCtrl::OnRfavfolderReset()
{
// TODO: Add your command handler code here
CString strName;
strName = GetItemText(m_hItem);
CString lpSubKey;
GetItemPath(m_hItem, lpSubKey);
lpSubKey = MENU_ORDER_KEY + lpSubKey;
if(m_hItem != GetRootItem())
lpSubKey += strName;
HKEY hKey;
HRESULT hr = RegOpenKey(HKEY_CURRENT_USER, lpSubKey, &hKey);
if(hr == ERROR_SUCCESS)
{
RegDeleteValue(hKey, "Order");
RegCloseKey(hKey);
}
//update tree
if(m_hItem == GetRootItem())
{
((CFavTree*)m_pFavTree)->Update(1);
}
else if(ItemHasChildren(m_hItem))
{
Expand(m_hItem, TVE_COLLAPSE);
HTREEITEM hItem, hItem2;
hItem = GetChildItem(m_hItem);
while(hItem)
{
hItem2 = hItem;
hItem = GetNextSiblingItem(hItem);
DeleteItem(hItem2);
}
}
//update menu
((CMainFrame*)pMainFrame)->m_bFavDirty = TRUE;
}
void CFavTreeCtrl::OnRfavRename()
{
// TODO: Add your command handler code here
EditLabel(m_hItem);
}
void CFavTreeCtrl::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
{
TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
// TODO: Add your control notification handler code here
CString strName;
strName = GetItemText(pTVDispInfo->item.hItem);
CString path,szPath;
if(!((CMainFrame*)pMainFrame)->GetFavoriteFolder(szPath))
return ;
GetItemPath(m_hItem, path);
path = "\\"+path;
path = szPath+ path;
CString str;
GetEditControl()->GetWindowText(str);
str.MakeLower();
str.TrimLeft();
str.TrimRight();
SetItemText(pTVDispInfo->item.hItem, str);
if(!MoveFile(path+strName+".url", path+str+".url"))
MoveFile(path+strName+".lnk", path+str+".lnk");
//update menu
((CMainFrame*)pMainFrame)->m_bFavDirty = TRUE;
*pResult = 0;
}
#pragma optimize( "s", off)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -