声明:

// studentDlg.h : header file
//

#if !defined(AFX_STUDENTDLG_H__33826904_70BF_4B3E_A689_7CB14E89B259__INCLUDED_)
#define AFX_STUDENTDLG_H__33826904_70BF_4B3E_A689_7CB14E89B259__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct REC
{
    int no;
    CString name;
    int num1;
    int num2;
    int num3;
    int sum;
    int avg;
    int order;
};


/////////////////////////////////////////////////////////////////////////////
// CStudentDlg dialog

class CStudentDlg : public CDialog
{
// Construction
public:
    CStudentDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data
    //{{AFX_DATA(CStudentDlg)
    enum { IDD = IDD_STUDENT_DIALOG };
    CListCtrl    m_list;
    int        m_ino;
    CString    m_csname;
    int        m_imath;
    int        m_iEnglish;
    int        m_ipol;
    //}}AFX_DATA
    
    struct REC m_rec[100];  //结构数组,用于存放学生成绩
    int m_position;  //输入学生个数
    


    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CStudentDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    //{{AFX_MSG(CStudentDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnButtonSave();
    afx_msg void OnButtonSum();
    afx_msg void OnButtonOrder();
    afx_msg void OnButtonExit();
    afx_msg void OnButtonAvg();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STUDENTDLG_H__33826904_70BF_4B3E_A689_7CB14E89B259__INCLUDED_)

定义:

// studentDlg.cpp : implementation file
//

#include "stdafx.h"
#include "student.h"
#include "studentDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CStudentDlg dialog

CStudentDlg::CStudentDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CStudentDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CStudentDlg)
    m_ino = 0;
    m_csname = _T("");
    m_imath = 0;
    m_iEnglish = 0;
    m_ipol = 0;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_position=0;
}

void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CStudentDlg)
    DDX_Control(pDX, IDC_LIST1, m_list);
    DDX_Text(pDX, IDC_EDIT1, m_ino);
    DDX_Text(pDX, IDC_EDIT2, m_csname);
    DDX_Text(pDX, IDC_EDIT3, m_imath);
    DDX_Text(pDX, IDC_EDIT4, m_iEnglish);
    DDX_Text(pDX, IDC_EDIT5, m_ipol);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
    //{{AFX_MSG_MAP(CStudentDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SUM, OnButtonSum)
    ON_BN_CLICKED(IDC_BUTTON_ORDER, OnButtonOrder)
    ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
    ON_BN_CLICKED(IDC_BUTTON_AVG, OnButtonAvg)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStudentDlg message handlers

BOOL CStudentDlg::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
    m_list.InsertColumn(0,"学号",LVCFMT_CENTER,50);
    m_list.InsertColumn(1,"姓名",LVCFMT_LEFT,75);
    m_list.InsertColumn(2,"数学",LVCFMT_LEFT,50);
    m_list.InsertColumn(3,"英语",LVCFMT_LEFT,50);
    m_list.InsertColumn(4,"政治",LVCFMT_LEFT,50);
    m_list.InsertColumn(5,"总分",LVCFMT_LEFT,50);
    m_list.InsertColumn(6,"平均分",LVCFMT_LEFT,50);
    m_list.InsertColumn(7,"名次",LVCFMT_LEFT,50);
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CStudentDlg::OnButtonSave() 
{    
    /*UpdataData()就是把控件显示的数据传输给成员变量
UpdataData(FALSE)就是把成员变量的数据传输给空间,并在控件显示*/
    if(!UpdateData(TRUE))
        return;
    //将变量数值赋给结构数组
    m_rec[m_position].no = m_ino;
    m_rec[m_position].name = m_csname;
    m_rec[m_position].num1 = m_imath;
    m_rec[m_position].num2 = m_iEnglish;
    m_rec[m_position].num3 = m_ipol;
    m_rec[m_position].sum = 0;
    m_rec[m_position].avg = 0;
    m_rec[m_position].order = 0;
    //写入列表
    CString tmp_str;
    tmp_str.Format("%d",m_ino);
    m_list.InsertItem(m_position,tmp_str);
    m_list.SetItemText(m_position,1,m_csname);
    tmp_str.Format("%d",m_imath);
    m_list.SetItemText(m_position,2,tmp_str);
    tmp_str.Format("%d",m_iEnglish);
    m_list.SetItemText(m_position,3,tmp_str);
    tmp_str.Format("%d",m_ipol);
    m_list.SetItemText(m_position,4,tmp_str);
    m_position++;
    m_ino++;
    UpdateData(FALSE);  //
    GetDlgItem(IDC_EDIT1)->SetFocus();
    ((CEdit *)GetDlgItem(IDC_EDIT1))->SetSel(0,-1);
        
}

void CStudentDlg::OnButtonSum() 
{
    CString tmp_str;
    for(int i=0;i<m_position;i++)
    {
        m_rec[i].sum = m_rec[i].num1 + m_rec[i].num2 + m_rec[i].num3;
        tmp_str.Format("%d",m_rec[i].sum);
        m_list.SetItemText(i,5,tmp_str);
    }
    
}

void CStudentDlg::OnButtonOrder() 
{
    int i,j;
    struct REC tmp_rec;
    for(i=0;i<m_position-1;i++)
    {
        for(j=i+1;j<m_position;j++)
        if(m_rec[i].sum<m_rec[j].sum)
        {
            tmp_rec = m_rec[j];
            m_rec[j] = m_rec[i];
            m_rec[i] = tmp_rec;
        }
    }
    //刷新屏幕
    CString tmp_str;
    m_list.DeleteAllItems();
    for(i=0;i<m_position;i++)
    {
        //写入列表
        tmp_str.Format("%d",m_rec[i].no);
        m_list.InsertItem(i,tmp_str);
        m_list.SetItemText(i,1,m_rec[i].name);
        tmp_str.Format("%d",m_rec[i].num1);
        m_list.SetItemText(i,2,tmp_str);
        tmp_str.Format("%d",m_rec[i].num2);
        m_list.SetItemText(i,3,tmp_str);
        tmp_str.Format("%d",m_rec[i].num3);
        m_list.SetItemText(i,4,tmp_str);
        tmp_str.Format("%d",m_rec[i].sum);
        m_list.SetItemText(i,5,tmp_str);
        tmp_str.Format("%d",m_rec[i].avg);
        m_list.SetItemText(i,6,tmp_str);
        m_rec[i].order=i+1;
        tmp_str.Format("%d",m_rec[i].order);
        m_list.SetItemText(i,7,tmp_str);
    }

}

void CStudentDlg::OnButtonExit() 
{
    CDialog::OnCancel();
    
}

void CStudentDlg::OnButtonAvg() 
{
    CString tmp_str;
    for(int i=0;i<m_position;i++)
    {
        m_rec[i].avg = m_rec[i].sum/3;
        tmp_str.Format("%d",m_rec[i].avg);
        m_list.SetItemText(i,6,tmp_str);
    }
    
}

我感觉只有这样,初学者才能更明白的怎么去用这个控件。

本文非自己写的,是从CSDN上下的别人的源码,然后自己给贴上去的。

 

posted on 2012-07-18 10:45  瓦楞球  阅读(2977)  评论(0编辑  收藏  举报