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

📄 选题系统dlg.cpp

📁 这是一个用MFC做的选题系统
💻 CPP
字号:
// 选题系统Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "选题系统.h"
#include "选题系统Dlg.h"
#include "Inputxuehaoandnamedlg.h"
#include "ResultSheet.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)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
 
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_xuesheng = _T("");
	m_number = 10;
	m_totaltitles = 6;
	m_totalnumber = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_ANIMATE1, m_animate);
	DDX_Control(pDX, IDC_XUESHENGLIST, m_xueshenglist);
	DDX_Control(pDX, IDC_STICKS, m_sliderctrl);
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	DDX_Text(pDX, IDC_XUESHENG, m_xuesheng);
	DDX_Text(pDX, IDC_NUMBER, m_number);
	DDX_Text(pDX, IDC_TOTALTITLES, m_totaltitles);
	DDV_MinMaxInt(pDX, m_totaltitles, 3, 10);
	DDX_Text(pDX, IDC_TOTALNUMBER, m_totalnumber);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_LBN_DBLCLK(IDC_XUESHENGLIST, OnDblclkXueshengList)
	ON_BN_CLICKED(IDC_BUTTON_BEGIN, OnButtonBegin)
	ON_BN_CLICKED(IDC_BUTTON_JIEGUO, OnButtonJieguo)
	ON_BN_CLICKED(IDC_BUTTON_INPUT, OnButtonInput)
	ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_WM_CTLCOLOR()
	ON_WM_TIMER()
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_STICKS, OnReleasedcaptureSticks)
	ON_EN_KILLFOCUS(IDC_NUMBER, OnKillfocusNumber)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	
	// TODO: Add extra initialization here
   ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN1))->SetRange(3,10);
   m_sliderctrl.SetRange (2,20);
   m_sliderctrl.SetPos (m_number);
   m_resultbutton.AutoLoad (IDC_BUTTON_JIEGUO,this);
   TiHao=1;   
   Number=0; 
   Total=0;   
   srand((unsigned)time(NULL));
   if(m_animate.Open ("FILECOPY.avi")==FALSE)
		MessageBox("该文件不存在");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnDblclkXueshengList() 
{
	// TODO: Add your control notification handler code here
	int i=m_xueshenglist.GetCurSel ();
	CString name,xuehao,xuehaoold;
	m_xueshenglist.GetText (i,xuehaoold);	
	xueshengrecord.Lookup (xuehaoold,name);
	CInputxuehaoandnamedlg dlg;	
	dlg.m_text ="更改学号";
	dlg.m_xuehaoandname =xuehaoold;
	if(dlg.DoModal ()==IDOK)
	{
     
		xuehao=dlg.m_xuehaoandname ;		
		
	}
	else
		return;
	dlg.m_text ="更改姓名";
	dlg.m_xuehaoandname =name;
	if(dlg.DoModal ()==IDOK)
	{

		xueshengrecord.RemoveKey (xuehaoold);
		xueshengrecord.SetAt(xuehao,dlg.m_xuehaoandname );
		m_xueshenglist.DeleteString (i);
		m_xueshenglist.InsertString (i,xuehao);
	}


}

void CMyDlg::OnButtonBegin() 
{
	// TODO: Add your control notification handler code here
	CString str;	
	UpdateData(TRUE);
	if((m_totaltitles>10)||(m_totaltitles<2))
		return;
	GetDlgItem(IDC_BUTTON_BEGIN)->GetWindowText(str);	
	
	if(str.Find ("暂停")<0)
	{
		KillTimer(1);
		GetDlgItem(IDC_BUTTON_BEGIN)->SetWindowText("暂停");
		GetDlgItemText(IDC_XUESHENG,m_xuesheng);		
		int index=m_xueshenglist.FindString (-1,m_xuesheng);
		str.Format ("%d",TiHao);
		resultmap.SetAt (m_xuesheng,TiHao);
		int count=resultmap.GetCount ();
		if((TiHao==1) && (Number==0))
		{
	    m_progress.SetRange (1,Total);
	    m_progress.SetPos (1);
	    m_progress.SetStep (1);
		}
        m_progress.StepIt ();
		m_xueshenglist.DeleteString (index);
		Number++;
		renshu_timu[TiHao]=Number;
		
		if(Number>=m_number)
		{
	//		renshu_timu[TiHao]=m_number;
			TiHao++;
			Number=0;
		}
	}
	else
	{
	 if(TiHao==m_totaltitles)
	 {
		 
		 KillTimer(1);		 
		 GetDlgItem(IDC_BUTTON_BEGIN)->SetWindowText("选题结束");
		 GetDlgItem(IDC_BUTTON_BEGIN)->EnableWindow(FALSE);
		 m_progress.SetPos (Total);
		 Total=0;
		 //剩下的人做最后一题		  
		 int num=m_xueshenglist.GetCount ();
		 renshu_timu[TiHao]=num;
	
		 for(int i=0;i<num;i++)
		 {
			 m_xueshenglist.GetText (i,m_xuesheng);	
			 resultmap.SetAt (m_xuesheng,TiHao);
		 }
		 m_xueshenglist.ResetContent ();
		 
	 }
	 else
	 { 
	 str.Format ("选第%d题",TiHao);
	 GetDlgItem(IDC_BUTTON_BEGIN)->SetWindowText(str);
	 SetTimer(1,10,NULL);
	}
	}
}

void CMyDlg::OnButtonJieguo() 
{
	// TODO: Add your control notification handler code here
	CResultSheet sheet("选题结果",this,0);
	sheet.DoModal ();
	
}



void CMyDlg::OnButtonInput() 
{
	// TODO: Add your control notification handler code here
	if(Total==0)
	{
		GetDlgItem(IDC_BUTTON_BEGIN)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_BEGIN)->SetWindowText("暂停");
	}

	CInputxuehaoandnamedlg dlg;
    CString str,xuehao;	
	while(1)
	{
	dlg.m_text ="输入学号";
	if(dlg.DoModal ()==IDOK)
	{
     
		xuehao=dlg.m_xuehaoandname ;		
		if(xueshengrecord.Lookup (xuehao,str)) //有相同
		{
			MessageBox("有相同的学号","输入错误");
			continue;
		}		  	 
		
	}
	else
		return;
	dlg.m_text ="输入姓名";
	if(dlg.DoModal ()==IDOK)
	{
        
		xueshengrecord.SetAt(xuehao,dlg.m_xuehaoandname );
		m_xueshenglist.AddString (xuehao);
		Total++;
		SetDlgItemInt(IDC_TOTALNUMBER,Total);
	}
	else
	  return;
	}
}

void CMyDlg::OnButtonRead() 
{
	// TODO: Add your control notification handler code here
	struct STUDENT 
	{
		char xuehao[11];
		char name[9];
		int index;
	}student;	
	CString str;
	char szFilter[]="Result Files(*.res)|*.res||";
    CFileDialog  filedlg (TRUE,"res",NULL,  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);
	filedlg.m_ofn.lpstrTitle ="读学生数据文件";	
    if(filedlg.DoModal()==IDOK)
	{
		CFile file;
		file.Open (filedlg.GetFileName (),CFile::modeRead   );
		int len=(int)(file.GetLength ()/sizeof(struct STUDENT));
		m_xueshenglist.ResetContent ();
		xueshengrecord.RemoveAll ();
		for(int i=0;i<len;i++)
		{
			file.Read (&student,sizeof(struct STUDENT));			
		    m_xueshenglist.AddString (student.xuehao );			
			xueshengrecord.SetAt (student.xuehao ,student.name );
		}
		file.Close ();
		SetDlgItemInt(IDC_SUM,len); 
		Total=len;
		GetDlgItem(IDC_BUTTON_BEGIN)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_BEGIN)->SetWindowText("暂停");
	}
	
}

void CMyDlg::OnButtonSave() 
{
	// TODO: Add your control notification handler code here

	
	int num,i;
	char szFilter[]="Result Files(*.res)|*.res||";
    CFileDialog  filedlg (FALSE,"res",NULL,  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);



	filedlg.m_ofn.lpstrTitle ="保存学生数据文件";	
    if(filedlg.DoModal()==IDOK)
	{
		CFile file;		
		file.Open (filedlg.GetFileName (),CFile::modeCreate|CFile::modeWrite);
		num=xueshengrecord.GetCount ();
		struct STUDENT 
		{ 
		char xuehao[11];
		char name[9];
		int index;
		}student;
		CString str,xuehao;
		
		POSITION ps;
		ps=xueshengrecord.GetStartPosition ();
		for(i=0;i<num;i++)
		{
			xueshengrecord.GetNextAssoc (ps,xuehao,str);			
			strcpy(student.xuehao ,xuehao);
			strcpy(student.name,str);		
			resultmap.Lookup (xuehao ,student.index  );
		//	strcpy(stu.index,str);
			file.Write (&student,sizeof(struct STUDENT));
		}
		file.Close ();
	
	}
}



HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(nCtlColor==CTLCOLOR_EDIT )
	{
      	pDC->SetTextColor (RGB(255,255,0));
	
	}
	/*if(pWnd->GetDlgCtrlID ()==IDC_XUESHENG)
	{
		pDC->SetTextColor (RGB(255,0,0));
		CBrush brush;
		pDC->SetBkMode (TRANSPARENT);
		hbr=::CreateSolidBrush (RGB(255,255,255));
		
	}
	if(pWnd->GetDlgCtrlID ()==IDC_TOTALNUMBER)
	{
      pDC->SetTextColor (RGB(255,0,0));
	}*/
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CMyDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
    
	int num=m_xueshenglist.GetCount ();
	int index;
	index=(long)rand()*num/RAND_MAX;
	if(index<num)
	{
	m_xueshenglist.GetText (index,m_xuesheng);
	m_xueshenglist.SetTopIndex (index);
	m_xueshenglist.SetCurSel (index);
	SetDlgItemText(IDC_XUESHENG,m_xuesheng);	
	}
	static int nNum=0;
	//显示一帧AVI片段
	m_animate.Seek (nNum);
    m_animate.Play (nNum,nNum+1,1);
    nNum++;
	if(nNum==17)  //FILECOPY.AVI文件有17帧
		nNum=0;
    CDialog::OnTimer(nIDEvent);
}

void CMyDlg::OnReleasedcaptureSticks(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	m_number=m_sliderctrl.GetPos ();
	SetDlgItemInt(IDC_NUMBER,m_number);
	*pResult = 0;
}



void CMyDlg::OnKillfocusNumber() 
{
	// TODO: Add your control notification handler code here
	m_number=GetDlgItemInt(IDC_NUMBER);
	m_sliderctrl.SetPos (m_number);
}

⌨️ 快捷键说明

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