其实本程序主要是熟悉了windows系统的应用程序基于消息机制的编程,同时熟悉了几个控件的操作,下面上主要的代码
新建一个基于DLG的mfc程序,加上下面的头文件
#include <iostream> using namespace std; //问题的结构体 struct Question { string question; string answer[4]; int right_answer; }; //定义全局变量 100个题目 Question quest[100];
DLG成员
class CTESTDlg : public CDialog { // Construction public: CTESTDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CTESTDlg) enum { IDD = IDD_TEST_DIALOG }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTESTDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CTESTDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnStudy(); afx_msg void OnSelchangeAnswers(); afx_msg void OnTest(); afx_msg void OnNextbt(); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: int mark;//考试分值,五个题目,一个20分,满分100 int QS[5];//随机题库序号 int count2;//题库总数 int count_Q;//当前输出的题目数量 };
其中count2用来保存读取的题目的个数,count_Q用来保存当前输出的题目个数,mark为考试当前得分,QS保存每次进入考试的时候随机生成的考试题目序号
DLG的初始化代码
BOOL CTESTDlg::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 //读取题目,放置到题目结构体中 CFile file("test.txt",CFile::shareExclusive); char *pBuf; DWORD len; len = file.GetLength(); pBuf = new char[len+1]; pBuf[len] = '