?? 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 + -