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

📄 usbdetectiddlg.cpp

📁 可以检测到插入到USB的设备的插入或者拔出
💻 CPP
字号:
// UsbDetectIDDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UsbDetectID.h"
#include "UsbDetectIDDlg.h"
#include "GUIDEMO.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CPtrArray g_Usb;

VOID	EnumerateHostControllers (ULONG     *DevicesConnected);

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsbDetectIDDlg dialog

CUsbDetectIDDlg::CUsbDetectIDDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUsbDetectIDDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUsbDetectIDDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nTimer = NULL;
}

void CUsbDetectIDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUsbDetectIDDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUsbDetectIDDlg, CDialog)
	//{{AFX_MSG_MAP(CUsbDetectIDDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDC_SHOW, OnShow)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_COMMAND(IDC_CLOSE, OnClose)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_TRAY_NOTIFICATION,OnTrayNotification)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsbDetectIDDlg message handlers

BOOL CUsbDetectIDDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	ReadIni();

	m_nTimer = SetTimer	(1, 2000, NULL);


	//在这里实现了当鼠标放在托盘上时
	//会出现一句话Blhaaa 示范例子它通过调用类CSystemTray 的成员函数来实现的
	//if (start_minimized)
	//PostMessage(WM_CLOSE);//如果鼠标移走,就关闭窗口
	m_trayIcon.Create(this, WM_USER_TRAY_NOTIFICATION, "UsbDetectID", m_hIcon, IDR_TUOPAN);

	CMenu *pSystemMenu=GetSystemMenu(FALSE);
	pSystemMenu->DeleteMenu(3,MF_BYPOSITION);
	pSystemMenu->DeleteMenu(2,MF_BYPOSITION);

	//PostMessage(WM_SYSCOMMAND, SC_CLOSE);

	//CGUIDEMO dlg;
	//int nResponse = dlg.DoModal();

	PostMessage (WM_SYSCOMMAND, SC_CLOSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUsbDetectIDDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		if (!g_bIsOpenAbout)
		{
			g_bIsOpenAbout = TRUE;
			CAboutDlg dlgAbout;
			dlgAbout.DoModal();
			g_bIsOpenAbout = FALSE;
		}
	}
	else if ((nID & 0xFFF0)==SC_CLOSE)
	{
		//OnClose();本来这个是关闭的这里也改为隐藏。
		ShowWindow(SW_HIDE);//系统菜单的关闭也改为隐藏。
	} 
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CUsbDetectIDDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CUsbDetectIDDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//////////////////////////下列托盘显示菜单
LONG CUsbDetectIDDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
{   
    switch ( lparam )// The tray icon sent us a message.  Let's see what it is
    {
        /*case WM_CONTEXTMENU:
            TRACE( "WM_CONTEXTMENU\n" );*/
        case WM_RBUTTONDOWN:
            {// 用户在托盘图标上单击鼠标右键,弹出上下文菜单隐藏/显示对话框。
                CMenu oMenu;
                if (oMenu.LoadMenu(IDR_TUOPAN))        
                {
                    CMenu* pPopup = oMenu.GetSubMenu(0);
                    ASSERT(pPopup != NULL);
                    CPoint oPoint;
					//if (IsWindowVisible())// 根据对话框窗口的显示/隐藏状态修改菜单名称
						//oMenu.ModifyMenu(IDC_SHOW,MF_STRING,IDC_SHOW,"隐藏(&H)");
					//else
						oMenu.ModifyMenu(IDC_SHOW,MF_STRING,IDC_SHOW,"Display");
                    // 确定鼠标位置以便在该位置附近显示菜单
					GetCursorPos( &oPoint );
                    SetForegroundWindow();
                    pPopup->TrackPopupMenu(
                        TPM_LEFTALIGN | TPM_RIGHTBUTTON,
                        oPoint.x, oPoint.y, this); 
                }
            }
            break;
		// 单击/双击鼠标左键均显示出对话框
		case WM_LBUTTONDBLCLK:
			break;
		case WM_LBUTTONDOWN:
			OnHelpAbout();
			//OnShow();
			break;
    }
    return 0;
}

void CUsbDetectIDDlg::OnShow() 
{
	// TODO: Add your command handler code here
	/*
	if (!IsWindowVisible()) 
	{
		ShowWindow(SW_SHOW);
	}
	else
	{
		ShowWindow(SW_HIDE);
	}
	*/

	if (!g_bIsOpened)
	{
		g_bIsOpened = TRUE;

		CGUIDEMO dlg;
		dlg.DoModal();

		g_bIsOpened = FALSE;
	}	
}

void CUsbDetectIDDlg::OnHelpAbout() 
{
	// TODO: Add your command handler code here
	//CAboutDlg   aboutdlg;
	//aboutdlg.DoModal();
	if (!g_bIsOpenAbout)
	{
		g_bIsOpenAbout = TRUE;
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
		g_bIsOpenAbout = FALSE;
	}

}



void CUsbDetectIDDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == m_nTimer)
	{
		SearchID();
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CUsbDetectIDDlg::SearchID()
{
	g_Usb.RemoveAll();

	ULONG devicesConnected;
	EnumerateHostControllers ( &devicesConnected);

	CString strMsg;
	strMsg.Format("Can't find any USB device:\n");

	bool	bFoundedAnyUsb = false;

	for (UINT j = 0; j < g_nTotalDetectNum; ++j)
	{
		bool bFoundThis = false;

		for (UINT i = 0; i < g_Usb.GetSize(); ++i)
		{
			PUSB_NODE_CONNECTION_INFORMATION pConnectInfo = (PUSB_NODE_CONNECTION_INFORMATION)g_Usb[i];

			if (pConnectInfo->ConnectionStatus == DeviceConnected)
			{
				if (pConnectInfo->DeviceDescriptor.idVendor == g_pUsbID[j].m_nVID && 
					pConnectInfo->DeviceDescriptor.idProduct == g_pUsbID[j].m_nPID)
				{
					// 原先没有的,则置成有新发现
					if (!g_pUsbID[j].m_bFound)
					{
						bFoundThis = true;

						g_pUsbID[j].m_bFound = true;
						CString strThisMsg;
						strThisMsg.Format("Vid = 0x%04x, Pid = 0x%04x\n", g_pUsbID[j].m_nVID, g_pUsbID[j].m_nPID);
						strMsg += strThisMsg;
						bFoundedAnyUsb = true;
					}
					// 原先已经有了,不变,表示发现了
					else
					{
						bFoundThis = true;
					}
				}
			}
		}

		// 如果没有找到这一个,则设置为没有
		if (!bFoundThis)
		{
			g_pUsbID[j].m_bFound = false;
		}

	}

	if (bFoundedAnyUsb)
	{
		if (!g_bIsOpened)
		{
			g_bIsOpened = TRUE;

			CGUIDEMO dlg;
			dlg.DoModal();

			g_bIsOpened = FALSE;
		}
	}
}

void CUsbDetectIDDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	PostMessage(WM_SYSCOMMAND, SC_CLOSE);
}

void CUsbDetectIDDlg::OnClose() 
{
	// TODO: Add your command handler code here
	OnOK();
}

⌨️ 快捷键说明

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