📄 key1exampledlg.cpp
字号:
// Key1ExampleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Key1Example.h"
#include "Key1ExampleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CKey1ExampleDlg dialog
CKey1ExampleDlg::CKey1ExampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CKey1ExampleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CKey1ExampleDlg)
// 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);
}
void CKey1ExampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CKey1ExampleDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CKey1ExampleDlg, CDialog)
//{{AFX_MSG_MAP(CKey1ExampleDlg)
ON_BN_CLICKED(IDC_OPEN_KEY1, OnOpenKey1)
ON_BN_CLICKED(IDC_CLOSE_KEY1, OnCloseKey1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CKey1ExampleDlg message handlers
BOOL CKey1ExampleDlg::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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
/***************************************************
实 验 代 码
****************************************************/
HANDLE hFile = INVALID_HANDLE_VALUE; /* 驱动程序设备句柄 */
DWORD Key1Count = 0;
// 读 KEY1 状态线程
DWORD CKey1ExampleDlg::ReadKey1Thread(LPVOID lparam)
{
BYTE status;
DWORD actlen;
CString strCount;
CKey1ExampleDlg *pDlg = (CKey1ExampleDlg*)lparam; /* 取得对话框指针 */
CStatic *pCountStatic = (CStatic*)pDlg->GetDlgItem(IDC_KEY1_COUNTER);
/* 取得显示计数值的文本框指针 */
while(TRUE)
{
if (hFile == INVALID_HANDLE_VALUE)
break; /* 驱动未打开, 退出线程 */
if (ReadFile(hFile, &status, 1, &actlen, NULL) == TRUE)
{
if (status == 1)
{ /* KEY1 按键按下 */
Key1Count++; /* 计数器计数 */
strCount.Format(_T("%d"), Key1Count);
pCountStatic->SetWindowText(strCount); /* 显示 */
}
else
break;
}
else
break; /* ReadFile()执行错误 */
}
return 1;
}
// "打开驱动" 按键单击事件代码
void CKey1ExampleDlg::OnOpenKey1()
{
DWORD IDThread;
HANDLE hReadKey1Thread; /* 读 KEY1 线程句柄 */
// 打开 KEY1 驱动
hFile = CreateFile(TEXT("KEY1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 KEY1 驱动失败!"));
return;
}
// 创建读 KEY1 状态线程
hReadKey1Thread = CreateThread(0, 0, ReadKey1Thread, this, 0, &IDThread);
if (hReadKey1Thread == NULL)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
return;
}
CloseHandle(hReadKey1Thread);
// 使能 "关闭驱动" 按键, 禁止 "打开驱动" 按键
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_KEY1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_KEY1); /* 取得控件指针 */
pOpenButton->EnableWindow(FALSE);
pCloseButton->EnableWindow(TRUE);
MessageBox(_T("打开 KEY1 驱动成功!"));
}
// "关闭驱动" 按键单击事件代码
void CKey1ExampleDlg::OnCloseKey1()
{
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_KEY1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_KEY1); /* 取得控件指针 */
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile); /* 关闭驱动 */
hFile = INVALID_HANDLE_VALUE;
pOpenButton->EnableWindow(TRUE); /* 使能"打开驱动"按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止"关闭驱动"按键 */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -