📄 treeview.c
字号:
item.mask = TVIF_CHILDREN | TVIF_HANDLE;
item.hItem = hItem;
item.cChildren = 1;
if (!TreeView_SetItem(hwndTV, &item))
return FALSE;
}
(void)TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
if (!hNewItem)
{
for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem))
{
item.mask = TVIF_HANDLE | TVIF_TEXT;
item.hItem = hNewItem;
item.pszText = buf;
item.cchTextMax = COUNT_OF(buf);
if (!TreeView_GetItem(hwndTV, &item)) continue;
if (lstrcmp(name, item.pszText) == 0) break;
}
}
if (hNewItem) (void)TreeView_SelectItem(hwndTV, hNewItem);
return hNewItem;
}
HWND StartKeyRename(HWND hwndTV)
{
HTREEITEM hItem;
if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
return TreeView_EditLabel(hwndTV, hItem);
}
static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
{
TVITEM tvi;
TVINSERTSTRUCT tvins;
HTREEITEM hRoot;
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
/* Set the text of the item. */
tvi.pszText = pHostName;
tvi.cchTextMax = lstrlen(tvi.pszText);
/* Assume the item is not a parent item, so give it an image. */
tvi.iImage = Image_Root;
tvi.iSelectedImage = Image_Root;
tvi.cChildren = 5;
/* Save the heading level in the item's application-defined data area. */
tvi.lParam = (LPARAM)NULL;
tvins.item = tvi;
tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
tvins.hParent = TVI_ROOT;
/* Add the item to the tree view control. */
if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
if (GetVersion() & 0x80000000)
{
/* Win9x specific key */
if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
}
/* expand and select host name */
(void)TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
(void)TreeView_Select(hwndTV, hRoot, TVGN_CARET);
return TRUE;
}
/*
* InitTreeViewImageLists - creates an image list, adds three bitmaps
* to it, and associates the image list with a tree view control.
* Returns TRUE if successful, or FALSE otherwise.
* hwndTV - handle to the tree view control.
*/
static BOOL InitTreeViewImageLists(HWND hwndTV)
{
HIMAGELIST himl; /* handle to image list */
HICON hico; /* handle to icon */
/* Create the image list. */
if ((himl = ImageList_Create(CX_ICON, CY_ICON,
ILC_MASK|ILC_COLOR32, 0, NUM_ICONS)) == NULL)
return FALSE;
/* Add the open file, closed file, and document bitmaps. */
hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
Image_Open = ImageList_AddIcon(himl, hico);
hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
Image_Closed = ImageList_AddIcon(himl, hico);
hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
Image_Root = ImageList_AddIcon(himl, hico);
/* Fail if not all of the images were added. */
if (ImageList_GetImageCount(himl) < NUM_ICONS)
{
return FALSE;
}
/* Associate the image list with the tree view control. */
(void)TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
return TRUE;
}
BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
{
DWORD dwCount, dwIndex, dwMaxSubKeyLen;
HKEY hRoot, hNewKey, hKey;
LPCTSTR keyPath;
LPTSTR Name;
LONG errCode;
HCURSOR hcursorOld;
static int expanding;
if (expanding) return FALSE;
if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
return TRUE;
}
expanding = TRUE;
hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
if (!keyPath) goto done;
if (*keyPath) {
errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
if (errCode != ERROR_SUCCESS) goto done;
} else {
hNewKey = hRoot;
}
errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
if (errCode != ERROR_SUCCESS) goto done;
dwMaxSubKeyLen++; /* account for the \0 terminator */
Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
if (!Name) goto done;
for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
DWORD cName = dwMaxSubKeyLen, dwSubCount;
errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
if (errCode != ERROR_SUCCESS) continue;
errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
if (errCode == ERROR_SUCCESS) {
errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
RegCloseKey(hKey);
}
if (errCode != ERROR_SUCCESS) dwSubCount = 0;
AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
}
SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM)pnmtv->itemNew.hItem);
RegCloseKey(hNewKey);
HeapFree(GetProcessHeap(), 0, Name);
done:
SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
SetCursor(hcursorOld);
expanding = FALSE;
return TRUE;
}
BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
{
TCHAR szNewKeyFormat[128];
TCHAR szNewKey[128];
LPCTSTR pszKeyPath;
int iIndex = 1;
HKEY hRootKey;
HKEY hKey = NULL;
HKEY hNewKey = NULL;
BOOL bSuccess = FALSE;
LONG lResult;
DWORD dwDisposition;
HTREEITEM hNewItem;
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, hItem, &hRootKey);
if (RegOpenKey(hRootKey, pszKeyPath, &hKey) != ERROR_SUCCESS)
goto done;
if (LoadString(hInst, IDS_NEW_KEY, szNewKeyFormat, sizeof(szNewKeyFormat) / sizeof(szNewKeyFormat[0])) <= 0)
goto done;
/* Need to create a new key with a unique name */
do
{
_sntprintf(szNewKey, sizeof(szNewKey) / sizeof(szNewKey[0]), szNewKeyFormat, iIndex++);
lResult = RegCreateKeyEx(hKey, szNewKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hNewKey, &dwDisposition);
if (hNewKey && (dwDisposition == REG_OPENED_EXISTING_KEY))
{
RegCloseKey(hNewKey);
hNewKey = NULL;
}
}
while(!hNewKey);
/* Insert the new key */
hNewItem = InsertNode(hwndTV, hItem, szNewKey);
if (!hNewItem)
goto done;
/* The new key's name is probably not appropriate yet */
(void)TreeView_EditLabel(hwndTV, hNewItem);
bSuccess = TRUE;
done:
if (hKey)
RegCloseKey(hKey);
if (hNewKey)
RegCloseKey(hNewKey);
return bSuccess;
}
/*
* CreateTreeView - creates a tree view control.
* Returns the handle to the new control if successful, or NULL otherwise.
* hwndParent - handle to the control's parent window.
*/
HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, HMENU id)
{
RECT rcClient;
HWND hwndTV;
/* Get the dimensions of the parent window's client area, and create the tree view control. */
GetClientRect(hwndParent, &rcClient);
hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, NULL,
WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_EDITLABELS,
0, 0, rcClient.right, rcClient.bottom,
hwndParent, id, hInst, NULL);
/* Initialize the image list, and add items to the control. */
if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
DestroyWindow(hwndTV);
return NULL;
}
return hwndTV;
}
void DestroyTreeView() {
if (pathBuffer)
HeapFree(GetProcessHeap(), 0, pathBuffer);
}
BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
{
HTREEITEM hRoot, hItem;
HTREEITEM hChildItem;
TCHAR szPathPart[128];
TCHAR szBuffer[128];
LPCTSTR s;
TVITEM tvi;
/* Total no-good hack */
if (!_tcsncmp(keyPath, _T("My Computer\\"), 12))
keyPath += 12;
hRoot = TreeView_GetRoot(hwndTV);
hItem = hRoot;
while(keyPath[0])
{
s = _tcschr(keyPath, '\\');
lstrcpyn(szPathPart, keyPath, s ? s - keyPath + 1 : _tcslen(keyPath) + 1);
/* Special case for root to expand root key abbreviations */
if (hItem == hRoot)
{
if (!_tcscmp(szPathPart, TEXT("HKCR")))
_tcscpy(szPathPart, TEXT("HKEY_CLASSES_ROOT"));
else if (!_tcscmp(szPathPart, TEXT("HKCU")))
_tcscpy(szPathPart, TEXT("HKEY_CURRENT_USER"));
else if (!_tcscmp(szPathPart, TEXT("HKLM")))
_tcscpy(szPathPart, TEXT("HKEY_LOCAL_MACHINE"));
else if (!_tcscmp(szPathPart, TEXT("HKU")))
_tcscpy(szPathPart, TEXT("HKEY_USERS"));
else if (!_tcscmp(szPathPart, TEXT("HKCC")))
_tcscpy(szPathPart, TEXT("HKEY_CURRENT_CONFIG"));
else if (!_tcscmp(szPathPart, TEXT("HKDD")))
_tcscpy(szPathPart, TEXT("HKEY_DYN_DATA"));
}
for (hChildItem = TreeView_GetChild(hwndTV, hItem); hChildItem;
hChildItem = TreeView_GetNextSibling(hwndTV, hChildItem))
{
memset(&tvi, 0, sizeof(tvi));
tvi.hItem = hChildItem;
tvi.mask = TVIF_TEXT | TVIF_CHILDREN;
tvi.pszText = szBuffer;
tvi.cchTextMax = sizeof(szBuffer) / sizeof(szBuffer[0]);
(void)TreeView_GetItem(hwndTV, &tvi);
if (!_tcscmp(szBuffer, szPathPart))
break;
}
if (!hChildItem)
return FALSE;
if (tvi.cChildren > 0)
{
if (!TreeView_Expand(hwndTV, hChildItem, TVE_EXPAND))
return FALSE;
}
keyPath = s ? s + 1 : _T("");
hItem = hChildItem;
}
(void)TreeView_SelectItem(hwndTV, hItem);
(void)TreeView_EnsureVisible(hwndTV, hItem);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -