VC++之MFC教程

VC++之MFC教程

初步使用MFC

(1)File-New新建对话框程序

(2)设置控件类型和属性,LIST属性设置为报告(Report):
这里写图片描述
这里写图片描述
这里写图片描述
(3)在对话框文件First32Dlg.cpp中修改初始化程序:

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

    // Add "About..." menu item to system menu.

    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList->InsertColumn(0,"学号",0,100);
    pList->InsertColumn(1,"姓名",0,100);
    pList->InsertColumn(2,"工资",0,100);
    //剩余部分不变
}

(4)分别双击三个按钮,添加关联函数:

void CFirst32Dlg::OnAdd() 
{
    // TODO: Add your control notification handler code here
    CString szNumb,szName,szSala;
    GetDlgItemText(IDC_NUMB,szNumb);
    GetDlgItemText(IDC_NAME,szName);
    GetDlgItemText(IDC_SALA,szSala);
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    int nCount = pList->GetItemCount();
    pList->InsertItem(nCount,szNumb);
    pList->SetItemText(nCount,1,szName);
    pList->SetItemText(nCount,2,szSala);
}

void CFirst32Dlg::OnDel() 
{
    // TODO: Add your control notification handler code here
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    int nSel=pList->GetSelectionMark();
    if(nSel<0)
    {
        AfxMessageBox("请选择列表中的员工号码在删除!");
        return;
    }
    pList->DeleteItem(nSel);

}

void CFirst32Dlg::OnMod() 
{
    // TODO: Add your control notification handler code here
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    int nSel = pList->GetSelectionMark();
    if(nSel<0)
    {
        AfxMessageBox("请选择列表中的员工号码再修改!");
        return;
    }
    CString szNumb,szName,szSala;
    GetDlgItemText(IDC_NUMB,szNumb);
    GetDlgItemText(IDC_NAME,szName);
    GetDlgItemText(IDC_SALA,szSala);
    pList->SetItemText(nSel,0,szNumb);
    pList->SetItemText(nSel,1,szName);
    pList->SetItemText(nSel,2,szSala);

}

(5)编译运行。

简单计算器

(1)新建对话框,拖入三个编辑框,一个组合框,一个静态文本,一个按钮
这里写图片描述
(2)修改对话框控件属性:
这里写图片描述
(3)在下拉列表属性中设置为Drop List,去掉Sort属性,添加五个操作符:
这里写图片描述
这里写图片描述
(4)打开类向导,分别对控件添加成员变量:
这里写图片描述
(5)双击“计算”按钮,添加响应函数:

void CCalcDlg::OnOK() 
{
    // TODO: Add extra validation here
    UpdateData();
    switch(m_nOper)
    {
    case 0:
        m_nResult = m_nLeft+m_nRight;
        break;
    case 1:
        m_nResult = m_nLeft-m_nRight;
        break;
    case 2:
        m_nResult = m_nLeft*m_nRight;
        break;
    case 3:
        m_nResult = m_nLeft/m_nRight;
        break;
    case 4:
        m_nResult = m_nLeft%m_nRight;
        break;
    }
    UpdateData(FALSE);
    //CDialog::OnOK();
}

常用控件的数值型变量

(1)新建对话框程序,添加控件:
这里写图片描述
(2)修改控件属性:
这里写图片描述
这里写图片描述
(3)控件添加成员变量:
这里写图片描述
(4)修改构造函数三部分:

CVdDlg::CVdDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CVdDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CVdDlg)
    m_nEduca = 1;//默认为-1,改为1===================================
    m_bEngl = TRUE;//默认为FALSE,改为TRUE
    m_bJapa = FALSE;
    m_dtJoin = COleDateTime::GetCurrentTime();
    m_bKorea = FALSE;
    m_szName = _T("");
    m_szNumb = _T("");
    m_bRuss = FALSE;
    m_nSex = 0;//默认为-1改为0========================================
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

(5)初始化函数增加代码:

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

    // Add "About..." menu item to system menu.

    m_list.InsertColumn(0,"工号",0,80);
    m_list.InsertColumn(1,"姓名",0,80);
    m_list.InsertColumn(2,"性别",0,60);
    m_list.InsertColumn(3,"学历",0,80);
    m_list.InsertColumn(4,"入职",0,80);
    m_list.InsertColumn(5,"外语",0,100);
    m_list.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

    // IDM_ABOUTBOX must be in the system command range.
    //***********************************************
}

(6)添加按钮消息响应函数:

void CVdDlg::OnAdd() 
{
    // TODO: Add your control notification handler code here
    UpdateData();
    int nCount = m_list.GetItemCount();
    m_list.InsertItem(nCount,m_szNumb);
    m_list.InsertItem(nCount,m_szName);
    m_list.SetItemText(nCount,2,m_nSex?"女":"男");
    char *p[]={"高中","大学","硕士","博士"};
    m_list.SetItemText(nCount,3,p[m_nEduca]);
    CString str;
    if(m_bEngl)
        str+="英";
    if(m_bJapa)
        str+="日";
    if(m_bRuss)
        str+="俄";
    if(str.IsEmpty())
        str+="无";
    m_list.SetItemText(nCount,5,str);
    str.Format("%d-%d-%d",m_dtJoin.GetYear(),m_dtJoin.GetMonth(),m_dtJoin.GetDay());
    m_list.SetItemText(nCount,4,str);

}

void CVdDlg::OnDel() 
{
    // TODO: Add your control notification handler code here
    POSITION pos = m_list.GetFirstSelectedItemPosition();
    if(!pos)
    {
        AfxMessageBox("请选择一行再删除!");
        return;
    }
    int nSel=m_list.GetNextSelectedItem(pos);
    CString str=m_list.GetItemText(nSel,0);
    str="确认删除" + str + "号信息吗?";
    if(AfxMessageBox(str,MB_YESNO)==IDYES)
        m_list.DeleteItem(nSel);

}

void CVdDlg::OnMod() 
{
    // TODO: Add your control notification handler code here
    POSITION pos = m_list.GetFirstSelectedItemPosition();
    if(!pos)
    {
        AfxMessageBox("请选择一行再删除!");
        return;
    }
    int nSel = m_list.GetItemCount();
    m_list.SetItemText(nSel,0,m_szNumb);
    m_list.SetItemText(nSel,1,m_szName);
    m_list.SetItemText(nSel,2,m_nSex?"女":"男");
    char *p[]={"高中","大学","硕士","博士"};
    m_list.SetItemText(nSel,3,p[m_nEduca]);
    CString str;
    if(m_bEngl)
        str+="英";
    if(m_bJapa)
        str+="日";
    if(m_bKorea)
        str+="韩";
    if(m_bRuss)
        str+="俄";
    if(str.IsEmpty())
        str+="无";
    m_list.SetItemText(nSel,5,str);
    str.Format("%d-%d-%d",m_dtJoin.GetYear(),m_dtJoin.GetMonth(),m_dtJoin.GetDay());
    m_list.SetItemText(nSel,4,str);
}

画矩形

void CCpenDlg::OnPaint() 
{
    CPaintDC dc(this);
    //dc.MoveTo(20,20);
    //dc.LineTo(120,20);
    CPen pn1(PS_SOLID,1,RGB(255,0,0));
    //CPen* pOldPen=dc.SelectObject(&pn1);
    //dc.MoveTo(20,30);
    //dc.LineTo(120,30);
    dc.SelectObject(pn1);
    dc.Rectangle(140,20,240,100);
}
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页