一、软件界面
二、源码
// demoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "demo.h"
#include "demoDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CdemoDlg 对话框
CdemoDlg::CdemoDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CdemoDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CdemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list1);
DDX_Control(pDX, IDC_LIST2, m_list2);
}
BEGIN_MESSAGE_MAP(CdemoDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_LONGIN, &CdemoDlg::OnBnClickedBtnLongin)
ON_BN_CLICKED(IDC_BTN_TEST, &CdemoDlg::OnBnClickedBtnTest)
END_MESSAGE_MAP()
// CdemoDlg 消息处理程序
BOOL CdemoDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
iniUI();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CdemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CdemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CdemoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
#define SS_DEBUG
void CdemoDlg::iniUI()
{
#ifdef SS_DEBUG
SetDlgItemText(IDC_EDIT_SETVERIP, L"192.168.0.145");
SetDlgItemText(IDC_EDIT_DATABASE, L"LeiMan");
SetDlgItemText(IDC_EDIT_PORT, L"1433");
SetDlgItemText(IDC_EDIT_USERID, L"sc");
SetDlgItemText(IDC_EDIT_PWD, L"000000");
SetDlgItemText(IDC_EDIT_ODER, L"固晶");
SetDlgItemText(IDC_EDIT_BARCODE, L"692345065");
#else
SetDlgItemText(IDC_EDIT_SETVERIP, L"192.168.1.10");
SetDlgItemText(IDC_EDIT_DATABASE, L"lmgd");
SetDlgItemText(IDC_EDIT_PORT, L"1433");
SetDlgItemText(IDC_EDIT_USERID, L"LJ");
SetDlgItemText(IDC_EDIT_PWD, L"lm2019");
SetDlgItemText(IDC_EDIT_ODER, L"固晶");
SetDlgItemText(IDC_EDIT_BARCODE, L"");
#endif
SetDlgItemText(IDC_EDIT_TIME1, L"0秒");
SetDlgItemText(IDC_EDIT_TIME2, L"0秒");
((CButton*)GetDlgItem(IDC_BTN_TEST))->EnableWindow(FALSE);
}
void CdemoDlg::OnBnClickedBtnLongin()
{
if (m_database.IsOpen()) m_database.Close();
CString sIP;
CString sDatabase;
CString sPort;
CString sUserID;
CString sPwd;
GetDlgItemText(IDC_EDIT_SETVERIP, sIP);
GetDlgItemText(IDC_EDIT_DATABASE, sDatabase);
GetDlgItemText(IDC_EDIT_PORT, sPort);
GetDlgItemText(IDC_EDIT_USERID, sUserID);
GetDlgItemText(IDC_EDIT_PWD, sPwd);
CString sSql;
sSql.Format(L"DRIVER={SQL SERVER};SERVER=%s,%s;DATABASE=%s;UID=%s;PWD=%s", sIP, sPort, sDatabase, sUserID, sPwd);
if(!m_database.OpenEx(sSql, CDatabase::noOdbcDialog))
{
AfxMessageBox(L"登录数据库失败,请检查用户名、密码是否正确");
((CButton*)GetDlgItem(IDC_BTN_TEST))->EnableWindow(FALSE);
}
else
{
AfxMessageBox(L"登录数据成功");
((CButton*)GetDlgItem(IDC_BTN_TEST))->EnableWindow(TRUE);
}
}
void CdemoDlg::OnBnClickedBtnTest()
{
if (!m_database.IsOpen())
{
AfxMessageBox(L"请先登录数据库");
return;
}
//1. 获取测试工序 和 条码
CString sOder;
CString sBarCode;
GetDlgItemText(IDC_EDIT_ODER, sOder);
GetDlgItemText(IDC_EDIT_BARCODE, sBarCode);
if (sOder.IsEmpty())
{
AfxMessageBox(L"测试工序不能为空");
return;
}
if (sBarCode.IsEmpty())
{
AfxMessageBox(L"测试条码不能为空");
return;
}
test1(sOder, sBarCode);
test2(sOder, sBarCode);
}
void CdemoDlg::test1(CString &sOder, CString &sBarCode)
{
int nColumnCount = m_list1.GetHeaderCtrl()->GetItemCount();
for (int i = nColumnCount - 1; i >= 0; i--) m_list1.DeleteColumn(i);
m_list1.DeleteAllItems();
DWORD dwStyle = m_list1.GetExtendedStyle();
dwStyle |= LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER;
m_list1.SetExtendedStyle(dwStyle);
m_list1.InsertColumn(0, L"序 号", LVCFMT_CENTER, 80);
m_list1.InsertColumn(1, L"x坐标", LVCFMT_CENTER, 80);
m_list1.InsertColumn(2, L"y坐标", LVCFMT_CENTER, 80);
clock_t start = clock();
#ifdef SS_DEBUG
CString sSql;
sSql.Format(L"SELECT TOP 1 操作时间 FROM COB WHERE 工序 = '%s' AND 条码号 = '%s'", sOder, sBarCode);
CRecordset recordset(&m_database);//绑定记录集
recordset.Open(CRecordset::none, sSql, CRecordset::none);//执行查询语句strSQL
CString sTemp;
recordset.GetFieldValue(_T("操作时间"), sTemp);
recordset.Close();
CString sSql2;
sSql2.Format(L"SELECT x坐标, y坐标 FROM COB WHERE (工序 = '%s') AND (条码号 = '%s') AND 操作时间= '%s'", sOder, sBarCode, sTemp);
#else
CString sSql;
sSql.Format(L"SELECT TOP 1 操作时间 FROM COB条码不良信息表_明细 WHERE 工序 = '%s' AND 条码号 = '%s'", sOder, sBarCode);
CRecordset recordset(&m_database);//绑定记录集
recordset.Open(CRecordset::none, sSql, CRecordset::none);//执行查询语句strSQL
CString sTemp;
recordset.GetFieldValue(_T("操作时间"), sTemp);
recordset.Close();
CString sSql2;
sSql2.Format(L"SELECT x坐标, y坐标 FROM COB条码不良信息表_明细 WHERE (工序 = '%s') AND (条码号 = '%s') AND 操作时间= '%s'", sOder, sBarCode, sTemp);
#endif
CRecordset recordset2(&m_database);//绑定记录集
recordset2.Open(CRecordset::none, sSql2, CRecordset::none);//执行查询语句strSQL
clock_t finish = clock();
float duration = (double)(finish - start) / CLOCKS_PER_SEC;
CString sTime;
sTime.Format(L"%.4f秒", duration);
SetDlgItemText(IDC_EDIT_TIME1, sTime);
if(!recordset2.CanUpdate()) AfxMessageBox(L"查询数据失败");
int iCount = 0;
while(!recordset2.IsEOF())//判断是否到记录集末尾
{
char buf[1024] = {0};
int iRow = m_list1.GetItemCount();
int iIndex = m_list1.InsertItem(iRow, CString(itoa(iRow+1, buf, 10)));
CString sPosX, sPosY;
recordset2.GetFieldValue(_T("x坐标"), sPosX);
recordset2.GetFieldValue(_T("y坐标"), sPosY);
m_list1.SetItemText(iIndex, 1, sPosX);
m_list1.SetItemText(iIndex, 2, sPosY);
recordset2.MoveNext();//移动到记录集的下一行
iCount++;
if (iCount > 5) break;
}
recordset2.Close();
}
void CdemoDlg::test2(CString &sOder, CString &sBarCode)
{
int nColumnCount = m_list2.GetHeaderCtrl()->GetItemCount();
for (int i = nColumnCount - 1; i >= 0; i--) m_list2.DeleteColumn(i);
m_list2.DeleteAllItems();
DWORD dwStyle = m_list2.GetExtendedStyle();
dwStyle |= LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER;
m_list2.SetExtendedStyle(dwStyle);
m_list2.InsertColumn(0, L"序 号", LVCFMT_CENTER, 80);
m_list2.InsertColumn(1, L"x坐标", LVCFMT_CENTER, 80);
m_list2.InsertColumn(2, L"y坐标", LVCFMT_CENTER, 80);
#ifdef SS_DEBUG
CString sSql;
sSql.Format(L"SELECT x坐标, y坐标 FROM COB WHERE (工序 = '%s') AND (条码号 = '%s') AND \
操作时间=(SELECT TOP (1) 操作时间 FROM COB WHERE (工序 = '%s') AND (条码号 = '%s') order by 操作时间 desc)", sOder, sBarCode, sOder, sBarCode);
#else
CString sSql;
sSql.Format(L"SELECT x坐标, y坐标 FROM COB条码不良信息表_明细 WHERE (工序 = '%s') AND (条码号 = '%s') AND \
操作时间=(SELECT TOP (1) 操作时间 FROM COB条码不良信息表_明细 WHERE (工序 = '%s') AND (条码号 = '%s') order by 操作时间 desc)", sOder, sBarCode, sOder, sBarCode);
#endif
clock_t start = clock();
CRecordset recordset(&m_database);//绑定记录集
recordset.Open(CRecordset::none, sSql, CRecordset::none);//执行查询语句strSQL
clock_t finish = clock();
float duration = (double)(finish - start) / CLOCKS_PER_SEC;
CString sTime;
sTime.Format(L"%.4f秒", duration);
SetDlgItemText(IDC_EDIT_TIME2, sTime);
if(!recordset.CanUpdate()) AfxMessageBox(L"查询数据失败");
int iCount = 0;
while(!recordset.IsEOF())//判断是否到记录集末尾
{
char buf[1024] = {0};
int iRow = m_list2.GetItemCount();
int iIndex = m_list2.InsertItem(iRow, CString(itoa(iRow+1, buf, 10)));
CString sPosX, sPosY;
recordset.GetFieldValue(_T("x坐标"), sPosX);
recordset.GetFieldValue(_T("y坐标"), sPosY);
m_list2.SetItemText(iIndex, 1, sPosX);
m_list2.SetItemText(iIndex, 2, sPosY);
recordset.MoveNext();//移动到记录集的下一行
iCount++;
if (iCount > 5) break;
}
recordset.Close();
}
三、测试结果
来源:CSDN
作者:mark-puls
链接:https://blog.csdn.net/cwj066/article/details/104820791