📄 item4dlg.cpp
字号:
// item4Dlg.cpp : implementation file
//
#include "DRAW.h"
#include "stdafx.h"
#include "item4.h"
#include "item4Dlg.h"
#include "DRAWAFREQ.h"
#include "HELP.h"
#include "ADraw.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
UINT TestThread(LPVOID pParam);
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()
/////////////////////////////////////////////////////////////////////////////
// CItem4Dlg dialog
CItem4Dlg::CItem4Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CItem4Dlg::IDD, pParent)
{
this->mpAFreqDialog=new DRAWAFREQ(this);
this->mpAFreqDialog->Create();
this->number=0;
once=true;
//{{AFX_DATA_INIT(CItem4Dlg)
// 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 CItem4Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CItem4Dlg)
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Control(pDX, IDC_LIST1, m_List);
DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CItem4Dlg, CDialog)
//{{AFX_MSG_MAP(CItem4Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPENCOM, OnButtonOpencom)
ON_COMMAND(ID_MENUITEM_AFREQ, OnMenuitemAfreq)
ON_BN_CLICKED(IDC_BUTTON_DATABASE, OnButtonDatabase)
ON_COMMAND(ID_MENUITEM_A, OnMenuitemA)
ON_COMMAND(ID_MENUITEM_B, OnMenuitemB)
ON_COMMAND(ID_MENUITEM_C, OnMenuitemC)
ON_COMMAND(ID_MENUITEM_HELP, OnMenuitemHelp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CItem4Dlg message handlers
BOOL CItem4Dlg::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
this->m_List.InsertColumn(0,"序号",LVCFMT_LEFT,100,-1);
this->m_List.InsertColumn(1,"a路频率(Hz)",LVCFMT_LEFT,100,-1);
this->m_List.InsertColumn(2,"b路频率(Hz)",LVCFMT_LEFT,100,-1);
this->m_List.InsertColumn(3,"两路频率差(Hz)",LVCFMT_LEFT,100,-1);
this->m_List.InsertColumn(4,"加速度(mg)",LVCFMT_LEFT,100,-1);
this->m_List.InsertColumn(5,"间隔时间(s)",LVCFMT_LEFT,100,-1);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CItem4Dlg::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 CItem4Dlg::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 CItem4Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CItem4Dlg::OnButtonOpencom()
{
// TODO: Add your control notification handler code here
CButton *pBtn=(CButton*)this->GetDlgItem(IDC_BUTTON_OPENCOM);
static bool tip=true;
if(tip)
{
this->m_List.DeleteAllItems( );
this->number=0;
this->m_progress.SetPos(0);
this->once=true;
CString onesecond=mNowTime;
mNowTime=ReadNowTime();
if(onesecond==mNowTime){mNowTime=onesecond;return;}
m_ctrlComm.SetCommPort(2);
m_ctrlComm.SetInputMode(1);
m_ctrlComm.SetInBufferSize(1024);
m_ctrlComm.SetSettings("38400,n,8,1");
if(!m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(TRUE);
m_ctrlComm.SetRThreshold(1);
m_ctrlComm.SetInputLen(0);
m_ctrlComm.GetInput();
pBtn->SetWindowText("关闭串口");
tip=false;
}
else
{
if(m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(FALSE);
pBtn->SetWindowText("打开串口");
tip=true;
}
}
BEGIN_EVENTSINK_MAP(CItem4Dlg, CDialog)
//{{AFX_EVENTSINK_MAP(CItem4Dlg)
ON_EVENT(CItem4Dlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CItem4Dlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048];
static bool tip1=false,tip2=false;
static int i=0;
if(this->m_ctrlComm.GetCommEvent()==2)
{
variant_inp=this->m_ctrlComm.GetInput();
safearray_inp=variant_inp;
len=safearray_inp.GetOneDimSize();
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);
for(k=0;k<len;k++)
{
char bt=*(char*)(rxdata+k);
if(bt=='*')tip1=true;
else if(bt!='*' && tip1==true){tip1=false;tip2=true;}
if(tip2){str[i++]=bt;}
if(i==17)
{
tip2=false;i=0;
this->number++;
AfxBeginThread((AFX_THREADPROC)TestThread,this);
}
}
UpdateData(FALSE);
}
}
void CItem4Dlg::CreateTable()
{
_ConnectionPtr m_pConnection;
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance("ADODB.Connection");
//创建Connection对象
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\频率测量数据库.mdb","","",adModeUnknown);
//连接数据库
}
}
catch(_com_error *e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败:%s",e->ErrorMessage());
AfxMessageBox(errormessage);
//显示错误信息
}
_variant_t RecordsAffected;
CString strSQL="CREATE TABLE "+mNowTime+"(序号 INTEGER,a路频率Hz SINGLE,b路频率Hz SINGLE,两路频率差Hz SINGLE,加速度mg SINGLE,间隔时间s SINGLE)";
m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
}
CString CItem4Dlg::ReadNowTime()
{
char tmpbuf[50],temp[15];
CString strtime;
_strdate( tmpbuf );
temp[0]=tmpbuf[0];temp[1]=tmpbuf[1];temp[2]='_';temp[3]=tmpbuf[3];temp[4]=tmpbuf[4];
_strtime( tmpbuf );
temp[5]='_';temp[6]=tmpbuf[0];temp[7]=tmpbuf[1];temp[8]='_';temp[9]=tmpbuf[3];temp[10]=tmpbuf[4];temp[11]='_';temp[12]=tmpbuf[6];temp[13]=tmpbuf[7];
temp[14]='\0';
strtime=temp;
return strtime;
}
void CItem4Dlg::OnMenuitemAfreq()
{
// TODO: Add your command handler code here
if (this->mpAFreqDialog->GetSafeHwnd() == 0)
{
this->mpAFreqDialog->Create();
}
this->mpAFreqDialog->ShowWindow(SW_SHOW);
}
CItem4Dlg::~CItem4Dlg()
{
delete this->mpAFreqDialog;
}
LRESULT CItem4Dlg::OnGoodbye(WPARAM wParam, LPARAM lParam)
{
this->mpAFreqDialog->DestroyWindow();
return 0L;
}
UINT TestThread(LPVOID pParam)
{
CItem4Dlg* tttt=(CItem4Dlg*)pParam;
tttt->mDataFreq.SetAll(tttt->number,tttt->str);
tttt->mDRAW.SetData(tttt->mDataFreq.GetmID(),tttt->mDataFreq.GetmAFreq());
tttt->mpAFreqDialog->Draw(tttt->mDRAW.GetPoint());
int pos;
pos=tttt->m_List.InsertItem((int)(tttt->mDataFreq.GetmID()-1),tttt->mDataFreq.GetmIDCString());
tttt->m_List.SetItemText(pos,1,tttt->mDataFreq.GetmAfreqCString());
tttt->m_List.SetItemText(pos,2,tttt->mDataFreq.GetmBFreqCString());
tttt->m_List.SetItemText(pos,3,tttt->mDataFreq.GetmFreqCString());
tttt->m_List.SetItemText(pos,4,tttt->mDataFreq.GetmAccCString());
tttt->m_List.SetItemText(pos,5,tttt->mDataFreq.GetmTimeCString());
tttt->m_List.EnsureVisible(tttt->mDataFreq.GetmID()-1,0);
return 0;
}
void CItem4Dlg::OnButtonDatabase()
{
// TODO: Add your control notification handler code here
if(number<1){MessageBox("还没有采集数据^_^ ","tcm提示你",MB_OK);return;}
if(!once){MessageBox("你先前已经写入数据库^_^ ","tcm提示你",MB_OK);return;}
CreateTable();
CString data[6];
//number;
this->m_progress.SetRange(0,number);
this->m_progress.SetStep(1);
for(int i=0;i<number;i++)
{
for(int j=0;j<6;j++)
data[j]=this->m_List.GetItemText(i,j);
this->mDataFreq.SetAll(data);
this->mDataFreq.WriteTable(this->mNowTime);
this->m_progress.StepIt();
}
MessageBox("数据在 \"C:\\频率测量数据库\",表名为开始采集时的时间。写入完毕^_^ ","tcm恭喜你",MB_OK);
once=false;
}
void CItem4Dlg::OnMenuitemA()
{
// TODO: Add your command handler code here
if(once){MessageBox("先得把数据写入数据库才能执行此操作","tcm提示你",MB_OK);return;}
CADraw mADraw;
mADraw.GetTableNameAndID(mNowTime,0);
mADraw.m_q="A路频率的准确值(Hz):";
mADraw.DoModal();
}
void CItem4Dlg::OnMenuitemB()
{
// TODO: Add your command handler code here
if(once){MessageBox("先得把数据写入数据库才能执行此操作","tcm提示你",MB_OK);return;}
CADraw mADraw;
mADraw.GetTableNameAndID(mNowTime,1);
mADraw.m_q="B路频率的准确值(Hz):";
mADraw.DoModal();
}
void CItem4Dlg::OnMenuitemC()
{
// TODO: Add your command handler code here
if(once){MessageBox("先得把数据写入数据库才能执行此操作","tcm提示你",MB_OK);return;}
CADraw mADraw;
mADraw.GetTableNameAndID(mNowTime,2);
//mADraw.SetWindowText(dd);
mADraw.m_q="频率差的准确值(Hz):";
mADraw.DoModal();
}
void CItem4Dlg::OnMenuitemHelp()
{
// TODO: Add your command handler code here
CHELP mHelp;
mHelp.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -