⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 st6dlg.cpp

📁 自已做的简单的库存管理软件。包含员工信息管理等
💻 CPP
字号:
// st6Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "st6.h"
#include "st6Dlg.h"
#include <stdlib.h>
#include "MySqlConnect.h"
#include "Mythread.h"
#include "SerialControl.h"
#include "myhead.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif





//UINT MfcThreadProc(LPVOID lpParam);



// Cst6Dlg 对话框



Cst6Dlg::Cst6Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(Cst6Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    str_db_server = _T("");
	str_db_name = _T("");
	str_db_user = _T("");
	str_db_password = _T("");

	CStdioFile f2;
	if( !f2.Open( STR_SET_FILE, CFile::modeRead ) ) 
	{
		//afxDump << "Unable to open file" << "\n";
	}
	else
	{
		CString   strLine = _T("");
		f2.ReadString(strLine);
		f2.Close();
		
		CString temp = _T(" ");
		int len = strLine.GetLength();
		CString str_tmp[16];
		for ( int i = 0; i < 16; i++ )
		{
			str_tmp[i] = _T("");
		}

		int j = 0;
		
		for ( int i = 0 ; i < len ; i ++ )
		{
			if ( strLine[i] != temp )
			{
				str_tmp[j] = str_tmp[j] + strLine[i];
			}
			else
			{
				j++;
			}
		}
		str_db_server = str_tmp[0];
		str_db_name = str_tmp[1];
		str_db_user = str_tmp[2];
		str_db_password = str_tmp[3];
	}

	//OpenSerialPort1();
}

void Cst6Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	DDX_Text(pDX, IDC_EDIT_DB_SERVER, str_db_server);
	DDX_Text(pDX, IDC_EDIT_DB_NAME, str_db_name);
	DDX_Text(pDX, IDC_EDIT_DB_USER, str_db_user);
	DDX_Text(pDX, IDC_EDIT_DB_PASSWORD, str_db_password);
}

BEGIN_MESSAGE_MAP(Cst6Dlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &Cst6Dlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &Cst6Dlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON_TEST, &Cst6Dlg::OnBnClickedButtonTest)
END_MESSAGE_MAP()


// Cst6Dlg 消息处理程序

BOOL Cst6Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	//启动串口监视线程
	DWORD threadID;
	hCommThread = ::CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0,
		(LPTHREAD_START_ROUTINE)SerialPort1ThreadProcess, AfxGetMainWnd()->m_hWnd, 0, &threadID);
	if (hCommThread == NULL)
	{
		::AfxMessageBox(_T("创建串口1处理线程失败"));
		::PostQuitMessage(0);
	}

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void Cst6Dlg::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
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR Cst6Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void Cst6Dlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	   
	
	
	UpdateData(TRUE); //读取编辑框内容
	if ( str_db_server.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入数据库服务器名称!"));
		return;
	}
	if ( str_db_name.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入数据库名称!"));
		return;
	}
	if ( str_db_user.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入访问数据库的用户名!"));
		return;
	}



	MySqlConnect sqlconnect;
	/*CString   str;     
	int   i   =  1;     	
	str.Format(_T("%d"),i);    //int转化为CString   
	sqlconnect.InitSQLServer( _T("MARK"), _T("wirss_a"), _T("sa"), str);*/

	if ( sqlconnect.InitSQLServer( str_db_server,str_db_name, str_db_user, str_db_password ) )
	{
		
		
		//Mythread *pThread;
		//pThread = (Mythread*) AfxBeginThread( RUNTIME_CLASS(Mythread) );
		
		//使用默认参数,由于CGUIThread自动删除,所以不需要保存该指针
		
		
		//AfxBeginThread(MfcThreadProc,NULL);
		
		
		
		//以下代码为读写文本文件
		CStdioFile mfile; 
		CString str2= str_db_server + _T(" ") + str_db_name + _T(" ") + str_db_user + _T(" ") + str_db_password + _T(" "); 
		if ( !mfile.Open(STR_SET_FILE,CFile::modeCreate | CFile::modeReadWrite))
		{
			AfxMessageBox(_T("创建出错文件出错!"));
			return;
		}
		mfile.WriteString(str2); 
		mfile.Close(); 
		
		
		::MessageBox(NULL,_T("保存文件成功,请重启系统"),(LPCTSTR)(_T("提示")),MB_OK);
		OnOK();
	}

}

void Cst6Dlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here


	OnCancel();
}


/*
//线程控制函数
UINT MfcThreadProc(LPVOID lpParam)
{
	int i = 0;
	while(1)
	{
		i++;
		if (i > 4000)
		{
			i = 0;

		}

	}
}
*/
void Cst6Dlg::OnBnClickedButtonTest()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE); //读取编辑框内容
	if ( str_db_server.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入数据库服务器名称!"));
		return;
	}
	if ( str_db_name.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入数据库名称!"));
		return;
	}
	if ( str_db_user.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入访问数据库的用户名!"));
		return;
	}



	MySqlConnect sqlconnect;
	/*CString   str;     
	int   i   =  1;     	
	str.Format(_T("%d"),i);    //int转化为CString   
	sqlconnect.InitSQLServer( _T("MARK"), _T("wirss_a"), _T("sa"), str);*/

	if ( sqlconnect.InitSQLServer( str_db_server,str_db_name, str_db_user, str_db_password ) )
	{
		
		
		//Mythread *pThread;
		//pThread = (Mythread*) AfxBeginThread( RUNTIME_CLASS(Mythread) );
		
		//使用默认参数,由于CGUIThread自动删除,所以不需要保存该指针
		
		
		//AfxBeginThread(MfcThreadProc,NULL);
		
		
		
		//以下代码为读写文本文件
		CStdioFile mfile; 
		CString str2= str_db_server + _T(" ") + str_db_name + _T(" ") + str_db_user + _T(" ") + str_db_password + _T(" "); 
		if ( !mfile.Open( STR_SET_FILE,CFile::modeCreate | CFile::modeReadWrite))
		{
			AfxMessageBox(_T("创建出错文件出错!"));
			return;
		}
		mfile.WriteString(str2); 
		mfile.Close(); 
		
		
		::MessageBox(NULL,_T("测试数据库连接成功,请保存设置重启系统"),(LPCTSTR)(_T("提示")),MB_OK);

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -