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

📄 guidemo.cpp

📁 检测USB设备的PID和UID等信息
💻 CPP
字号:
// GUIDEMO.cpp : implementation file
//

#include "stdafx.h"
#include "usbdetectid.h"
#include "GUIDEMO.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGUIDEMO dialog


CGUIDEMO::CGUIDEMO(CWnd* pParent /*=NULL*/)
	: CBitmapDialog(CGUIDEMO::IDD, pParent)
//	: CBkDialog(CGUIDEMO::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGUIDEMO)
	//}}AFX_DATA_INIT
	//m_bFirstTime = TRUE;

}


void CGUIDEMO::DoDataExchange(CDataExchange* pDX)
{
	CBitmapDialog::DoDataExchange(pDX);
	//CBkDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGUIDEMO)
	DDX_Control(pDX, IDC_BUTTON_BTN9, m_btn9);
	DDX_Control(pDX, IDC_BUTTON_BTN8, m_btn8);
	DDX_Control(pDX, IDC_BUTTON_BTN7, m_btn7);
	DDX_Control(pDX, IDC_BUTTON_BTN6, m_btn6);
	DDX_Control(pDX, IDC_BUTTON_BTN5, m_btn5);
	DDX_Control(pDX, IDC_BUTTON_BTN4, m_btn4);
	DDX_Control(pDX, IDC_BUTTON_BTN3, m_btn3);
	DDX_Control(pDX, IDC_BUTTON_BTN2, m_btn2);
	DDX_Control(pDX, IDC_BUTTON_BTN10, m_btn10);
	DDX_Control(pDX, IDC_BUTTON_BTN1, m_btn1);
	DDX_Control(pDX, IDCANCEL, m_btnCancel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGUIDEMO, CBitmapDialog)
	//{{AFX_MSG_MAP(CGUIDEMO)
	ON_BN_CLICKED(IDC_BUTTON_BTN1, OnButtonBtn1)
	ON_BN_CLICKED(IDC_BUTTON_BTN2, OnButtonBtn2)
	ON_BN_CLICKED(IDC_BUTTON_BTN3, OnButtonBtn3)
	ON_BN_CLICKED(IDC_BUTTON_BTN4, OnButtonBtn4)
	ON_BN_CLICKED(IDC_BUTTON_BTN5, OnButtonBtn5)
	ON_BN_CLICKED(IDC_BUTTON_BTN6, OnButtonBtn6)
	ON_BN_CLICKED(IDC_BUTTON_BTN7, OnButtonBtn7)
	ON_BN_CLICKED(IDC_BUTTON_BTN8, OnButtonBtn8)
	ON_BN_CLICKED(IDC_BUTTON_BTN9, OnButtonBtn9)
	ON_BN_CLICKED(IDC_BUTTON_BTN10, OnButtonBtn10)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGUIDEMO message handlers

BOOL CGUIDEMO::OnInitDialog() 
{
	CBitmapDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	/*
	CBitmap m_bitmap; //建立一个位图对象
	HBITMAP hbitmap;//建立一个位图句柄
	hbitmap =(HBITMAP)::LoadImage(NULL,"test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	m_bitmap.DeleteObject(); //调用CGdiObject的DeleteObject函数将以前的BITMAP对象删除。
	m_bitmap.Attach(hbitmap);//把得到的位图句柄附到m_bitmap对象。
	*/

	// 首先找出检查到的ID
	bool bFoundID = false;
	for (UINT j = 0; j < g_nTotalDetectNum; ++j)
	{
		if (g_pUsbID[j].m_bFound)
		{
			bFoundID = true;
			break;
		}
	}
	
	if (!bFoundID)
	{
		j = 0;
	}

	struUSB_ID &UsbId = g_pUsbID[j];
	g_nCurUsbId = j;

	if (LoadBitmap(NULL, UsbId.m_szBKfile))
	{
		CBitmap *bmp = GetBitmap ();

		BITMAP bm;
		bmp->GetBitmap (&bm);

		m_nWidth = bm.bmWidth;
		m_nHeight = bm.bmHeight;

		RECT rWorkArea;
		BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA,  
							sizeof(RECT),
							&rWorkArea,
							0);    
		CRect rcWA;
		if(!bResult)
		{//如果调用不成功就利用GetSystemMetrics获取屏幕面积
			int iSX=GetSystemMetrics(SM_CXFULLSCREEN);
			int iSY=GetSystemMetrics(SM_CYFULLSCREEN);
			rcWA=CRect(0,0,iSX,iSY);
		}
		else
			rcWA=rWorkArea;

		int xc = rcWA.right - m_nWidth;
		if (xc < 0) xc = rcWA.right;

		int yc = rcWA.bottom - m_nHeight;
		if (yc < 0) yc = 0;

		SetWindowPos(&wndTop,xc/2,yc/2, m_nWidth, m_nHeight, SWP_SHOWWINDOW);
	}

	SetClickAnywhereMove (TRUE);

// init button
	InItButton(m_btn1, UsbId.m_pButtonSet[0].m_szBmpFileName,0, UsbId);
	InItButton(m_btn2, UsbId.m_pButtonSet[1].m_szBmpFileName,1, UsbId);
	InItButton(m_btn3, UsbId.m_pButtonSet[2].m_szBmpFileName,2, UsbId);
	InItButton(m_btn4, UsbId.m_pButtonSet[3].m_szBmpFileName,3, UsbId);
	InItButton(m_btn5, UsbId.m_pButtonSet[4].m_szBmpFileName,4, UsbId);
	InItButton(m_btn6, UsbId.m_pButtonSet[5].m_szBmpFileName,5, UsbId);
	InItButton(m_btn7, UsbId.m_pButtonSet[6].m_szBmpFileName,6, UsbId);
	InItButton(m_btn8, UsbId.m_pButtonSet[7].m_szBmpFileName,7, UsbId);
	InItButton(m_btn9, UsbId.m_pButtonSet[8].m_szBmpFileName,8, UsbId);
	InItButton(m_btn10, UsbId.m_pButtonSet[9].m_szBmpFileName,9, UsbId);

	if (UsbId.m_szExitfile[0])
	{
		InItButton(m_btnCancel, UsbId.m_szExitfile,10, UsbId);
	}
	else
	{
		m_btnCancel.SetBitmaps(IDB_BITMAP_EXIT, RGB(0, 0, 255));
		m_btnCancel.DrawBorder(FALSE, FALSE);
	}

	if (UsbId.m_szSound)
	{
		m_btnCancel.SetSound (UsbId.m_szSound, NULL);
	}

	SetWindowText("USB Util");
	SetForegroundWindow();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


struct struButtonPos
{
	UINT	m_nLeft;
	UINT	m_nTop;
};

struButtonPos ButtonPos[11] = 
{
	{52,43},
	{52,85},
	{52,127},
	{52,166},
	{52,210},

	{178,43},
	{178,85},
	{178,127},
	{178,166},
	{178,210},

	{268,332}
};

int g_nOrgWidth = 360;
int g_nOrgHigth = 385;


void	CGUIDEMO::InItButton(CButtonST &btn, char *szBmpFileName, int nBtnIdx, struUSB_ID &UsbId)
{
	btn.ShowWindow(SW_HIDE);

	if (!szBmpFileName || !szBmpFileName[0])
		return;

	HBITMAP hbitmap_btn;
	hbitmap_btn = (HBITMAP)::LoadImage (NULL, szBmpFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	if (!hbitmap_btn)
	{
		CString strMsg;
		strMsg.Format ("Can't load %s!", szBmpFileName);
		AfxMessageBox(strMsg);
	}
	else
	{
		btn.ShowWindow(SW_SHOW);
		btn.SetBitmaps(hbitmap_btn, RGB(0, 0, 255));
		//m_btnBanner1.SetBitmaps(IDB_BITMAP_BANNER1, RGB(0, 0, 255));
		btn.DrawBorder(FALSE, FALSE);

		CBitmap	*bmBitmap = CBitmap::FromHandle (hbitmap_btn);

		BITMAP bm;
		bmBitmap->GetBitmap (&bm);

		int width = bm.bmWidth;
		int heigth = bm.bmHeight;

		int	x = (m_nWidth * ButtonPos[nBtnIdx].m_nLeft) / g_nOrgWidth;
		int	y = (m_nHeight* ButtonPos[nBtnIdx].m_nTop) / g_nOrgHigth;

		btn.MoveWindow(x,y,width,heigth);
	}

	if (UsbId.m_szSound)
	{
		btn.SetSound (UsbId.m_szSound, NULL);
	}

	//int shBtnColor = 30;
	//btn.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor);
	//btn.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));
	//btn.SetAlign(CButtonST::ST_ALIGN_HORIZ_RIGHT);
}

void CGUIDEMO::ExeAct(int nEntry)
{
	char *szAct = g_pUsbID[g_nCurUsbId].m_pButtonSet[nEntry].m_szact;

	if (!szAct || !szAct[0])
		return;

/*
	if (!strncmp (szAct, "URL ", 4))
	{
		GotoURL(&szAct[4], SW_SHOW, "htm");
	}
	else if (!strncmp (szAct, "PDF ", 4))
	{
		GotoURL(&szAct[4], SW_SHOW, "pdf");
	}
*/
	if (!strncmp (szAct, "FILE ", 5))
	{
		char *szFileExt = GetFileExt(&szAct[5]);

		GotoURL(&szAct[5], SW_SHOW, szFileExt);
	}
	else if (!strncmp (szAct, "MENU", 4))
	{
		OnMenu(nEntry);
	}
	else if (!strncmp (szAct, "EXEC ", 5))
	{
		WinExec(&szAct[5], SW_SHOW);
	}
}

LONG CGUIDEMO::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
{
    HKEY hkey;
    LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);

    if (retval == ERROR_SUCCESS) {
        long datasize = MAX_PATH;
        TCHAR data[MAX_PATH];
        RegQueryValue(hkey, NULL, data, &datasize);
        lstrcpy(retdata,data);
        RegCloseKey(hkey);
    }

    return retval;
}


HINSTANCE CGUIDEMO::GotoURL(LPCTSTR url, int showcmd, char *szType)
{
    TCHAR key[MAX_PATH + MAX_PATH];

    // First try ShellExecute()
    HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);

    // If it failed, get the .htm regkey and lookup the program
    if ((UINT)result <= HINSTANCE_ERROR) {

		if (!szType || !szType[0])
			return 0;

		char szThisType[_MAX_PATH];
		strcpy (szThisType, ".");
		strcat (szThisType, szType);

        if (GetRegKey(HKEY_CLASSES_ROOT, szThisType, key) == ERROR_SUCCESS) {
            lstrcat(key, _T("\\shell\\open\\command"));

            if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
                TCHAR *pos;
                pos = _tcsstr(key, _T("\"%1\""));
                if (pos == NULL) {                     // No quotes found
                    pos = _tcsstr(key, _T("%1"));       // Check for %1, without quotes 
                    if (pos == NULL)                   // No parameter at all...
                        pos = key+lstrlen(key)-1;
                    else
                        *pos = '\0';                   // Remove the parameter
                }
                else
                    *pos = '\0';                       // Remove the parameter

                lstrcat(pos, _T(" "));
                lstrcat(pos, url);

#ifdef _UNICODE
                USES_CONVERSION;
                result = (HINSTANCE) WinExec(T2A(key),showcmd);
#else
                result = (HINSTANCE) WinExec(key,showcmd);
#endif
            }
        }
    }

    return result;
}


void CGUIDEMO::OnButtonBtn1() 
{
	// TODO: Add your control notification handler code here
	ExeAct(0);
}

void CGUIDEMO::OnButtonBtn2() 
{
	// TODO: Add your control notification handler code here
	ExeAct(1);
}

void CGUIDEMO::OnButtonBtn3() 
{
	// TODO: Add your control notification handler code here
	ExeAct(2);
}

void CGUIDEMO::OnButtonBtn4() 
{
	// TODO: Add your control notification handler code here
	ExeAct(3);
}

void CGUIDEMO::OnButtonBtn5() 
{
	// TODO: Add your control notification handler code here
	ExeAct(4);
}

void CGUIDEMO::OnButtonBtn6() 
{
	// TODO: Add your control notification handler code here
	ExeAct(5);
}

void CGUIDEMO::OnButtonBtn7() 
{
	// TODO: Add your control notification handler code here
	ExeAct(6);
}

void CGUIDEMO::OnButtonBtn8() 
{
	// TODO: Add your control notification handler code here
	ExeAct(7);
	
}

void CGUIDEMO::OnButtonBtn9() 
{
	// TODO: Add your control notification handler code here
	ExeAct(8);
}

void CGUIDEMO::OnButtonBtn10() 
{
	// TODO: Add your control notification handler code here
	ExeAct(9);
}

void CGUIDEMO::OnMenu(int nEntry) 
{
	// TODO: Add your control notification handler code here
	UINT nMenuItem = 2;

	if (!(nMenuItem > 0))
		return;

	CMenu menu;
	int pMenuID = 0;
	DWORD SelectionMade;
	
	// Create a new menu for the application window.
	VERIFY(menu.CreateMenu());

	CMenu	m_PopMenu;
	VERIFY(m_PopMenu.CreatePopupMenu());

    // insert a separator item at the top
    //m_PopMenu.InsertMenu(0, MF_BYPOSITION | MF_SEPARATOR, 0, "title");
    //m_PopMenu.InsertMenu(0, MF_BYPOSITION | MF_STRING | MF_DISABLED, 0, "menu");
    SetMenuDefaultItem(m_PopMenu.m_hMenu, 0, TRUE);

	//g_pMenuSet[i].m_aMenuStr.Add(szTmpMenuItem);
	//g_pMenuSet[i].m_aMenuAct.Add(szTmpMenuAct);


	for (int i = 0; i < g_pUsbID[g_nCurUsbId].m_pMenuSet[nEntry].m_aMenuStr.GetSize(); i++)
	{
		CString strName;
		strName.Format("%s", g_pUsbID[g_nCurUsbId].m_pMenuSet[nEntry].m_aMenuStr[i]);

		m_PopMenu.AppendMenu(MF_STRING, i+100, (LPCTSTR)strName);
	}

	menu.AppendMenu(MF_POPUP, (UINT) m_PopMenu.m_hMenu, "&Popup");
	
	CMenu* pPopup = menu.GetSubMenu(pMenuID);
	ASSERT(pPopup != NULL);
	
	POINT pp;
	GetCursorPos(&pp);
	SelectionMade = pPopup->TrackPopupMenu(
		TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
		pp.x,pp.y,this);
	
	pPopup->DestroyMenu();

	if (SelectionMade)
	{
		//CString strDispMsg;
		//strDispMsg.Format("%d", SelectionMade);
		//MessageBox(strDispMsg);

		int nSel = SelectionMade - 100;
		if (nSel < 0 || nSel > g_pUsbID[g_nCurUsbId].m_pMenuSet[nEntry].m_aMenuStr.GetSize())
		{
			return;
		}
		else
		{
			char *szAct = (char *&)g_pUsbID[g_nCurUsbId].m_pMenuSet[nEntry].m_aMenuAct[nSel];

			if (!szAct || !szAct[0])
				return;

			if (!strncmp (szAct, "FILE ", 5))
			{
				char *szFileExt = GetFileExt(&szAct[5]);

				GotoURL(&szAct[5], SW_SHOW, szFileExt);
			}
			else if (!strncmp (szAct, "EXEC ", 5))
			{
				WinExec(&szAct[5], SW_SHOW);
			}
		}
	}
}

char *CGUIDEMO::GetFileExt (char *szFileName)
{
	if (!szFileName || !szFileName[0] || !strchr (szFileName, '.'))
		return NULL;

	char *szDot = strchr (szFileName, '.');
	return szDot+1;
}

⌨️ 快捷键说明

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