MFC常用控件

按钮控件

按钮控件包括命令按钮(Button)、单选按钮(Radio Button)和复选框(Check Box)等。

按钮控件会向父窗口发送通知消息,最常用的通知消息莫过于BN_CLICKED和BN_DOUBLECLICKED了。用户在按钮上单击鼠标时会向父窗口发送BN_CLICKED消息,双击鼠标时发送BN_DOUBLECLICKED消息。

命令按钮(Button)

在一个对话框中,Button可以定义一个默认按钮,这只要选中按钮属性中的“Default”选项。如果在对话框活动的时候按下了Enter键,则等同于单击了默认按钮。

void CWindowDlg::OnBnClickedBtnTest1()
{
    MessageBox(L"提示1");
}

void CWindowDlg::OnBnClickedBtnTest2()
{
    MessageBox(L"默认按钮属性设置为true");
}
//测试2按钮的默认属性设置为true,当在对话框中点击enter键时,弹出对话框:
默认按钮属性设置为true

 动态生成一个Button控件的方式如下:

头文件中定义CButton对象

CButton   m_btnTest1;

执行CButton::Create 创建一个CButton对象:

BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
int CWindowDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
m_btnTest1.Create(L"哈哈哈", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(20, 100, 100, 160), this, IDC_BTN_HELLO1);

//pParentWnd指示拥有按钮的父窗口(this为当前对话框对象),不能为NULL;
//nID指定与按钮关联的ID号,用于按钮控件的事件处理( IDC_BTN_HELLO1)
}

stdadx.h中定义IDC_BTN_HELLO1宏

#define IDC_BTN_HELLO1 1000

运行后对话框显示的Button按钮结果如下:

 如果想让按钮有其他动作,比如当鼠标移动到按钮上时,按钮文本信息会变化。实现如下:

1.自定义继承自CButton的子类CMyButton

2.执行CButton::Create 创建一个CButton对象

3.为按钮IDC_BTN_HELLO绑定消息处理函数OnBnClickedBtnHello并更改文本

#pragma once
#include "afxwin.h"
class CMyButton :    public CButton
{
public:
    CMyButton();
    ~CMyButton();
    DECLARE_MESSAGE_MAP()
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};
View Code
//MyButton.cpp
#include "stdafx.h"
#include "MyButton.h"


CMyButton::CMyButton()
{
}


CMyButton::~CMyButton()
{
}

BEGIN_MESSAGE_MAP(CMyButton, CButton)
    ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
    this->SetWindowText(L"11111");

    CButton::OnMouseMove(nFlags, point);
}
View Code
//WindowDlg.h
public:
    CMyButton m_btnTest;   //测试按钮
int CWindowDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;
    //第一种方法
    m_btnTest.Create(L"Hello1", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(20, 100, 100, 160), this, IDC_BTN_HELLO);

    //第二种方法
    //人 = 躯体(m_btnTest) + 灵魂(hWnd)
    //HWND hWnd = ::CreateWindow(L"Button", L"Hello", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 100,100, 60, m_hWnd, (HMENU)IDC_BTN_HELLO, AfxGetInstanceHandle(), NULL);
    //m_btnTest.Attach(hWnd);
}
View Code

 当鼠标移到到动态创建的按钮上时,文本变化。

单选按钮(Radio Button)

单选按钮使用时,一般是多个组成一组,组中每个单选按钮的选中状态具有互斥关系,即同组的单选按钮只能有一个被选中。
单选按钮有选中和未选中两种状态,为选中状态时单选按钮中心会出现一个蓝点,以标识选中状态。

如下定义IDC_RADIO1,IDC_RADIO2,IDC_RADIO3,默认选中IDC_RADIO1

BOOL CWindowDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    ...
    // 
    ((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);//性别男 选中
}

复选框(Check Box)

复选框其实就是把一个编辑框和一个列表框组合到了一起,分为三种: 简易(Simple)组合框、下拉式(Dropdown)组合框和下拉列表式(Drop List)组合框。

以下拉式为例:

这里设置类型跟数据分别如下:

 然后在OnInitDialog方法中通过GetDlgItem获取该复选框指针后对其进行操作如下:

CComboBox* pCombBox = (CComboBox*)GetDlgItem(IDC_COMBO1);
    pCombBox->AddString(L"菲律宾");
    pCombBox->InsertString(1, L"越南");
    pCombBox->DeleteString(2);

    pCombBox->SetCurSel(0);//设置当前第一条选中

 编辑框

MFC为编辑框提供了CEdit类。编辑框的所有操作都封装到了CEdit类中。一般如果我们要防止别人在编辑框中进行输入,可以设置编辑框的属性 Read Only。

例如:默认为编辑框显示:HelloWorld

在OnInitDialog()方法中调用

SetDlgItemText(IDC_EDIT2, L"HelloWorld");

结果如下:

列表框控件(ListBox)

 提供 Windows 列表框功能。

CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
    pListBox->AddString(L"C++");
    pListBox->AddString(L"Python");
    pListBox->AddString(L"Java");
    pListBox->AddString(L"前端");
    pListBox->AddString(L"安卓");
    pListBox->AddString(L"iOS");

列表视图控件(Listcontrol)

1.将列表视图控件IDC_LIST2从工具箱中拖动值设计界面,并选择相关属性:视图为“Report”

 2.获取列表控件,设置整行选取风格,绑定显示数据

//获取列表控件
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST2);
    //整行选取风格
    pListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

    pListCtrl->InsertColumn(0, L"编号", LVCFMT_LEFT,100);
    pListCtrl->InsertColumn(1, L"姓名", LVCFMT_LEFT,200);
    pListCtrl->InsertColumn(2, L"性别", LVCFMT_LEFT,150);

    pListCtrl->InsertItem(0, L"1");
    pListCtrl->SetItemText(0, 1, L"张三");
    pListCtrl->SetItemText(0, 2, L"");

    pListCtrl->InsertItem(1, L"2");
    pListCtrl->SetItemText(1, 1, L"李四");
    pListCtrl->SetItemText(1, 2, L"");
View Code

LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES设置了选中时的样式为全行选中、并具有网格线

结果如下:

 参考:https://blog.csdn.net/cao_jie_xin/article/details/99585010

原文地址:https://www.cnblogs.com/shuzhongke/p/15548976.html