-
Notifications
You must be signed in to change notification settings - Fork 1
/
VoltDlg.cpp
179 lines (135 loc) · 4.49 KB
/
VoltDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// VoltDlg.cpp : implementation file
//
#include "stdafx.h"
#include "轨道清洁检测车监控系统.h"
#include "VoltDlg.h"
#include "TeeChart/tchart.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVoltDlg dialog
CVoltDlg::CVoltDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVoltDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVoltDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
//初始化com环境
::CoInitialize(NULL);
//创建连接对象和记录集对象
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(
"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=清洁车;Data Source=WENDY-PC",
"","",adModeUnknown);///连接数据库
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
m_pRecordset.CreateInstance(__uuidof(Recordset));
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("SELECT COUNT(*) FROM 逆变器电压",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex); //取得第一个字段的值放入vCount变量
count=vCount.lVal; //获取记录集的记录数
m_pRecordset->Close(); //关闭记录集
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSql;
strSql.Format("SELECT * FROM 逆变器电压");
hr = m_pRecordset->Open(strSql.AllocSysString(),
m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
void CVoltDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVoltDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
DDX_Control(pDX,IDC_VOLT,m_Chart);
}
BEGIN_MESSAGE_MAP(CVoltDlg, CDialog)
//{{AFX_MSG_MAP(CVoltDlg)
ON_WM_DESTROY()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVoltDlg message handlers
BOOL CVoltDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_RecordNum = 0;
m_Chart.GetLegend().SetVisible(false);
m_Chart.GetAspect().SetView3D(FALSE);
//m_Chart.GetHeader().GetText().SetItem(0,COleVariant("hello"));
m_Chart.GetAxis().GetLeft().GetTitle().SetCaption("逆变器电压");
//定义坐标系空间
CSeries mycs0 =(CSeries)m_Chart.Series(0);
CAxes coord = (CAxes)m_Chart.GetAxis();
CAxis left0 = (CAxis)coord.GetLeft();
left0.SetMinimum(0);
left0.SetMaximum(10);
left0.SetIncrement(0.1);
left0.SetStartPosition(0);
left0.SetEndPosition(100);
left0.SetPositionPercent(0);
COleDateTime CurTime;
COleDateTimeSpan tmSpan;
CString csTime;
CurTime = COleDateTime::GetCurrentTime();
tmSpan = COleDateTimeSpan(0,0,0,1); //1s
CString str;
m_pRecordset->MoveFirst();
//先用前十条记录初始化
for(int i =0;i<10;i++){
str = (char*)(_bstr_t)m_pRecordset->GetCollect(_variant_t("逆变器电压"));
csTime= CurTime.Format("%H:%M:%S");
mycs0.Add(atof(str),csTime,RGB(255,0,0));
CurTime+= tmSpan;
m_pRecordset->MoveNext();
m_RecordNum++;
}
//m_RecordNum为9,已经初始化前十条记录
SetTimer(1,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CVoltDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
COleDateTime CurTime = COleDateTime::GetCurrentTime();
COleDateTimeSpan tmSpan = COleDateTimeSpan(0,0,0,1); //1s
CString csTime = CurTime.Format("%H:%M:%S");
CString str;
if(m_RecordNum==9) m_pRecordset->Move((long)m_RecordNum++);
if(nIDEvent == 1){
//使用这个可以让坐标轴动起来
str = (char*)(_bstr_t)m_pRecordset->GetCollect(_variant_t("逆变器电压"));
csTime= CurTime.Format("%H:%M:%S");
m_Chart.Series(0).Add(atof(str),csTime,RGB(255,0,0));
CurTime+= tmSpan;
m_Chart.GetAxis().GetBottom().Scroll(1.0,TRUE);
m_pRecordset->MoveNext();
if(m_pRecordset->adoEOF) {KillTimer(1);m_pRecordset->Close();MessageBox("END OF RECORD!");}
m_RecordNum++;
}
CDialog::OnTimer(nIDEvent);
}
void CVoltDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
::CoUninitialize();
}