📄 usbview.c
字号:
// USBView_OnClose()
//
//*****************************************************************************
VOID
USBView_OnClose (
HWND hWnd
)
{
DestroyTree();
PostQuitMessage(0);
EndDialog(hWnd, 0);
}
//*****************************************************************************
//
// USBView_OnCommand()
//
//*****************************************************************************
VOID
USBView_OnCommand (
HWND hWnd,
int id,
HWND hwndCtl,
UINT codeNotify
)
{
MENUITEMINFO menuInfo;
switch (id)
{
case ID_AUTO_REFRESH:
gDoAutoRefresh = !gDoAutoRefresh;
menuInfo.cbSize = sizeof(menuInfo);
menuInfo.fMask = MIIM_STATE;
menuInfo.fState = gDoAutoRefresh ? MFS_CHECKED : MFS_UNCHECKED;
SetMenuItemInfo(ghMainMenu,
id,
FALSE,
&menuInfo);
break;
case ID_CONFIG_DESCRIPTORS:
gDoConfigDesc = !gDoConfigDesc;
menuInfo.cbSize = sizeof(menuInfo);
menuInfo.fMask = MIIM_STATE;
menuInfo.fState = gDoConfigDesc ? MFS_CHECKED : MFS_UNCHECKED;
SetMenuItemInfo(ghMainMenu,
id,
FALSE,
&menuInfo);
break;
case ID_ABOUT:
DialogBox(ghInstance,
MAKEINTRESOURCE(IDD_ABOUT),
ghMainWnd,
AboutDlgProc);
break;
case ID_EXIT:
UnregisterDeviceNotification(gNotifyDevHandle);
UnregisterDeviceNotification(gNotifyHubHandle);
DestroyTree();
PostQuitMessage(0);
EndDialog(hWnd, 0);
break;
case ID_REFRESH:
RefreshTree();
break;
}
}
//*****************************************************************************
//
// USBView_OnLButtonDown()
//
//*****************************************************************************
VOID
USBView_OnLButtonDown (
HWND hWnd,
BOOL fDoubleClick,
int x,
int y,
UINT keyFlags
)
{
gbButtonDown = TRUE;
SetCapture(hWnd);
}
//*****************************************************************************
//
// USBView_OnLButtonUp()
//
//*****************************************************************************
VOID
USBView_OnLButtonUp (
HWND hWnd,
int x,
int y,
UINT keyFlags
)
{
gbButtonDown = FALSE;
ReleaseCapture();
}
//*****************************************************************************
//
// USBView_OnMouseMove()
//
//*****************************************************************************
VOID
USBView_OnMouseMove (
HWND hWnd,
int x,
int y,
UINT keyFlags
)
{
SetCursor(ghSplitCursor);
if (gbButtonDown)
{
ResizeWindows(TRUE, x);
}
}
//*****************************************************************************
//
// USBView_OnSize();
//
//*****************************************************************************
VOID
USBView_OnSize (
HWND hWnd,
UINT state,
int cx,
int cy
)
{
ResizeWindows(FALSE, 0);
}
//*****************************************************************************
//
// USBView_OnNotify()
//
//*****************************************************************************
LRESULT
USBView_OnNotify (
HWND hWnd,
int DlgItem,
LPNMHDR lpNMHdr
)
{
if (lpNMHdr->code == TVN_SELCHANGED)
{
HTREEITEM hTreeItem;
hTreeItem = ((NM_TREEVIEW *)lpNMHdr)->itemNew.hItem;
if (hTreeItem)
{
UpdateEditControl(ghEditWnd,
ghTreeWnd,
hTreeItem);
}
}
return 0;
}
//*****************************************************************************
//
// USBView_OnDeviceChange()
//
//*****************************************************************************
BOOL
USBView_OnDeviceChange (
HWND hwnd,
UINT uEvent,
DWORD dwEventData
)
{
if (gDoAutoRefresh)
{
switch (uEvent)
{
case DBT_DEVICEARRIVAL:
case DBT_DEVICEREMOVECOMPLETE:
RefreshTree();
break;
}
}
return TRUE;
}
//*****************************************************************************
//
// DestroyTree()
//
//*****************************************************************************
VOID DestroyTree (VOID)
{
// Clear the selection of the TreeView, so that when the tree is
// destroyed, the control won't try to constantly "shift" the
// selection to another item.
//
TreeView_SelectItem(ghTreeWnd, NULL);
// Destroy the current contents of the TreeView
//
if (ghTreeRoot)
{
WalkTree(ghTreeRoot, CleanupItem, 0);
TreeView_DeleteAllItems(ghTreeWnd);
ghTreeRoot = NULL;
}
}
//*****************************************************************************
//
// RefreshTree()
//
//*****************************************************************************
VOID RefreshTree (VOID)
{
CHAR statusText[128];
ULONG devicesConnected;
// Clear the selection of the TreeView, so that when the tree is
// destroyed, the control won't try to constantly "shift" the
// selection to another item.
//
TreeView_SelectItem(ghTreeWnd, NULL);
// Clear the edit control
//
SetWindowText(ghEditWnd, "");
// Destroy the current contents of the TreeView
//
if (ghTreeRoot)
{
WalkTree(ghTreeRoot, CleanupItem, 0);
TreeView_DeleteAllItems(ghTreeWnd);
ghTreeRoot = NULL;
}
// Create the root tree node
//
ghTreeRoot = AddLeaf(TVI_ROOT, 0, "My Computer");
if (ghTreeRoot != NULL)
{
// Enumerate all USB buses and populate the tree
//
EnumerateHostControllers(ghTreeRoot, &devicesConnected);
//
// Expand all tree nodes
//
WalkTree(ghTreeRoot, ExpandItem, 0);
// Update Status Line with number of devices connected
//
wsprintf(statusText, "Devices Connected: %d Hubs Connected: %d",
devicesConnected, TotalHubs);
SetWindowText(ghStatusWnd, statusText);
}
else
{
OOPS();
}
}
//*****************************************************************************
//
// AboutDlgProc()
//
//*****************************************************************************
LRESULT CALLBACK
AboutDlgProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch (uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
EndDialog (hwnd, 0);
break;
}
break;
}
return FALSE;
}
//*****************************************************************************
//
// AddLeaf()
//
//*****************************************************************************
HTREEITEM
AddLeaf (
HTREEITEM hTreeParent,
LPARAM lParam,
LPTSTR lpszText,
...
)
{
TCHAR szBuffer[1024];
va_list list;
TV_INSERTSTRUCT tvins;
HTREEITEM hti;
// added for tree view icons
PUSB_NODE_CONNECTION_INFORMATION ConnectInfo = NULL;
if (lParam)
{
ConnectInfo = ((PUSBDEVICEINFO)lParam)->ConnectionInfo;
}
// end add
va_start(list, lpszText);
vsprintf(szBuffer,
lpszText,
list);
memset(&tvins, 0, sizeof(tvins));
// Set the parent item
//
tvins.hParent = hTreeParent;
tvins.hInsertAfter = TVI_LAST;
// pszText and lParam members are valid
//
tvins.item.mask = TVIF_TEXT | TVIF_PARAM;
// Set the text of the item.
//
tvins.item.pszText = szBuffer;
// Set the user context item
//
tvins.item.lParam = lParam;
// Add the item to the tree-view control.
//
hti = TreeView_InsertItem(ghTreeWnd, &tvins);
// added
tvins.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvins.item.hItem = hti;
// Determine which icon to display for the device
tvins.item.iImage = giGoodDevice;
tvins.item.iSelectedImage = giGoodDevice;
/* if (!lParam)
{
if (!(strcmp("My Computer", lpszText)))
{
tvins.item.iImage = giComputer;
tvins.item.iSelectedImage = giComputer;
}
else
{
//it is the host controller
}
} */
if (!ConnectInfo)
{
if (!(strcmp("My Computer", lpszText)))
{
tvins.item.iImage = giComputer;
tvins.item.iSelectedImage = giComputer;
}
else
{
//it is the host controller
}
}
else
{
if (ConnectInfo->DeviceIsHub) // Device is a Hub!!
{
tvins.item.iImage = giHub;
tvins.item.iSelectedImage = giHub;
}
if (!ConnectInfo->CurrentConfigurationValue)
{
tvins.item.iImage = giBadDevice;
tvins.item.iSelectedImage = giBadDevice;
if (NoDeviceConnected == ConnectInfo->ConnectionStatus) // Empty Port
{
tvins.item.iImage = giNoDevice;
tvins.item.iSelectedImage = giNoDevice;
}
}
}
TreeView_SetItem(ghTreeWnd, &tvins.item);
return hti;
}
//*****************************************************************************
//
// WalkTree()
//
//*****************************************************************************
VOID
WalkTree (
HTREEITEM hTreeItem,
LPFNTREECALLBACK lpfnTreeCallback,
DWORD dwRefData
)
{
if (hTreeItem)
{
// Recursively call WalkTree on the node's first child.
//
WalkTree(TreeView_GetChild(ghTreeWnd, hTreeItem),
lpfnTreeCallback,
dwRefData);
//
// Call the lpfnCallBack on the node itself.
//
(*lpfnTreeCallback)(ghTreeWnd, hTreeItem);
//
//
// Recursively call WalkTree on the node's first sibling.
//
WalkTree(TreeView_GetNextSibling(ghTreeWnd, hTreeItem),
lpfnTreeCallback,
dwRefData);
}
}
//*****************************************************************************
//
// ExpandItem()
//
//*****************************************************************************
VOID
ExpandItem (
HWND hTreeWnd,
HTREEITEM hTreeItem
)
{
//
// Make this node visible.
//
TreeView_Expand(hTreeWnd, hTreeItem, TVE_EXPAND);
}
#if DBG
//*****************************************************************************
//
// Oops()
//
//*****************************************************************************
VOID
Oops
(
CHAR *File,
ULONG Line
)
{
char szBuf[256];
wsprintf(szBuf, "File: %s, Line %d", File, Line);
MessageBox(ghMainWnd, szBuf, "Oops!", MB_OK);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -