📄 dpmonitordlg.cpp
字号:
// DPMonitorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DPMonitor.h"
#include "DPMonitorDlg.h"
#include "..\ReadWriteDeviceinterface.h" // Has class GUID definition
#include <winioctl.h>
// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
GUID ClassGuid = ReadWriteDevice_CLASS_GUID;
typedef short CSHORT;
typedef struct _TIME_FIELDS {
CSHORT Year; // range [1601...]
CSHORT Month; // range [1..12]
CSHORT Day; // range [1..31]
CSHORT Hour; // range [0..23]
CSHORT Minute; // range [0..59]
CSHORT Second; // range [0..59]
CSHORT Milliseconds;// range [0..999]
CSHORT Weekday; // range [0..6] == [Sunday..Saturday]
} TIME_FIELDS;
typedef TIME_FIELDS *PTIME_FIELDS;
BOOL KeepRunning;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DWORD WINAPI ServiceThread(LPVOID pParam)
{
HANDLE m_hDevice,IOWaiter;
char *buf;
DWORD Error;
ULONG nBytesRead;
CString msg;
HWND ViewHwnd = (HWND)pParam;
CDPMonitorDlg* pDlg=(CDPMonitorDlg*)AfxGetMainWnd();
m_hDevice = OpenByInterface( &ClassGuid, 0, &Error);
if (m_hDevice == INVALID_HANDLE_VALUE)
{
msg.Format("Cannot open device, error 0x%x\n", GetLastError());
pDlg->OnPrintError(msg);
return 1;
}
IOWaiter = CreateEvent( NULL, TRUE, FALSE, NULL);
if( IOWaiter==NULL)
{
msg.Format("Cannot Create Event, error 0x%x\n", GetLastError());
pDlg->OnPrintError(msg);
CloseHandle(m_hDevice);
return 1;
}
OVERLAPPED ol;
ol.Offset = 0;
ol.OffsetHigh = 0;
ol.hEvent = IOWaiter;
KeepRunning=TRUE;
for(;;)
{
ResetEvent(IOWaiter);
buf = new char[512];
if (buf==NULL)
{
CloseHandle(m_hDevice);
CloseHandle(IOWaiter);
return 1;
}
if (!ReadFile(
m_hDevice,
buf,
512,
&nBytesRead,
&ol) )
{
if( GetLastError()!=ERROR_IO_PENDING)
{
msg.Format("GET_TIMESTAMP_DATA failed %x\n", GetLastError());
pDlg->OnPrintError(msg);
goto EXIT;
}
while( WaitForSingleObject( IOWaiter, 100)==WAIT_TIMEOUT)
{
if(!KeepRunning)
{
// Cancel the pending read
CancelIo(m_hDevice);
goto EXIT;
}
}
if( !GetOverlappedResult( m_hDevice, &ol, &nBytesRead, FALSE))
{
msg.Format("GetOverlappedResult failed %d (ol.Internal=%X)",
GetLastError(),ol.Internal);
pDlg->OnPrintError(msg);
continue;
}
}
buf[nBytesRead]=0;
::PostMessage( ViewHwnd, WM_DEBUGPRINTEVENT, 0, (LPARAM)buf);
}
EXIT: delete buf;
CloseHandle(m_hDevice);
CloseHandle(IOWaiter);
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CDPMonitorDlg dialog
CDPMonitorDlg::CDPMonitorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDPMonitorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDPMonitorDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDPMonitorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDPMonitorDlg)
DDX_Control(pDX, IDC_LIST1, m_Event);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDPMonitorDlg, CDialog)
//{{AFX_MSG_MAP(CDPMonitorDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_DEBUGPRINTEVENT,OnDebugPrintEvent)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDPMonitorDlg message handlers
BOOL CDPMonitorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
KeepRunning=FALSE;
CWnd* m_cWnd=AfxGetMainWnd();
HWND ViewHwnd = m_cWnd->m_hWnd;
DWORD Tid;
Thread1=CreateThread(0, 0x1000, ServiceThread, ViewHwnd, 0, &Tid);
m_Event.InsertColumn(0,"Time",LVCFMT_LEFT,60,-1);
m_Event.InsertColumn(1,"Driver",LVCFMT_LEFT,100,-1);
m_Event.InsertColumn(2,"Info",LVCFMT_LEFT,240,-1);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CDPMonitorDlg::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 CDPMonitorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDPMonitorDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if (KeepRunning)
{
KeepRunning=FALSE;
WaitForSingleObject(Thread1,INFINITE);
}
}
void CDPMonitorDlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_Event.DeleteAllItems();
}
LONG CDPMonitorDlg::OnDebugPrintEvent( UINT, LONG lParam)
{
char *Buf,*event;
CString msg;
int ListnowItem;
Buf=(char *)lParam;
PTIME_FIELDS pTF = (PTIME_FIELDS)lParam;
CTime EventTime(pTF->Year,pTF->Month,pTF->Day,pTF->Hour,pTF->Minute,pTF->Second);
msg.Format("%s",EventTime.Format("%H:%M:%S"));
ListnowItem=m_Event.GetItemCount( );
m_Event.InsertItem(ListnowItem,msg);
lParam+=sizeof(TIME_FIELDS);
event=(char *)lParam;
msg.Format("%s",event);
int FirstTabPos = msg.Find(':');
if( FirstTabPos==-1) return 0;
CString Driver = msg.Left(FirstTabPos);
m_Event.SetItemText(ListnowItem,1,Driver);
CString Info = msg.Mid(FirstTabPos+1);
m_Event.SetItemText(ListnowItem,2,Info);
delete Buf;
return 0;
}
void CDPMonitorDlg::OnPrintError(CString Message)
{
CTime t = CTime::GetCurrentTime();
CString msg;
msg.Format("%s",t.Format("%H:%M:%S"));
int ListnowItem;
ListnowItem=m_Event.GetItemCount( );
ListnowItem=m_Event.InsertItem(ListnowItem,msg);
m_Event.SetItemText(ListnowItem,1,"Monotor");
m_Event.SetItemText(ListnowItem,2,Message);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -