⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbview.c

📁 可以使用VC编译的USB view源码。对usb调试有指导意义
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++

Copyright (c) 1997-1998 Microsoft Corporation

Module Name:

    USBVIEW.C

Abstract:

    This is the GUI goop for the USBVIEW application.

Environment:

    user mode

Revision History:

    04-25-97 : created

--*/

//*****************************************************************************
// I N C L U D E S
//*****************************************************************************

#include <windows.h>
#include <basetyps.h>
#include <windowsx.h>
#include <initguid.h>
#include <devioctl.h>
#include <usbioctl.h>
#include <dbt.h>
#include <stdio.h>

#include "resource.h"
#include "usbview.h"

//*****************************************************************************
// D E F I N E S
//*****************************************************************************

// window control defines
//
#define SIZEBAR             0
#define WINDOWSCALEFACTOR   15

//*****************************************************************************
// L O C A L    F U N C T I O N    P R O T O T Y P E S
//*****************************************************************************

int WINAPI
WinMain (
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpszCmdLine,
    int       nCmdShow
);

BOOL
CreateMainWindow (
    int nCmdShow
);

VOID
ResizeWindows (
    BOOL    bSizeBar,
    int     BarLocation
);

LRESULT CALLBACK
MainDlgProc (
    HWND   hwnd,
    UINT   uMsg,
    WPARAM wParam,
    LPARAM lParam
);

BOOL
USBView_OnInitDialog (
    HWND    hWnd,
    HWND    hWndFocus,
    LPARAM  lParam
);

VOID
USBView_OnClose (
    HWND hWnd
);

VOID
USBView_OnCommand (
    HWND hWnd,
    int  id,
    HWND hwndCtl,
    UINT codeNotify
);

VOID
USBView_OnLButtonDown (
    HWND hWnd,
    BOOL fDoubleClick,
    int  x,
    int  y,
    UINT keyFlags
);

VOID
USBView_OnLButtonUp (
    HWND hWnd,
    int  x,
    int  y,
    UINT keyFlags
);

VOID
USBView_OnMouseMove (
    HWND hWnd,
    int  x,
    int  y,
    UINT keyFlags
);

VOID
USBView_OnSize (
    HWND hWnd,
    UINT state,
    int  cx,
    int  cy
);

LRESULT
USBView_OnNotify (
    HWND    hWnd,
    int     DlgItem,
    LPNMHDR lpNMHdr
);

BOOL
USBView_OnDeviceChange (
    HWND  hwnd,
    UINT  uEvent,
    DWORD dwEventData
);


VOID DestroyTree (VOID);

VOID RefreshTree (VOID);

LRESULT CALLBACK
AboutDlgProc (
    HWND   hwnd,
    UINT   uMsg,
    WPARAM wParam,
    LPARAM lParam
);

VOID
WalkTree (
    HTREEITEM        hTreeItem,
    LPFNTREECALLBACK lpfnTreeCallback,
    DWORD            dwRefData
);

VOID
ExpandItem (
    HWND      hTreeWnd,
    HTREEITEM hTreeItem
);

//*****************************************************************************
// G L O B A L S    P R I V A T E    T O    T H I S    F I L E
//*****************************************************************************

HINSTANCE       ghInstance;
HWND            ghMainWnd;
HMENU           ghMainMenu;
HWND            ghTreeWnd;
HWND            ghEditWnd;
HWND            ghStatusWnd;
HCURSOR         ghSplitCursor;

int             gBarLocation    = 0;
BOOL            gbButtonDown    = FALSE;
HTREEITEM       ghTreeRoot      = NULL;

BOOL            gDoAutoRefresh  = FALSE;
BOOL            gDoConfigDesc   = FALSE;

// added
int             giGoodDevice;
int             giBadDevice;
int             giComputer;
int             giHub;
int             giNoDevice;
HDEVNOTIFY      gNotifyDevHandle;
HDEVNOTIFY      gNotifyHubHandle;


//*****************************************************************************
//
// WinMain()
//
//*****************************************************************************

int WINAPI
WinMain (
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpszCmdLine,
    int       nCmdShow
)
{
    MSG     msg;
    HACCEL  hAccel;

    ghInstance = hInstance;

    ghSplitCursor = LoadCursor(ghInstance,
                               MAKEINTRESOURCE(IDC_SPLIT));

    if (!ghSplitCursor)
    {
        OOPS();
        return 0;
    }

    hAccel = LoadAccelerators(ghInstance,
                              MAKEINTRESOURCE(IDACCEL));

    if (!hAccel)
    {
        OOPS();
        return 0;
    }

    if (!CreateTextBuffer())
    {
        return 0;
    }

    if (!CreateMainWindow(nCmdShow))
    {
        return 0;
    }

    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(ghMainWnd,
                                  hAccel,
                                  &msg) &&
            !IsDialogMessage(ghMainWnd,
                             &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    DestroyTextBuffer();

    CHECKFORLEAKS();

    return 1;
}

//*****************************************************************************
//
// CreateMainWindow()
//
//*****************************************************************************

BOOL
CreateMainWindow (
    int nCmdShow
)
{
    RECT rc;

    InitCommonControls();

    ghMainWnd = CreateDialog(ghInstance,
                             MAKEINTRESOURCE(IDD_MAINDIALOG),
                             NULL,
                             MainDlgProc);

    if (ghMainWnd == NULL)
    {
        OOPS();
        return FALSE;
    }

    GetWindowRect(ghMainWnd, &rc);

    gBarLocation = (rc.right - rc.left) / 3;

    ResizeWindows(FALSE, 0);

    ShowWindow(ghMainWnd, nCmdShow);

    UpdateWindow(ghMainWnd);

    return TRUE;
}


//*****************************************************************************
//
// ResizeWindows()
//
// Handles resizing the two child windows of the main window.  If
// bSizeBar is true, then the sizing is happening because the user is
// moving the bar.  If bSizeBar is false, the sizing is happening
// because of the WM_SIZE or something like that.
//
//*****************************************************************************

VOID
ResizeWindows (
    BOOL    bSizeBar,
    int     BarLocation
)
{
    RECT    MainClientRect;
    RECT    MainWindowRect;
    RECT    TreeWindowRect;
    RECT    StatusWindowRect;
    int     right;

    // Is the user moving the bar?
    //
    if (!bSizeBar)
    {
        BarLocation = gBarLocation;
    }

    GetClientRect(ghMainWnd, &MainClientRect);

    GetWindowRect(ghStatusWnd, &StatusWindowRect);

    // Make sure the bar is in a OK location
    //
    if (bSizeBar)
    {
        if (BarLocation <
            GetSystemMetrics(SM_CXSCREEN)/WINDOWSCALEFACTOR)
        {
            return;
        }

        if ((MainClientRect.right - BarLocation) <
            GetSystemMetrics(SM_CXSCREEN)/WINDOWSCALEFACTOR)
        {
            return;
        }
    }

    // Save the bar location
    //
    gBarLocation = BarLocation;

    // Move the tree window
    //
    MoveWindow(ghTreeWnd,
               0,
               0,
               BarLocation,
               MainClientRect.bottom - StatusWindowRect.bottom + StatusWindowRect.top,
               TRUE);

    // Get the size of the window (in case move window failed
    //
    GetWindowRect(ghTreeWnd, &TreeWindowRect);
    GetWindowRect(ghMainWnd, &MainWindowRect);

    right = TreeWindowRect.right - MainWindowRect.left;

    // Move the edit window with respect to the tree window
    //
    MoveWindow(ghEditWnd,
               right+SIZEBAR,
               0,
               MainClientRect.right-(right+SIZEBAR),
               MainClientRect.bottom - StatusWindowRect.bottom + StatusWindowRect.top,
               TRUE);

    // Move the Status window with respect to the tree window
    //
    MoveWindow(ghStatusWnd,
               0,
               MainClientRect.bottom - StatusWindowRect.bottom + StatusWindowRect.top,
               MainClientRect.right,
               StatusWindowRect.bottom - StatusWindowRect.top,
               TRUE);
}


//*****************************************************************************
//
// MainWndProc()
//
//*****************************************************************************

LRESULT CALLBACK
MainDlgProc (
    HWND   hWnd,
    UINT   uMsg,
    WPARAM wParam,
    LPARAM lParam
)
{

    switch (uMsg)
    {        

        HANDLE_MSG(hWnd, WM_INITDIALOG,     USBView_OnInitDialog);
        HANDLE_MSG(hWnd, WM_CLOSE,          USBView_OnClose);
        HANDLE_MSG(hWnd, WM_COMMAND,        USBView_OnCommand);
        HANDLE_MSG(hWnd, WM_LBUTTONDOWN,    USBView_OnLButtonDown);
        HANDLE_MSG(hWnd, WM_LBUTTONUP,      USBView_OnLButtonUp);
        HANDLE_MSG(hWnd, WM_MOUSEMOVE,      USBView_OnMouseMove);
        HANDLE_MSG(hWnd, WM_SIZE,           USBView_OnSize);
        HANDLE_MSG(hWnd, WM_NOTIFY,         USBView_OnNotify);
        HANDLE_MSG(hWnd, WM_DEVICECHANGE,   USBView_OnDeviceChange);
    }

    return 0;
}

//*****************************************************************************
//
// USBView_OnInitDialog()
//
//*****************************************************************************

BOOL
USBView_OnInitDialog (
    HWND    hWnd,
    HWND    hWndFocus,
    LPARAM  lParam
)
{
    HFONT                           hFont;
    HIMAGELIST                      himl;
    HICON                           hicon;
    DEV_BROADCAST_DEVICEINTERFACE   broadcastInterface;    


    // Register to receive notification when a USB device is plugged in.
    broadcastInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    broadcastInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

    memcpy( &(broadcastInterface.dbcc_classguid),
            &(GUID_CLASS_USB_DEVICE),
            sizeof(struct _GUID));
    
    gNotifyDevHandle = RegisterDeviceNotification(hWnd,
                                                  &broadcastInterface,
                                                  DEVICE_NOTIFY_WINDOW_HANDLE);

    // Now register for Hub notifications.
    memcpy( &(broadcastInterface.dbcc_classguid),
            &(GUID_CLASS_USBHUB),
            sizeof(struct _GUID));
    
    gNotifyHubHandle = RegisterDeviceNotification(hWnd,
                                                  &broadcastInterface,
                                                  DEVICE_NOTIFY_WINDOW_HANDLE);
                                               
    //end add    

    ghTreeWnd = GetDlgItem(hWnd, IDC_TREE);

    //added
    if ((himl = ImageList_Create(15, 15,
                                 FALSE, 2, 0)) == NULL)
    {
        OOPS();
    }

    hicon = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_ICON));
    giGoodDevice = ImageList_AddIcon(himl, hicon);
    
    hicon = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_BADICON));
    giBadDevice = ImageList_AddIcon(himl, hicon);

    hicon = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_COMPUTER));
    giComputer = ImageList_AddIcon(himl, hicon);

    hicon = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_HUB));
    giHub = ImageList_AddIcon(himl, hicon);

    hicon = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_NODEVICE));
    giNoDevice = ImageList_AddIcon(himl, hicon);
    

    TreeView_SetImageList(ghTreeWnd, himl, TVSIL_NORMAL);
    // end add


    ghEditWnd = GetDlgItem(hWnd, IDC_EDIT);

    ghStatusWnd = GetDlgItem(hWnd, IDC_STATUS);

    ghMainMenu = GetMenu(hWnd);

    if (ghMainMenu == NULL)
    {
        OOPS();
    }

    hFont  = CreateFont(13,  8, 0, 0,
                        400, 0, 0, 0,
                        0,   1, 2, 1,
                        49,  TEXT("Courier"));

    SendMessage(ghEditWnd,
                WM_SETFONT,
                (WPARAM) hFont,
                0);

    RefreshTree();    

    return FALSE;
}

//*****************************************************************************
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -