📄 regview.cpp
字号:
WS_VISIBLE | WS_CHILD | WS_VSCROLL |
WS_BORDER | LVS_REPORT,
prect->left, prect->top,
prect->right - prect->left,
prect->bottom - prect->top,
hWnd, (HMENU)ID_LISTV,
hInst, NULL);
// Add columns.
if (hwndLV) {
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM |
LVCF_ORDER;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 120;
lvc.pszText = TEXT ("Name");
lvc.iOrder = 0;
lvc.iSubItem = 0;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc);
lvc.mask |= LVCF_SUBITEM;
lvc.pszText = TEXT ("Data");
lvc.cx = 250;
lvc.iOrder = 1;
lvc.iSubItem = 1;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc);
}
return hwndLV;
}
//----------------------------------------------------------------------
// InitTreeView - Initialize tree view control.
//
HWND CreateTV (HWND hWnd, RECT *prect) {
HBITMAP hBmp;
HIMAGELIST himl;
HWND hwndTV;
// Create tree view. Size it so that it fits under
// the command bar and fills the left part of the client area.
hwndTV = CreateWindowEx (0, WC_TREEVIEW,
TEXT (""), WS_VISIBLE | WS_CHILD | WS_VSCROLL |
WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS |
TVS_LINESATROOT, prect->left, prect->top,
prect->right, prect->bottom,
hWnd, (HMENU)ID_TREEV, hInst, NULL);
if (!IsWindow (hwndTV))
return 0;
// Create image list control for tree view icons.
himl = ImageList_Create (16, 16, ILC_COLOR, 2, 0);
// Load first two images from one bitmap.
hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (ID_BMPS));
ImageList_Add (himl, hBmp, NULL);
DeleteObject (hBmp);
TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
return hwndTV;
}
//----------------------------------------------------------------------
// InsertLV - Add an item to the list view control.
//
int InsertLV (HWND hWnd, int nItem, LPTSTR pszName, LPTSTR pszData) {
HWND hwndLV = GetDlgItem (hWnd, ID_LISTV);
LVITEM lvi;
int rc;
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
lvi.iItem = nItem;
lvi.iSubItem = 0;
lvi.pszText = pszName;
lvi.iImage = 0;
lvi.lParam = nItem;
rc = SendMessage (hwndLV, LVM_INSERTITEM, 0, (LPARAM)&lvi);
lvi.mask = LVIF_TEXT;
lvi.iItem = nItem;
lvi.iSubItem = 1;
lvi.pszText = pszData;
rc = SendMessage (hwndLV, LVM_SETITEM, 0, (LPARAM)&lvi);
return 0;
}
//----------------------------------------------------------------------
// InsertTV - Insert item into tree view control.
//
HTREEITEM InsertTV (HWND hWnd, HTREEITEM hParent, TCHAR *pszName,
LPARAM lParam, DWORD nChildren) {
TV_INSERTSTRUCT tvis;
HWND hwndTV = GetDlgItem (hWnd, ID_TREEV);
// Initialize the insertstruct.
memset (&tvis, 0, sizeof (tvis));
tvis.hParent = hParent;
tvis.hInsertAfter = TVI_LAST;
tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_CHILDREN |
TVIF_IMAGE;
tvis.item.pszText = pszName;
tvis.item.cchTextMax = lstrlen (pszName);
tvis.item.iImage = 1;
tvis.item.iSelectedImage = 1;
tvis.item.lParam = lParam;
if (nChildren)
tvis.item.cChildren = 1;
else
tvis.item.cChildren = 0;
return TreeView_InsertItem (hwndTV, &tvis);
}
//----------------------------------------------------------------------
// GetTree - Compute the full path of the tree view item.
//
int GetTree (HWND hWnd, HTREEITEM hItem, HKEY *pRoot, TCHAR *pszKey,
int *pnMax) {
TV_ITEM tvi;
TCHAR szName[256];
HTREEITEM hParent;
HWND hwndTV = GetDlgItem (hWnd, ID_TREEV);
memset (&tvi, 0, sizeof (tvi));
hParent = TreeView_GetParent (hwndTV, hItem);
if (hParent) {
// Get the parent of the parent of the...
GetTree (hWnd, hParent, pRoot, pszKey, pnMax);
// Get the name of the item.
tvi.mask = TVIF_TEXT;
tvi.hItem = hItem;
tvi.pszText = szName;
tvi.cchTextMax = dim(szName);
TreeView_GetItem (hwndTV, &tvi);
StringCchCat (pszKey, *pnMax, TEXT ("\\"));
(*pnMax)--;
StringCchCat (pszKey, *pnMax, szName);
(*pnMax) -= lstrlen(szName);
} else {
*pszKey = TEXT ('\0');
szName[0] = TEXT ('\0');
// Get the name of the item.
tvi.mask = TVIF_TEXT | TVIF_PARAM;
tvi.hItem = hItem;
tvi.pszText = szName;
tvi.cchTextMax = dim(szName);
if (TreeView_GetItem (hwndTV, &tvi))
*pRoot = (HKEY)tvi.lParam;
else {
int rc = GetLastError();
}
}
return 0;
}
//----------------------------------------------------------------------
// DisplayValue - Display the data, depending on the type.
//
int DisplayValue (HWND hWnd, int nCnt, LPTSTR pszName, PBYTE pbData,
DWORD dwDSize, DWORD dwType) {
TCHAR szData[512];
int i, len, len1;
switch (dwType) {
case REG_MULTI_SZ:
len = dim (szData);
szData[0] = TEXT('\0');
while ((*pbData != 0) && (len > 0)) {
len1 = lstrlen ((LPTSTR)pbData);
if (FAILED (StringCchCat (szData, len, (LPTSTR)pbData)))
break;
len -= len1;
pbData += (len1+1) * sizeof (TCHAR);
if (*pbData == 0)
break;
if (FAILED (StringCchCat (szData, len, TEXT(","))))
break;
len--;
}
break;
case REG_EXPAND_SZ:
case REG_SZ:
StringCchCopy (szData, dim (szData), (LPTSTR)pbData);
break;
case REG_DWORD:
wsprintf (szData, TEXT ("%X"), *(int *)pbData);
break;
case REG_BINARY:
szData[0] = TEXT ('\0');
for (i = 0; i < (int)dwDSize; i++) {
len = lstrlen (szData);
wsprintf (&szData[len], TEXT ("%02X "), pbData[i]);
if (len > dim(szData) - 6)
break;
}
break;
default:
wsprintf (szData, TEXT ("Unknown type: %x"), dwType);
}
InsertLV (hWnd, nCnt, pszName, szData);
return 0;
}
//----------------------------------------------------------------------
// EnumValues - Enumerate each of the values of a key.
//
int EnumValues (HWND hWnd, HKEY hRoot, LPTSTR pszKey) {
int nCnt = 0, rc;
DWORD dwNSize, dwDSize, dwType;
TCHAR szName[MAX_PATH];
BYTE bData[1024];
HKEY hKey;
if (lstrlen (pszKey)) {
if (RegOpenKeyEx (hRoot, pszKey, 0, 0, &hKey) != ERROR_SUCCESS)
return 0;
} else
hKey = hRoot;
// Clean out list view.
ListView_DeleteAllItems (GetDlgItem (hWnd, ID_LISTV));
// Enumerate the values in the list view control.
nCnt = 0;
dwNSize = dim(szName);
dwDSize = dim(bData);
rc = RegEnumValue (hKey, nCnt, szName, &dwNSize,
NULL, &dwType, bData, &dwDSize);
while (rc == ERROR_SUCCESS) {
// Display the value in the list view control.
DisplayValue (hWnd, nCnt, szName, bData, dwDSize, dwType);
dwNSize = dim(szName);
dwDSize = dim(bData);
nCnt++;
rc = RegEnumValue (hKey, nCnt, szName, &dwNSize,
NULL, &dwType, bData, &dwDSize);
}
if (hKey != hRoot)
RegCloseKey (hKey);
return 1;
}
//----------------------------------------------------------------------
// CountChildren - Count the number of children of a key.
//
DWORD CountChildren (HKEY hRoot, LPTSTR pszKeyPath, LPTSTR pszKey,
int nPathMax) {
TCHAR *pEnd;
DWORD dwCnt;
HKEY hKey;
size_t dwLen;
// Safe lstrlen
if (FAILED(StringCchLength (pszKeyPath, nPathMax, &dwLen)))
return 0;
pEnd = pszKeyPath + dwLen;
StringCchCopy (pEnd, nPathMax - dwLen, TEXT("\\"));
StringCchCopy (pEnd, nPathMax - dwLen - 1, pszKey);
if (RegOpenKeyEx(hRoot, pszKeyPath, 0, 0, &hKey) ==
ERROR_SUCCESS) {
RegQueryInfoKey (hKey, NULL, NULL, 0, &dwCnt, NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
RegCloseKey (hKey);
}
*pEnd = TEXT ('\0');
return dwCnt;
}
//----------------------------------------------------------------------
// EnumChildren - Enumerate the child keys of a key.
//
int EnumChildren (HWND hWnd, HTREEITEM hParent, HKEY hRoot,
LPTSTR pszKey, int nKeyMax) {
int i = 0, rc;
DWORD dwNSize;
DWORD dwCSize;
TCHAR szName[MAX_PATH];
TCHAR szClass[256];
FILETIME ft;
DWORD nChild;
HKEY hKey;
TVITEM tvi;
// All keys but root need to be opened.
if (*pszKey != TEXT('\0')) {
if (RegOpenKeyEx (hRoot, pszKey, 0, 0, &hKey) != ERROR_SUCCESS) {
rc = GetLastError();
return 0;
}
} else
hKey = hRoot;
dwNSize = dim(szName);
dwCSize = dim(szClass);
rc = RegEnumKeyEx (hKey, i, szName, &dwNSize, NULL,
szClass, &dwCSize, &ft);
while (rc == ERROR_SUCCESS) {
nChild = CountChildren (hRoot, pszKey, szName, nKeyMax);
// Add key to tree view.
InsertTV (hWnd, hParent, szName, 0, nChild);
dwNSize = dim(szName);
rc = RegEnumKeyEx (hKey, ++i, szName, &dwNSize,
NULL, NULL, 0, &ft);
}
// If this wasn't the root key, close it.
if (hKey != hRoot)
RegCloseKey (hKey);
// If no children, remove expand button.
if (i == 0) {
tvi.hItem = hParent;
tvi.mask = TVIF_CHILDREN;
tvi.cChildren = 0;
TreeView_SetItem (GetDlgItem (hWnd, ID_TREEV), &tvi);
}
return i;
}
//======================================================================
// About Dialog procedure
//
BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDOK:
case IDCANCEL:
EndDialog (hWnd, 0);
return TRUE;
}
break;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -