📄 timer1msdlg.cpp
字号:
// timer1msDlg.cpp : implementation file
//
#include "stdafx.h"
#include "timer1ms.h"
#include "timer1msDlg.h"
#include "conio.h"
#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()
/////////////////////////////////////////////////////////////////////////////
// CTimer1msDlg dialog
CTimer1msDlg::CTimer1msDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTimer1msDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTimer1msDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTimer1msDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTimer1msDlg)
DDX_Control(pDX, IDC_EDIT3, m_edit3);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTimer1msDlg, CDialog)
//{{AFX_MSG_MAP(CTimer1msDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnSetTimer)
ON_BN_CLICKED(IDC_BUTTON2, OnKillTimer)
ON_BN_CLICKED(IDC_BUTTON3, OnDisplyCount)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTimer1msDlg message handlers
BOOL CTimer1msDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTimer1msDlg::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 CTimer1msDlg::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 CTimer1msDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//-------------------- 定时器程序主要部分 ----------------------------------
#include "mmsystem.h" //包含多媒体定时器函数的头文件
#pragma comment (lib,"winmm.lib") //连接多媒体库
#define MilliSecond 1 //定时间隔1毫秒
#define Accuracy 1 //系统允许的分辨率最小值
#define Min(x,y) ((x < y) ? x : y)
#define Max(x,y) ((x > y) ? x : y)
UINT TimerID; //定义定时器句柄
int count=0; //定义一个变量以进行计数
int TimerAccuracy;
char temp[100];
void CALLBACK TimerCallProc(UINT TimerID, UINT msg, DWORD dwUser, DWORD dwa, DWORD dwb)
//定义定时器事件的调用函数
{//TODO:此处可加入操作AD板的工作,将数据存储与一全局变量中;
// 当数据取得一定量后,可发消息通知其他数据处理程序、线程读取
int oo;
count++;
}
void CTimer1msDlg::OnSetTimer()
{
// TODO: Add your control notification handler code here
TIMECAPS timecaps;
int TimerResolution;
//从系统获得关于定时器服务能力的信息,
//分辨率不能超出系统许可值(1到16毫秒)
if (timeGetDevCaps(&timecaps,sizeof(TIMECAPS))==TIMERR_NOERROR)
TimerAccuracy=Min(Max(timecaps.wPeriodMin,Accuracy),timecaps.wPeriodMax);
timeBeginPeriod(TimerAccuracy);
//设置定时器分辨率
TimerResolution=1; //设置定时间隔为1毫秒
//产生间隔1毫秒,周期执行的定时器事件;启动定时器
TimerID = timeSetEvent(TimerResolution,TimerAccuracy,&TimerCallProc,1,TIME_PERIODIC);
}
void CTimer1msDlg::OnKillTimer()
{
// TODO: Add your control notification handler code here
timeKillEvent(TimerID); //删除定时器事件
timeEndPeriod(TimerAccuracy); //清除定时器分辨率
}
void CTimer1msDlg::OnDisplyCount()
{
// TODO: Add your control notification handler code here
sprintf(temp,"%d",count);
//显示在启动定时器后,进入定时器事件程序count次
//此处可加入_inp等IO读写指令操作AD板,在定时器事件函数中程序应尽量短小
m_edit3.SetWindowText (temp);
}
BOOL CTimer1msDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
timeKillEvent(TimerID); //删除定时器事件
timeEndPeriod(TimerAccuracy); //清除定时器分辨率
return CDialog::DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -