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

📄 gasstationdlg.cpp

📁 只是老师布置的一个作业而已。VC下编写的加油站系统
💻 CPP
字号:
 // GasStationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GasStation.h"
#include "GasStationDlg.h"

// 各功能模块的对话框
#include "SessionDlg.h"
#include "LoginDlg.h"
#include "WorkerMgrDlg.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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGasStationDlg dialog

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

void CGasStationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGasStationDlg)
	DDX_Control(pDX, ID_WORKER_MGR, m_btnWorkerMgr);
	DDX_Control(pDX, ID_STORAGE_QUERY, m_btnStorageQuery);
	DDX_Control(pDX, ID_STOCK_RECORD, m_btnStockRecord);
	DDX_Control(pDX, ID_SELL_RECORD, m_btnSellRecord);
	DDX_Control(pDX, ID_MEMBER_MGR, m_btnMemberMgr);
	DDX_Control(pDX, ID_GAS_TYPE_MGR, m_btnGasTypeMgr);
	DDX_Control(pDX, ID_DEPOT_MGR, m_btnDepotMgr);
	DDX_Control(pDX, ID_ACCOUNT_MGR, m_btnAccountMgr);
	DDX_Control(pDX, ID_LOGIN, m_btnLogin);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGasStationDlg, CDialog)
	//{{AFX_MSG_MAP(CGasStationDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_LOGIN, OnLogin)
	ON_BN_CLICKED(ID_WORKER_MGR, OnWorkerMgr)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(ID_DEPOT_MGR, OnDepotMgr)
	ON_BN_CLICKED(ID_MEMBER_MGR, OnMemberMgr)
	ON_BN_CLICKED(ID_GAS_TYPE_MGR, OnGasTypeMgr)
	ON_BN_CLICKED(ID_STOCK_RECORD, OnStockRecord)
	ON_BN_CLICKED(ID_SELL_RECORD, OnSellRecord)
	ON_BN_CLICKED(ID_STORAGE_QUERY, OnStorageQuery)
	ON_BN_CLICKED(ID_ACCOUNT_MGR, OnAccountMgr)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGasStationDlg message handlers

BOOL CGasStationDlg::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);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	if (!InitChildDialog()) {
		MB_ERROR("初始化子对话框失败!");
		EndDialog(0);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGasStationDlg::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 CGasStationDlg::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();
	}
}

HCURSOR CGasStationDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGasStationDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	OnCancel();
}

void CGasStationDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	ConnectToSQLServer(FALSE);
	
	CDialog::OnOK();
}

void CGasStationDlg::OnLogin() 
{
	// TODO: Add your control notification handler code here
	CString strBtnText;
	m_btnLogin.GetWindowText(strBtnText);
	if ( strBtnText== "登陆") {
		CLoginDlg loginDlg;
		if (loginDlg.DoModal() != IDOK) 
			return ;

		if (!ADOSQLServer.IsValidWorkerPassword(loginDlg.m_nNumber, loginDlg.m_strPassword)) {
			MB_ERROR("密码错误,登陆失败!");
			return ;
		}
		switch (ADOSQLServer.GetWorkerType(loginDlg.m_nNumber)) {
		case 0:
			m_btnWorkerMgr.EnableWindow(FALSE);
			m_btnStorageQuery.EnableWindow(FALSE);
			m_btnStockRecord.EnableWindow(FALSE);
			m_btnSellRecord.EnableWindow(TRUE);
			m_btnMemberMgr.EnableWindow(FALSE);
			m_btnGasTypeMgr.EnableWindow(FALSE);
			m_btnDepotMgr.EnableWindow(FALSE);
			m_btnAccountMgr.EnableWindow(FALSE);

			break;
		case 1:
			m_btnWorkerMgr.EnableWindow(FALSE);
			m_btnStorageQuery.EnableWindow(FALSE);
			m_btnStockRecord.EnableWindow(TRUE);
			m_btnSellRecord.EnableWindow(FALSE);
			m_btnMemberMgr.EnableWindow(FALSE);
			m_btnGasTypeMgr.EnableWindow(FALSE);
			m_btnDepotMgr.EnableWindow(FALSE);
			m_btnAccountMgr.EnableWindow(FALSE);

			break;
		case 2:
			m_btnWorkerMgr.EnableWindow(FALSE);
			m_btnMemberMgr.EnableWindow(FALSE);
			m_btnGasTypeMgr.EnableWindow(FALSE);
			m_btnDepotMgr.EnableWindow(FALSE);
			m_btnStockRecord.EnableWindow(FALSE);
			m_btnSellRecord.EnableWindow(FALSE);
			m_btnStorageQuery.EnableWindow(TRUE);
			m_btnAccountMgr.EnableWindow(TRUE);
			
			break;
		case 3:
			m_btnWorkerMgr.EnableWindow(TRUE);
			m_btnMemberMgr.EnableWindow(TRUE);
			m_btnGasTypeMgr.EnableWindow(TRUE);
			m_btnDepotMgr.EnableWindow(TRUE);
			m_btnStockRecord.EnableWindow(TRUE);
			m_btnSellRecord.EnableWindow(TRUE);
			m_btnStorageQuery.EnableWindow(TRUE);
			m_btnAccountMgr.EnableWindow(TRUE);
			
			break;
		default:
			m_btnWorkerMgr.EnableWindow(FALSE);
			m_btnStorageQuery.EnableWindow(FALSE);
			m_btnStockRecord.EnableWindow(FALSE);
			m_btnSellRecord.EnableWindow(FALSE);
			m_btnMemberMgr.EnableWindow(FALSE);
			m_btnGasTypeMgr.EnableWindow(FALSE);
			m_btnDepotMgr.EnableWindow(FALSE);
			m_btnAccountMgr.EnableWindow(FALSE);
			
			return ;
		}

		m_btnLogin.SetWindowText("注销");
	}
	else {
		if (MB_QUERY("确定要注销当前用户么?") == IDNO)
			return ;
		m_btnLogin.SetWindowText("登陆");
		m_btnWorkerMgr.EnableWindow(FALSE);
		m_btnStorageQuery.EnableWindow(FALSE);
		m_btnStockRecord.EnableWindow(FALSE);
		m_btnSellRecord.EnableWindow(FALSE);
		m_btnMemberMgr.EnableWindow(FALSE);
		m_btnGasTypeMgr.EnableWindow(FALSE);
		m_btnDepotMgr.EnableWindow(FALSE);
		m_btnAccountMgr.EnableWindow(FALSE);
		m_btnLogin.EnableWindow(FALSE);
	}
}

BOOL CGasStationDlg::ConnectToSQLServer(BOOL bConnected)
{
	if (!bConnected) {
		ADOSQLServer.CloseRecordSet();
		ADOSQLServer.CloseConn();

		return TRUE;
	}

	CSessionDlg sessionDlg;
	if (sessionDlg.DoModal()!= IDOK) {
		return FALSE;
	}

	if(FAILED(ADOSQLServer.Init())) {
		MB_ERROR("无法初始化ADO对象,请重新启动程序!");
		return FALSE;
	}
	ADOSQLServer.SetDataSource((const char *)sessionDlg.m_lpServer);
	ADOSQLServer.SetInitialCatalog((const char *)sessionDlg.m_lpDatabase);
	ADOSQLServer.SetUserId((const char *)sessionDlg.m_lpUsername);
	ADOSQLServer.SetUserPassword((const char *)sessionDlg.m_lpPassword);
	ADOSQLServer.BuildConnString();
	
	if(FAILED(ADOSQLServer.ConnectToDB())) {
		MB_ERROR("连接数据库服务器失败,无法启动程序!");
		return FALSE;
	}

	return TRUE;
}

void CGasStationDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	static bool bFirstShow = true;

	if (bFirstShow) {
		if (!ConnectToSQLServer(TRUE)) {
			OnCancel();
		}
		bFirstShow = false;
	}
}

BOOL CGasStationDlg::InitChildDialog()
{
	if (!m_workerMgrDlg.Create(IDD_WORKER_MGR_DIALOG, this)) {
		return FALSE;
	}
	m_workerMgrDlg.ShowWindow(SW_HIDE);

	if (!m_depotMgrDlg.Create(IDD_DEPOT_MGR_DIALOG, this)) {
		return FALSE;
	}
	m_depotMgrDlg.ShowWindow(SW_HIDE);

	if (!m_memberMgrDlg.Create(IDD_MEMBER_MGR_DIALOG, this)) {
		return FALSE;
	}
	m_memberMgrDlg.ShowWindow(SW_HIDE);

	if (!m_gasTypeMgrDlg.Create(IDD_GAS_TYPE_MGR_DIALOG, this)) {
		return FALSE;
	}
	m_gasTypeMgrDlg.ShowWindow(SW_HIDE);

	if (!m_storageQueryDlg.Create(IDD_STORAGE_QUERY_DIALOG, this)) {
		return FALSE;
	}
	m_storageQueryDlg.ShowWindow(SW_HIDE);
	
	if (!m_accountMgrDlg.Create(IDD_ACCOUNT_MGR_DIALOG, this)) {
		return FALSE;
	}
	m_accountMgrDlg.ShowWindow(SW_HIDE);

	return TRUE;
}

void CGasStationDlg::OnWorkerMgr() 
{
	// TODO: Add your control notification handler code here
	m_workerMgrDlg.ShowWindow(SW_SHOW);
	m_workerMgrDlg.BringWindowToTop();
}

void CGasStationDlg::OnDepotMgr() 
{
	// TODO: Add your control notification handler code here
	m_depotMgrDlg.ShowWindow(SW_SHOW);
	m_depotMgrDlg.BringWindowToTop();
}

void CGasStationDlg::OnMemberMgr() 
{
	// TODO: Add your control notification handler code here	
	m_memberMgrDlg.ShowWindow(SW_SHOW);
	m_memberMgrDlg.BringWindowToTop();
}

void CGasStationDlg::OnGasTypeMgr() 
{
	// TODO: Add your control notification handler code here	
	m_gasTypeMgrDlg.ShowWindow(SW_SHOW);
	m_gasTypeMgrDlg.BringWindowToTop();
}

void CGasStationDlg::OnStockRecord() 
{
	// TODO: Add your control notification handler code here
	CStockRecordDlg stockRecordDlg;
	if (stockRecordDlg.DoModal() != IDOK) 
		return ;

	if (!ADOSQLServer.AddNewStockRecord(
		stockRecordDlg.m_strTime,
		stockRecordDlg.m_strGasType,
		stockRecordDlg.m_strDepot,
		stockRecordDlg.m_fAmount,
		stockRecordDlg.m_fPrice,
		stockRecordDlg.m_nWorker))
	{
		MB_ERROR("添加进货信息失败,可能是由于油库已满!");
	}
}

void CGasStationDlg::OnSellRecord() 
{
	// TODO: Add your control notification handler code here
	CSellRecordDlg sellRecordDlg;
	if (sellRecordDlg.DoModal() != IDOK) 
		return ;

if (!ADOSQLServer.AddNewSellRecord(
		sellRecordDlg.m_strTime,
		sellRecordDlg.m_strGasType,
		sellRecordDlg.m_strDepot,
		sellRecordDlg.m_fAmount,
		sellRecordDlg.m_fPrice,
		sellRecordDlg.m_nWorker,
		sellRecordDlg.m_nMember, 
		sellRecordDlg.m_fAgio))
	{
		MB_ERROR("添加销售信息失败,可能是由于油库没有足够的储油!");
	}	
}

void CGasStationDlg::OnStorageQuery() 
{
	// TODO: Add your control notification handler code here
	m_storageQueryDlg.ShowWindow(SW_SHOW);
	m_storageQueryDlg.BringWindowToTop();
}

void CGasStationDlg::OnAccountMgr() 
{
	// TODO: Add your control notification handler code here
	m_accountMgrDlg.ShowWindow(SW_SHOW);
	m_accountMgrDlg.BringWindowToTop();	
}

⌨️ 快捷键说明

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