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

📄 ezdriversetupdlg.cpp

📁 驱动安装程序
💻 CPP
字号:
// EzDriverSetupDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EzDriverSetup.h"
#include "EzDriverSetupDlg.h"
#include <AfxPriv.h>

#define COUNT_OF(x)	(sizeof(x)/sizeof((x)[0]))

#pragma comment(lib,"Setupapi.lib")
#pragma comment(lib,"newdev.lib")
#include <stdio.h>
#define DT	DebugTrace
BOOL DebugTrace(const char * lpszFormat,...)
{
	HWND hwnd = ::FindWindowA(NULL,"DebugHelper ");
	if(!IsWindow(hwnd))
		hwnd = ::FindWindowA(NULL,"DebugHelper ");
	if(hwnd)
	{
		char szMsg[512];

		va_list argList;
		va_start(argList, lpszFormat);
		try
		{
			vsprintf(szMsg,lpszFormat, argList);
		}
		catch(...)
		{
			strcpy(szMsg ,"DebugHelper:Invalid string format!");
		}
		va_end(argList);

		::SendMessage(hwnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szMsg);
	}
	return TRUE;
}

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CEzDriverSetupDlg dialog

CEzDriverSetupDlg::CEzDriverSetupDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEzDriverSetupDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEzDriverSetupDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEzDriverSetupDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEzDriverSetupDlg)
	DDX_Control(pDX, IDC_COMBO_INF, m_comboInf);
	DDX_Control(pDX, IDC_LIST_DEVICE, m_lstDevice);
	//}}AFX_DATA_MAP
}

//Windows设备管理器在改变设备状态后会广播一个自注册的消息。
//其格式为"DevMgrRefreshOn"+计算机名;
//我们在收到此消息后刷新设备列表
//注意:最好不用WM_DEVICECHANGE,WM_DEVICECHANGE在改变设备之前已发送,但改变设备可能需要一些时间,
//如果一接收到WM_DEVICECHANGE就更新的话,可能设备改变这一动作还没有做完。
//因此,响应Windows设备管理器自注册更好些。
//由于有些自制安装程序,比如sofeice带的EzDriverInstaller在改变状态后只广播WM_DEVICECHANGE
//最好两种情况综合考虑。
static CString GetDeviceChangedMsg()
{
	CString szComputerName;
	DWORD dwSize = MAX_PATH;
	::GetComputerName(szComputerName.GetBufferSetLength(dwSize),&dwSize);

	return "DevMgrRefreshOn"+szComputerName;
}

const UINT WM_DEVICECHANGED = ::RegisterWindowMessage(GetDeviceChangedMsg());

BEGIN_MESSAGE_MAP(CEzDriverSetupDlg, CDialog)
	//{{AFX_MSG_MAP(CEzDriverSetupDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_CBN_SELCHANGE(IDC_COMBO_INF, OnSelchangeComboInf)
	ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
    ON_REGISTERED_MESSAGE(WM_DEVICECHANGED,OnDeviceChanged)
	ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
	ON_UPDATE_COMMAND_UI_RANGE(IDC_BTN_UPDATE_DRIVER,IDC_BTN_REMOVE_DEVICE,OnUpdateBtn)
	ON_COMMAND_RANGE(IDC_BTN_UPDATE_DRIVER,IDC_BTN_REFRESH,OnBtn)
	ON_UPDATE_COMMAND_UI(IDC_EDIT_DEVICE_STATUS,OnUpdateBtn)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEzDriverSetupDlg message handlers

BOOL CEzDriverSetupDlg::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
	
	Init();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEzDriverSetupDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	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 CEzDriverSetupDlg::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 CEzDriverSetupDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CEzDriverSetupDlg::Init()
{
	m_lstDevice.InsertColumn(0,"Devices",LVCFMT_LEFT,300);

	UpdateControlPos();

	
	int nInf = AfxGetApp()->GetProfileInt("Settings","nInf",0);
	for(int i=0;i<nInf;i++)
	{
		CString szItem;
		szItem.Format("Inf%02d",i);
		CString szInfFile = AfxGetApp()->GetProfileString("Settings",szItem,"");
		if(!szInfFile.IsEmpty())
			m_comboInf.AddString(szInfFile);
	}

	/*char *pszArrInfFile[]={
		"C:\\Program Files\\10moons\\MC_DriverSetup\\TV3\\CAP7134.INF",
		"C:\\Program Files\\10moons\\MC_DriverSetup\\TV3\\PhTV7134.inf",
		"G:\\WINDDK\\2600\\src\\general\\toaster\\Bin\\bus.inf",
		"f:\\hellowdm\\hellowdm.inf",
		"I:\\0vc\\WDM\\HelloWDM\\hellowdm.inf",
		"I:\\WINDDK\\2600\\src\\general\\toaster\\Bin\\toaster.inf",
		"I:\\WINDDK\\2600\\src\\general\\toaster\\Bin\\bus.inf",
		"G:\\WINDDK\\2600\\src\\general\\toaster\\bin\\filter.inf",
	};
		for(int i=0;i<sizeof(pszArrInfFile)/sizeof(pszArrInfFile[0]);i++)
			m_comboInf.AddString(pszArrInfFile[i]);
	//*/
	
	int nSel = AfxGetApp()->GetProfileInt("Settings","nComboInf",0);
	if(nSel>=0 && nSel < m_comboInf.GetCount())
	{
		m_comboInf.SetCurSel(nSel);
		OnSelchangeComboInf();
	}

	UINT nArrTips[]={
		IDC_BTN_UPDATE_DRIVER,IDS_BTN_UPDATE_DRIVER,
		IDC_BTN_ENABLE_DEVICE,IDS_BTN_ENABLE_DEVICE,
		IDC_BTN_RESTART_DEVICE,IDS_BTN_RESTART_DEVICE,
		IDC_BTN_ADD_DEVICE,IDS_BTN_ADD_DEVICE,
		IDC_BTN_REMOVE_DEVICE,IDS_BTN_REMOVE_DEVICE,
	};
	AddToolTips(nArrTips,COUNT_OF(nArrTips));

	SaveWindowPos(FALSE);
	DragAcceptFiles(TRUE);
	
	return TRUE;
}

void CEzDriverSetupDlg::OnDestroy() 
{
	SaveWindowPos(TRUE);

	int nSel = m_comboInf.GetCurSel();
	AfxGetApp()->WriteProfileInt("Settings","nComboInf",nSel);
	
	int nInf = m_comboInf.GetCount();
	AfxGetApp()->WriteProfileInt("Settings","nInf",nInf);
	for(int i=0;i<nInf;i++)
	{
		CString szItem;
		szItem.Format("Inf%02d",i);
		CString szInfFile;
		m_comboInf.GetLBText(i,szInfFile);
		if(!szInfFile.IsEmpty())
			AfxGetApp()->WriteProfileString("Settings",szItem,szInfFile);
	}
	
	CDialog::OnDestroy();


}

void CEzDriverSetupDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	UpdateControlPos();	
}

void CEzDriverSetupDlg::UpdateControlPos()
{
	if(!m_lstDevice.GetSafeHwnd())
		return;

	CRect rc;
	GetClientRect(rc);
	
	CRect rcDevice(rc);
	rcDevice.DeflateRect(0,30,100,2);
	m_lstDevice.MoveWindow(rcDevice);
	
	/*
	CRect rcStatus(rc);
	rcStatus.DeflateRect(0,00,100,0);
	rcStatus.top = rcStatus.bottom -196;
	GetDlgItem(IDC_EDIT_DEVICE_STATUS)->MoveWindow(rcStatus);
	//*/

	rcDevice = rc;
	rcDevice.DeflateRect(0,8,100,0);
	m_comboInf.MoveWindow(rcDevice);

	CWnd *pWnd=NULL;
	for(UINT nID = IDC_BTN_UPDATE_DRIVER;(pWnd=GetDlgItem(nID))!=NULL;nID++)
	{
		CRect rcWindow;
		pWnd->GetWindowRect(rcWindow);
		ScreenToClient(rcWindow);

		int w = rcWindow.Width(),h=rcWindow.Height();
		rcWindow.right = rc.right-4;
		rcWindow.left = rc.right - 4-w;
		pWnd->MoveWindow(rcWindow);
	}
}

BOOL CEzDriverSetupDlg::PreTranslateMessage(MSG* pMsg) 
{
	if(	m_toolTip.GetSafeHwnd())
	{
		m_toolTip.RelayEvent(pMsg) ;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

LRESULT CEzDriverSetupDlg::OnDeviceChanged(WPARAM wp, LPARAM lp)
{
	//DT("OnDeviceChanged");
	m_lstDevice.OnDeviceChanged(wp,lp);
	return 0;
}

void CEzDriverSetupDlg::OnSelchangeComboInf() 
{
	int nSel = m_comboInf.GetCurSel();
	if(nSel != CB_ERR)
	{
		CString szInfFile;
		m_comboInf.GetLBText(nSel,szInfFile);
		m_lstDevice.OpenInfFile(szInfFile);
	}
	else
		m_lstDevice.OpenInfFile("");
}

LRESULT CEzDriverSetupDlg::OnKickIdle(WPARAM wp, LPARAM lp)
{
	UpdateDialogControls(this, TRUE);
	return 0;
}

BOOL CEzDriverSetupDlg::Find(UINT nIDArr[], UINT cbSize, UINT nFind)
{
	for(UINT i=0;i<cbSize;i++)
	{
		if(nIDArr[i] == nFind)
			return TRUE;
	}
	return FALSE;
}

BOOL CEzDriverSetupDlg::IsFunctionEnabled(UINT nID)
{
	if(nID == IDC_EDIT_DEVICE_STATUS)
		return TRUE;

	return m_lstDevice.IsFunctionEnabled(nID);
}

void CEzDriverSetupDlg::OnUpdateBtn(CCmdUI *pCmdUI)
{
	UINT nID = pCmdUI->m_nID;
	BOOL bEnabled = IsFunctionEnabled(nID);
	pCmdUI->Enable(bEnabled);

	if(nID == IDC_BTN_ENABLE_DEVICE)
	{
		BOOL bDeviceDesabled = m_lstDevice.IsDeviceDisabled();
		SetBtnText(nID,bDeviceDesabled?IDS_ENABLE_DEVICE:IDS_DISABLE_DEVICE);
	}
	else if(nID == IDC_EDIT_DEVICE_STATUS)
	{
		CString szDeviceStatus = "hello";
		CString szCur;
		GetDlgItemText(IDC_EDIT_DEVICE_STATUS,szCur);
		if(szCur != szDeviceStatus)
			SetDlgItemText(IDC_EDIT_DEVICE_STATUS,szDeviceStatus);
	}
}

void CEzDriverSetupDlg::OnBtn(UINT nID)
{
	m_lstDevice.OnBtn(nID);

}

void CEzDriverSetupDlg::SetBtnText(UINT nID, UINT nIDS)
{
	CWnd *pWnd = GetDlgItem(nID);
	if(pWnd)
	{
		CString szText,szTextNew;
		szTextNew.LoadString(nIDS);
		pWnd->GetWindowText(szText);
		if(szText != szTextNew)
			pWnd->SetWindowText(szTextNew);

	}
	else
		ASSERT(FALSE);
}

void CEzDriverSetupDlg::AddToolTip(UINT nIDCtrl, UINT nIDString)
{
	if(!m_toolTip.GetSafeHwnd())
		m_toolTip.Create(this, TTS_ALWAYSTIP);
	
	CWnd *pWnd = GetDlgItem(nIDCtrl);
	if(pWnd && m_toolTip.GetSafeHwnd())
	{
		m_toolTip.AddTool(pWnd,nIDString);
	}
	else
		ASSERT(FALSE);
}

void CEzDriverSetupDlg::AddToolTips(UINT nIDArr[], UINT nSize)
{
	ASSERT( (nSize & 0x01) == 0);

	if(!m_toolTip.GetSafeHwnd())
		m_toolTip.Create(this, TTS_ALWAYSTIP);
	
	for(UINT i=0;i<nSize;i+=2)
	{
		CWnd *pWnd = GetDlgItem(nIDArr[i]);	
		ASSERT(pWnd);
		m_toolTip.AddTool(pWnd,nIDArr[i+1]);
	}
}

void CEzDriverSetupDlg::OnBtnOpen() 
{
	CString sFilter="Inf Files(*.inf) | *.inf|";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, sFilter, this);
	if(dlg.DoModal() == IDOK)
	{
		CString szInfFile = dlg.GetPathName();

		int nIndex = m_comboInf.SelectString(-1,szInfFile);
		if( nIndex == -1)
			nIndex = m_comboInf.AddString(szInfFile);
		m_comboInf.SetCurSel(nIndex);
		OnSelchangeComboInf();
	}
}

void CEzDriverSetupDlg::OnBtnClose() 
{
	int nIndex = m_comboInf.GetCurSel();
	if(nIndex != -1)
	{
		int nSel = -1;
		m_comboInf.DeleteString(nIndex);
		int nc = m_comboInf.GetCount();
		if(nc>0)
		{
			if(nIndex<nc)
				nSel = nIndex;
			else
				nSel = nc-1;
		}
		if(nSel != -1)
		{
			m_comboInf.SetCurSel(nSel);
			OnSelchangeComboInf();
		}
		else
		{
			m_lstDevice.DeleteAllItems();
			m_lstDevice.OpenInfFile("");
		}
	}
}

void CEzDriverSetupDlg::SaveWindowPos(BOOL bSave)
{
	if(bSave)
	{
		// save windows position info
		WINDOWPLACEMENT wndStatus = {0};
		wndStatus.length = sizeof(WINDOWPLACEMENT);
		GetWindowPlacement(&wndStatus);
		AfxGetApp()->WriteProfileBinary("Settings","Pos",
			(LPBYTE)&wndStatus,sizeof(wndStatus));
	}
	else
	{
		unsigned char * pData = NULL;
		UINT nRead = 0;
		WINDOWPLACEMENT wndStatus = {0};
		AfxGetApp()->GetProfileBinary("Settings","Pos",
			&pData,&nRead);
		if(nRead == sizeof(wndStatus))
		{
			memcpy((char*)&wndStatus,(char*)pData,nRead);
			VERIFY(SetWindowPlacement(&wndStatus));
		}
		delete pData;
		pData = NULL;
	}
}

void CEzDriverSetupDlg::OnDropFiles(HDROP hDropInfo) 
{
	UINT nFiles = ::DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);

	CWinApp* pApp = AfxGetApp();
	ASSERT(pApp != NULL);

	BOOL bFirst = TRUE;
	for (UINT iFile = 0; iFile < nFiles; iFile++)
	{
		TCHAR szFileName[_MAX_PATH];
		::DragQueryFile(hDropInfo, iFile, szFileName, _MAX_PATH);

		char szBuf[MAX_PATH];
		_tsplitpath(szFileName, NULL, NULL, NULL, szBuf);
		if(lstrcmpi(szBuf,".inf") == 0)
		{
			int nIndex = m_comboInf.SelectString(-1,szFileName);
			if( nIndex == -1)
			{
				nIndex = m_comboInf.AddString(szFileName);
				if(bFirst)//
				{
					bFirst = FALSE;
					m_comboInf.SetCurSel(nIndex);
					OnSelchangeComboInf();
				}
			}
			else
				OnSelchangeComboInf();
		}
	}
	::DragFinish(hDropInfo);
		
	CDialog::OnDropFiles(hDropInfo);
}

⌨️ 快捷键说明

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