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

📄 testappdlg.cpp

📁 1394测试程序
💻 CPP
字号:
// TestAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include <dbt.h>
#include "TestApp.h"
#include "TestAppDlg.h"
#include "hlstatic.h"

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


static
const char*
DeviceChangeMsgStr(UINT x);



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

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

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CString	m_strVer;
	CString	m_strCopyright;
	//}}AFX_DATA
	CHyperLinkStatic	m_HlStatic;//打开浏览器

	// 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)
{	
	m_strVer = "Version 1.1(debug)";
	m_strCopyright = "Copyright 2002-2003 by Wang Xiao";
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_STATIC_ABOUT_HL, m_HlStatic);
	DDX_Text(pDX, IDC_STATIC_ABOUT_VER, m_strVer);
	DDX_Text(pDX, IDC_STATIC_ABOUT_COPYRIGHT, m_strCopyright);
	//}}AFX_DATA_MAP
}

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



/////////////////////////////////////////////////////////////////////////////
// CTestAppDlg dialog

/////////////////////////////////////////////////////////////////////////////

CTestAppDlg::CTestAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestAppDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestAppDlg)
		// 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);
	// --> moved to OnInitDialog()
	m_nMode = 0;
	m_DevNotify=NULL;

}


CTestAppDlg::~CTestAppDlg()
{
}



void CTestAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestAppDlg)
	DDX_Control(pDX, IDC_TAB_MAIN, m_TabControlMain);
	DDX_Control(pDX, IDC_EDIT_OUTPUT_WINDOW, m_EditControl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestAppDlg, CDialog)
	//{{AFX_MSG_MAP(CTestAppDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SHOW_OUTPUT_WINDOW, OnButtonShowOutputWindow)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_OUTPUT_WINDOW, OnButtonClearOutputWindow)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_MAIN, OnSelchangeTabMain)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestAppDlg message handlers

BOOL CTestAppDlg::OnInitDialog()
{
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	// call base class
	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
	
	// create all dialog page instances, parent window = Tab Control
	VERIFY( m_DlgPgDevice.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgConfiguration.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgBusTopology.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgAsyncRdWr.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgAllocateAddressRange.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgAsyncLockStream.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgAsyncLookBack.Create(&m_TabControlMain, this) );
/*  //2002.9.22修改,为了进行异步调试
	VERIFY( m_DlgPgAsyncLckStr.Create(&m_TabControlMain, this) );
	VERIFY( m_DlgPgIsochIo.Create(&m_TabControlMain, this) );
    //修改结束
*/
//	VERIFY( m_DlgPgOutput.Create(&m_TabControlMain, this) );
	// register our dialog page instances with the tab control
	m_TabControlMain.AddDialogPage(&m_DlgPgDevice,IDS_DIALOGPAGE_DEVICE);
	m_TabControlMain.AddDialogPage(&m_DlgPgConfiguration,IDS_DIALOGPAGE_CONFIGURATION);
	m_TabControlMain.AddDialogPage(&m_DlgPgBusTopology,IDS_DIALOGPAGE_BUS_TOPOLOGY);
	m_TabControlMain.AddDialogPage(&m_DlgPgAsyncRdWr,IDS_DIALOGPAGE_ASYNCRW);
	m_TabControlMain.AddDialogPage(&m_DlgPgAllocateAddressRange,IDS_DIALOGPAGE_ALLOCATE_ADDRESS_RANGE);
	m_TabControlMain.AddDialogPage(&m_DlgPgAsyncLockStream,IDS_DIALOGPAGE_ASYNC_LOCK_STREAM);
	m_TabControlMain.AddDialogPage(&m_DlgPgAsyncLookBack,IDS_DIALOGPAGE_ASYNC_LOOKBACK);
/*  //2002.9.22修改,为了进行异步调试
	m_TabControlMain.AddDialogPage(&m_DlgPgAsyncLckStr,IDS_DIALOGPAGE_ASYNCLS);
	m_TabControlMain.AddDialogPage(&m_DlgPgIsochIo,IDS_DIALOGPAGE_ISOCH_IO);
    //修改结束
*/
//	m_TabControlMain.AddDialogPage(&m_DlgPgOutput,IDS_DIALOGPAGE_OUTPUT);
	// update selection
	m_TabControlMain.UpdatePageSelection();

	RECT nwRect;
	//Get the rect infor;
	GetWindowRect(&nwRect);//window
	nwRect.bottom = nwRect.bottom - 270 ;
	MoveWindow(&nwRect);

	BOOL succ = m_font.CreatePointFont(
								100,	//int nPointSize, 
								"Courier",	//LPCTSTR lpszFaceName,
								NULL				// CDC* pDC = NULL
								); 
	if ( succ ) 
	{
		m_EditControl.SetFont(&m_font);
	} 
	else 
	{
		::AfxMessageBox("Cannot init font");
	}

	// set text limit to 512K
	m_EditControl.SetLimitText(512*1024);

	// register handler for device notifications from devices with specified interface
	if (!RegisterDevNotify(&g_Test1394DefaultIID, &m_DevNotify)) {
		PrintOut("Cannot register device notification."NL);
	} 

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestAppDlg::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 CTestAppDlg::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 CTestAppDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CTestAppDlg::OnButtonShowOutputWindow() 
{	
	// show output window on screen
	m_nMode++;

	m_nMode = m_nMode % 2;

	RECT nwRect;
//	RECT ntRect;
	

	//Get the rect infor;
	GetWindowRect(&nwRect);//window
//	GetDlgItem(IDC_EDIT_OUTPUT_WINDOW)->GetWindowRect(&ntRect);//Edit window	

	if (m_nMode == 0)//no show
	{		
		nwRect.bottom = nwRect.bottom - 270 ;
		//change
		MoveWindow(&nwRect);	
	}
	
	if (m_nMode == 1)//show
	{
		nwRect.bottom = nwRect.bottom + 270;
		//Change
		MoveWindow(&nwRect);	
	}
}


void CTestAppDlg::OnButtonClearOutputWindow() 
{
	// clear output window
//	theApp.m_MainDlg.m_DlgPgOutput.Clear();	
// 	theApp.SendMsgToOutputWin(WM_USER_CLEAR_OUTPUT_WIN,0,0);
	m_EditControl.SetWindowText("");
}



void CTestAppDlg::OnSelchangeTabMain(NMHDR* pNMHDR, LRESULT* pResult) 
{
	m_TabControlMain.UpdatePageSelection();
	
	*pResult = 0;
}


/*
void CTestAppDlg::PostNcDestroy() 
{
	// destroy all non-modal dialog windows
	CNonModalDlg *dlg;
	while ( !g_NonModalDialogList.IsEmpty() ) {
		dlg = (CNonModalDlg*)g_NonModalDialogList.GetHead();
		dlg->DestroyWindow();
	}
	
	CDialog::PostNcDestroy();
}
*/

void CTestAppDlg::OnDestroy() 
{
	// close device
	g_CurrentDev.Close();

	if (_winver >=0x500) { // fix a bug in win 98, it becomes unstable after this call
		if (m_DevNotify) {
			UnregisterDeviceNotification(m_DevNotify);//注销
			m_DevNotify = NULL;
		} 
	}

	CDialog::OnDestroy();
}



//注册
BOOL
CTestAppDlg::RegisterDevNotify(
	const GUID *InterfaceClassGuid, 
	HDEVNOTIFY *hDevNotify
	)
/*
Routine Description:
    Registers for notification of changes in the device interfaces for
    the specified interface class GUID. 

Parameters:
    InterfaceClassGuid - The interface class GUID for the device 
        interfaces. 

    hDevNotify - Receives the device notification handle. On failure, 
        this value is NULL.

Return Value:
    If the function succeeds, the return value is TRUE.

    If the function fails, the return value is FALSE.
*/
{
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
	/*
	typedef struct _DEV_BROADCAST_DEVICEINTERFACE 
	{
	  DWORD dbcc_size; 
	  DWORD dbcc_devicetype; 
	  DWORD dbcc_reserved; 
	  GUID dbcc_classguid; 
	  TCHAR dbcc_name[1]; 
	} DEV_BROADCAST_DEVICEINTERFACE *PDEV_BROADCAST_DEVICEINTERFACE;
	*/
    ::ZeroMemory(&NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
	NotificationFilter.dbcc_name[0] = 0;
    NotificationFilter.dbcc_classguid = *InterfaceClassGuid;//设备接口

    // device notifications should be send to the main dialog
	//win32程序调用RegisterDeviceNotification注册,表示它想接受pnp通知设备变化情况
	*hDevNotify = RegisterDeviceNotification(
											m_hWnd, 
											&NotificationFilter,
											DEVICE_NOTIFY_WINDOW_HANDLE
											);
    if ( !(*hDevNotify) ) 
	{
		DWORD Err = GetLastError();
		CString str;
		str.Format("RegisterDeviceNotification failed, errcode:%d",Err);		
		PrintOut( str.GetBufferSetLength( str.GetLength() ) );
		str.ReleaseBuffer();
		PrintOut(NL);
		return FALSE;
    }

    return TRUE;
}


//WM_DEVICECHANGE的回调函数,对硬件环境的改变做出反应
BOOL CTestAppDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
	DEV_BROADCAST_DEVICEINTERFACE* data=(DEV_BROADCAST_DEVICEINTERFACE*)dwData;

	CString strMsg;
	strMsg.Format("OnDeviceChange message: %08X (%s)",nEventType,DeviceChangeMsgStr(nEventType));
	PrintOut( strMsg.GetBufferSetLength( strMsg.GetLength() ) );
	strMsg.ReleaseBuffer();
	PrintOut(NL);
//	PrintOut("OnDeviceChange message: %08X (%s)"NL,nEventType,DeviceChangeMsgStr(nEventType));

	// check if data is valid
	if ( (data==NULL) || (data->dbcc_name==NULL) || (strlen(data->dbcc_name)==0) ) 
		return TRUE;

	// convert interface name to CString
	CString Name(data->dbcc_name);

	// there is some strange behavior in Win98
	// there are notifications with dbcc_name = "."
	// we ignore this
	if (Name.GetLength() < 5) 
		return TRUE;	


	switch (nEventType) 
	{
		case DBT_DEVICEREMOVECOMPLETE://一个设备已被删除
			// a device with our interface has been removed or is stopped
	//		PrintOut("The 1394 device"NL" %s"NL" has been removed."NL,data->dbcc_name);
			// close the global handle
			if ( g_CurrentDev.GetDevicePathName() && (0==Name.CompareNoCase(g_CurrentDev.GetDevicePathName())) ) 
			{
				PrintOut("Closing driver interface"NL NL);
				g_CurrentDev.Close();
				PrintOut("Device closed."NL);
				// destroy device list
				DestroyDeviceList(g_DevList);
				g_DevList=NULL;
				// update global var
				g_DeviceNumber = -1;
				// update display
				m_DlgPgDevice.m_StrDeviceNodeAddr = _T("");
				m_DlgPgDevice.m_StrHCNodeAddr = _T("");
				m_DlgPgDevice.m_StrDriverVersionInfo = ("Not connected to device driver");
				m_DlgPgDevice.UpdateData(FALSE);
			}
			// destroy all non-modal dialog windows
	#if 0
			CNonModalDlg *dlg;
			while ( !g_NonModalDialogList.IsEmpty() ) {
				dlg = (CNonModalDlg*)g_NonModalDialogList.GetHead();
				dlg->DestroyWindow();
			}
	#endif
			break;

		case DBT_DEVICEARRIVAL://一个设备已插入,现在可使用
			// a device with our interface has been activated (started)
			PrintOut("A new 1394 device has been plugged in and is now available."NL);	
	//		PrintOut("Device path is:"NL" %s."NL, data->dbcc_name);	
			break;

		case DBT_DEVICEQUERYREMOVE://请求删除设备的许可
			// windows asked, if our device can be removed, we answer with TRUE (yes)
			PrintOut("MSG: DBT_DEVICEQUERYREMOVE -- Application returned success."NL);
			break;

		case DBT_DEVICEREMOVEPENDING://设备要被删除,不能取消
			// device remove is pending
			PrintOut("MSG: DBT_DEVICEREMOVEPENDING."NL);
			break;

		default:
			break;
	} 

	return TRUE;
}



//
// helper functions
//

//lint -save -e773
#define _ENUMSTR(e) \
		(x==(e)) ? #e
//lint -restore

static
const char*
DeviceChangeMsgStr(UINT x)
{
	return (
		_ENUMSTR(DBT_DEVICEARRIVAL) :
		_ENUMSTR(DBT_DEVICEQUERYREMOVE) :
		_ENUMSTR(DBT_DEVICEQUERYREMOVEFAILED) :
		_ENUMSTR(DBT_DEVICEREMOVEPENDING) :
		_ENUMSTR(DBT_DEVICEREMOVECOMPLETE) :
		_ENUMSTR(DBT_DEVICETYPESPECIFIC) :
		_ENUMSTR(DBT_CUSTOMEVENT) :
		_ENUMSTR(DBT_USERDEFINED) :
		_ENUMSTR(DBT_DEVNODES_CHANGED) :
		"unknown"
	);
}

void CTestAppDlg::OutputString(LPTSTR pstrMsg)
{
	int len;
	int limit;
	
	// get current length
	len=m_EditControl.GetWindowTextLength(); 
	// get limit
	limit = m_EditControl.GetLimitText() - 2048;

	if ( len > limit ) {
		// delete 8K text
		m_EditControl.SetSel(0,8192);
		m_EditControl.Clear();
		len=m_EditControl.GetWindowTextLength(); 
	}
	// append string
	m_EditControl.SetSel(len,len);
	m_EditControl.ReplaceSel(pstrMsg, FALSE);
}

⌨️ 快捷键说明

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