MFC::通用控件

通用控件

1.01 通过控件ID获得控件窗口指针

void CDemoDlg::OnTest() 
{
	//获得控件窗口指针
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT);

	CString strText = _T("");
	strText.Format(L"pEdit=0x%X\n&m_ctrlEdit=0x%X\n", pEdit, &m_ctrlEdit);
	AfxMessageBox(strText);
}

1.02 通过控件指针获得控件ID

void CDemoDlg::OnTest() 
{
	//获得控件ID
	int nID = m_ctrlEdit.GetDlgCtrlID();

	CString strText = _T("");
	strText.Format(L"nID=%d\nIDC_EDIT=%d", nID, IDC_EDIT);
	AfxMessageBox(strText);
}

1.03获得指定点的控件指针

void CDemoDlg::OnTest()
{
	CRect rect;
	GetClientRect(&rect);

	//获得中指定点控件窗口指针
	CWnd* pWnd = ChildWindowFromPoint(CPoint(rect.Width() / 2, 15), CWP_ALL);

	//获得Edit控件窗口指针
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT);

	if (pWnd != NULL)
	{
 		CString strText = _T("");
		strText.Format(L"pWnd=0x%X\npEdit=0x%X\n", pWnd, pEdit);
		AfxMessageBox(strText);
	}
}

1.04 遍历控件

//获得第一个子窗口
CWnd* pChildWnd = GetTopWindow();
while (pChildWnd != NULL)
{
	//获得控件ID
	int nID = pChildWnd->GetDlgCtrlID();
	CString strText = _T("");
	strText.Format(_T("%d"), nID);
	pChildWnd->SetWindowText(strText);

	//获得下一个子窗口
	pChildWnd = pChildWnd->GetNextWindow();
}

1.05 根据父窗口的大小改变控件的大小和位置

void CDemoDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);

	//根据父窗口的大小移动窗口
	CWnd* pWnd = GetDlgItem(IDC_LIST);
	if (pWnd != NULL)
	{
		pWnd->MoveWindow(10, 10, cx - 20, cy - 20);
	}
}

1.06 显示或隐藏控件

void CDemoDlg::OnTest()
{
	//获得控件窗口指针
	CWnd* pWnd = GetDlgItem(IDC_EDIT);

	//判断控件窗口是否可见
	if (pWnd->IsWindowVisible())
	{
		//隐藏控件窗口
		pWnd->ShowWindow(SW_HIDE);

		SetDlgItemText(IDC_TEST, _T("显示控件"));
	}
	else
	{
		//显示控件窗口
		pWnd->ShowWindow(SW_SHOW);

		SetDlgItemText(IDC_TEST, _T("隐藏控件"));
	}
}

1.07 启用或禁用控件

void CDemoDlg::OnTest()
{
	//获得控件窗口指针
	CWnd* pWnd = GetDlgItem(IDC_EDIT);
	//判断控件窗口是否可用
	if (pWnd->IsWindowEnabled())
	{
		//禁用控件窗口
		pWnd->EnableWindow(FALSE);
		SetDlgItemText(IDC_TEST, _T("启用控件"));
	}
	else
	{
		//启用控件窗口
		pWnd->EnableWindow(TRUE);
		SetDlgItemText(IDC_TEST, _T("禁用控件"));
	}
}

1.08 改变控件的颜色

创建CDemoEdit类继承CEdit类
//设置文本前景色和背景色
HBRUSH CDemoEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	//设置文本前景色和背景色
	pDC->SetTextColor(RGB(0xFF, 0x00, 0x00));
	pDC->SetBkColor(RGB(0x99, 0xCC, 0xFF));
	return m_hBrush;
}

1.09 设置控件的焦点

void CDemoDlg::OnTest()
{
	//获得编辑框窗口
	CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT1);
	CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT2);
	
	//切换编辑框焦点
	if (m_nFocus == 0)
	{
		if (pEdit2->SetFocus())
		{
			m_nFocus = 1;
		}
	}
	else if (m_nFocus == 1)
	{
		if (pEdit1->SetFocus())
		{
			m_nFocus = 0;
		}
	}
}

1.10 控件添加工具提示

CToolTipCtrl m_ToolTip;

// 创建工具提示
m_ToolTip.Create(this);
// 将工具提示与控件对应起来
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT), _T("编辑框"));

BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg) 
{
	//将鼠标消息传递给工具提示
	m_ToolTip.RelayEvent(pMsg);

	return CDialog::PreTranslateMessage(pMsg);
}

1.11 编辑框中追加文本

void CDemoDlg::OnTest()
{
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT);

	int nLength = pEdit->GetWindowTextLength();

	//选定当前文本的末端
	pEdit->SetSel(nLength, nLength);

	//l追加文本
	pEdit->ReplaceSel(_T("ABC2"));
}

1.12 限制编辑框输入文本的长度

//获得编辑框的字体大小
TEXTMETRIC tm;
CDC* pDC = m_ctrlEdit.GetDC();
pDC->GetTextMetrics(&tm);
m_ctrlEdit.ReleaseDC(pDC);

//获得编辑框的格式化矩形
CRect rect;
m_ctrlEdit.GetRect(&rect);

m_ctrlEdit.LimitText(rect.Width() / tm.tmAveCharWidth);

1.13 限制编辑框只能输入数字

创建CDemoEdit类继承CEdit类
void CDemoEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	//判断字符是否为数字
	if ((nChar < '0') || (nChar > '9'))
	{
		if (nChar != VK_BACK)
		{
			AfxMessageBox(_T("请输入数字。"));
			return;
		}
	}
	CEdit::OnChar(nChar, nRepCnt, nFlags);
}

1.14 编辑框中输入的字符转换为大写(小写)

void CDemoEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	//将字符转换为大写字符
	if (!IsCharUpper((TCHAR)nChar))
	{	
		nChar = (UINT)CharUpper((TCHAR*)nChar);
	}

	//将字符转换为小写字符
	// if (!IsCharLower((TCHAR)nChar))
	// {
	// 	nChar = (UINT)CharLower((TCHAR*)nChar);
	// }

	//处理消息
	DefWindowProc(WM_CHAR, nChar, MAKELPARAM(nRepCnt, nFlags));
}

1.15 按钮上显示图标

//添加BS_ICON风格
m_ctrlButton.ModifyStyle(0, BS_ICON);
m_ctrlButton.MoveWindow(10,10,32,32);

//加载图标
HINSTANCE hInstance = AfxFindResourceHandle(MAKEINTRESOURCE(IDI_ICON),RT_GROUP_ICON);
HICON hIcon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
//设置按钮图标
m_ctrlButton.SetIcon(hIcon);
//加载位图
HINSTANCE hInstance = AfxFindResourceHandle(MAKEINTRESOURCE(IDB_BITMAP), RT_GROUP_ICON);
HBITMAP hBitmap = ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP));

//设置按钮位图
m_ctrlButton.SetBitmap(hBitmap);
//加载按钮位图
m_ctrlButton.LoadBitmaps(IDB_UP, IDB_DOWN);

1.16 设置和获得单选按钮的选中状态

//设置单选按钮的选中状态
((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(0);
((CButton*)GetDlgItem(IDC_RADIO2))->SetCheck(1);
((CButton*)GetDlgItem(IDC_RADIO3))->SetCheck(0);
void CDemoDlg::OnTest()
{
	//获得单选按钮的选中状态
	if (((CButton*)GetDlgItem(IDC_RADIO1))->GetCheck())
	{
		AfxMessageBox(_T("单选按钮1选中。"));
	}
	if (((CButton*)GetDlgItem(IDC_RADIO2))->GetCheck())
	{
		AfxMessageBox(_T("单选按钮2选中。"));
	}
	if (((CButton*)GetDlgItem(IDC_RADIO3))->GetCheck())
	{
		AfxMessageBox(_T("单选按钮3选中。"));
	}
}
void CDemoDlg::OnTest() 
{
	//获得组中单选按钮的选中状态
	int nID = GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO3);

	if (nID == IDC_RADIO1)
	{
		AfxMessageBox(_T("单选按钮1选中。"));
	}
	else if (nID == IDC_RADIO2)
	{
		AfxMessageBox(_T("单选按钮2选中。"));
	}
	else if(nID == IDC_RADIO3)
	{
		AfxMessageBox(_T("单选按钮3选中。"));
	}
}

1.17 组合框控件

//向组合框中列表框添加文本
m_ctrlComboBox.AddString(strText);
//设置组合框当前选择项
m_ctrlComboBox.SetCurSel(0);
void CDemoDlg::OnSelchangeCombo() 
{
	CString strText = _T("");
	//获得组合框文本
	m_ctrlComboBox.GetWindowText(strText);
	SetDlgItemText(IDC_EDIT, strText);
}
/* 设置组合框中列表框的宽度 */
CDC* pDC = m_ctrlComboBox.GetDC();
for (int n = 0; n < m_ctrlComboBox.GetCount(); n++)
{
	CString strText = _T("");
	int nTextWidth = 0;

	//获得组合框中列表框文本
	m_ctrlComboBox.GetLBText(n, strText);

	//获得组合框中列表框文本宽度
	nTextWidth = pDC->GetTextExtent(strText).cx;

	nWidth = max(nWidth, nTextWidth);
}
m_ctrlComboBox.ReleaseDC(pDC);
//加滚动条宽度和左右边界宽度
nWidth += ::GetSystemMetrics(SM_CXVSCROLL) + ::GetSystemMetrics(SM_CXEDGE) * 2;
//设置组合框中列表框宽度
m_ctrlComboBox.SetDroppedWidth(nWidth);
//在组合框中显示文件列表
m_ctrlComboBox.Dir(DDL_DRIVES, _T(""));

1.18 在列表框中添加水平滚动条

//向List添加文本
m_ctrlList.AddString(strText);
//发送LB_SETHORIZONTALEXTENT消息
SendDlgItemMessage(IDC_LIST, LB_SETHORIZONTALEXTENT, 400, 0);

1.19 进度条

//设置进度条的范围
m_ctrlProgress.SetRange(0, 100);
//设置进度条的每一步的增量
m_ctrlProgress.SetStep(10);
//设置进度条的当前位置
m_ctrlProgress.SetPos(0);
//用每一步的增量来增加进度条的当前位置
m_ctrlProgress.StepIt();

1.19 滑块

//设置滑块的位置的最大值和最小值
m_ctrlSlider.SetRange(0, 100);
//设置滑块的当前位置
m_ctrlSlider.SetPos(50);
//获得滑块的当前位置
int nPos = m_ctrlSlider.GetPos();

1.19 范围按钮CSpinButtonCtrl

//CSpinButtonCtrl	m_ctrlSpinButton;
//设置旋转按钮的位置范围
m_ctrlSpinButton.SetRange(-100, 100);
//设置旋转按钮的当前位置
m_ctrlSpinButton.SetPos(0);
//设置旋转按钮的当前基数
m_ctrlSpinButton.SetBase(1);
//设置旋转按钮的伙伴窗口
m_ctrlSpinButton.SetBuddy(GetDlgItem(IDC_EDIT));

1.20 列表CListCtrl

void CDemoDlg::InitList()
{

	//设置列表控件的背景图片
	m_ctrlList.SetBkImage(_T("c:\\bg.bmp"));
	//设置文本背景颜色为透明色
	m_ctrlList.SetTextBkColor(CLR_NONE);

	//设置列表控件整行选中
	m_ctrlList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	//设置列表控件显示网格线
	m_ctrlList.SetExtendedStyle(LVS_EX_GRIDLINES);

	// 设置列表控件的报表显示方式
	m_ctrlList.ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT);
	// 在列表控件中插入列
	for (int n = 0; n < 4; n++)
	{
		CString strColumnHeading = _T("");
		strColumnHeading.Format(_T("Column %d"), n);
		m_ctrlList.InsertColumn(n, strColumnHeading, LVCFMT_LEFT, 100);
	}
	CString strText = _T("");
	LVITEM lvItem;
	// 在列表控件中插入行
	for (int m = 0; m < 10; m++)
	{
		strText.Format(_T("Item %d"), m);
		lvItem.mask = LVIF_TEXT;
		lvItem.iItem = m;
		lvItem.iSubItem = 0;
		lvItem.pszText = strText.GetBuffer(strText.GetLength());
		// 插入列表项
		m_ctrlList.InsertItem(&lvItem);
		strText.ReleaseBuffer();

		for (int n = 1; n < 4; n++)
		{
			strText.Format(_T("SubItem %d %d"), m, n);
			lvItem.mask = LVIF_TEXT;
			lvItem.iItem = m;
			lvItem.iSubItem = n;
			lvItem.pszText = strText.GetBuffer(strText.GetLength());
			m_ctrlList.SetItem(&lvItem);
			// 设置子列表项
			strText.ReleaseBuffer();
		}
	}
}
/* 设置列表控件使用复选框 */
void CDemoDlg::InitList()
{
	//设置列表控件的报表显示方式
	m_ctrlList.ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT);

	//设置列表控件使用复选框
	m_ctrlList.SetExtendedStyle(LVS_EX_CHECKBOXES);

	//在列表控件中插入列
	for (int n = 0; n < 4; n++)
	{
		CString strColumnHeading = _T("");
		strColumnHeading.Format(_T("Column %d"), n);
		m_ctrlList.InsertColumn(n, strColumnHeading, LVCFMT_LEFT, 100);
	}

	//在列表控件中插入行
	for (int m = 0; m < 10; m++)
	{
		m_ctrlList.InsertItem(m, _T(""));
		m_ctrlList.SetCheck(m, TRUE);

		for (int n = 1; n < 4; n++)
		{
			CString strText = _T("");
			strText.Format(_T("SubItem %d %d"), m, n);
			m_ctrlList.SetItemText(m, n, strText);
		}
	}
}

void CDemoDlg::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0)
	{
		return;
	}
	//CheckBox旧状态
	BOOL bOldChecked = (BOOL)(((pNMListView->uOldState & 0x3000) >> 12) - 1);
	//CheckBox新状态
	BOOL bNewChecked = (BOOL)(((pNMListView->uNewState & 0x3000) >> 12) - 1);

	if (!bOldChecked && bNewChecked)
	{
		m_ctrlList.SetItemText(pNMListView->iItem, pNMListView->iSubItem, _T("TRUE"));
	}
	else if (bOldChecked && !bNewChecked)
	{
		m_ctrlList.SetItemText(pNMListView->iItem, pNMListView->iSubItem, _T("FALSE"));
	}
}
/* 自动排序表 */
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	//获得列表控件
	CListCtrl* pListCtrl = (CListCtrl*)lParamSort;

	CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
	CString strItem2 = pListCtrl->GetItemText(lParam2, 0);

	return strcmp(strItem2, strItem1);
}

void CDemoDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	//排序表项
	m_ctrlList.SortItems(CompareFunc, LPARAM(&m_ctrlList));
}

1.20 树CTreeCtrl

void CDemoDlg::InitTree()
{
	//在树控件中添加连线和按钮
	m_ctrlTree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);

	//设置树控件的图像列表
	m_ctrlTree.SetImageList(&m_ImageList, TVSIL_NORMAL);

	CString strText = _T("");
	TVINSERTSTRUCT tvInsert;
	tvInsert.hParent = TVI_ROOT;
	tvInsert.hInsertAfter = NULL;
	tvInsert.item.mask = TVIF_TEXT;
	tvInsert.item.pszText = _T("Root");

	//在树控件中插入项
	HTREEITEM hRoot = m_ctrlTree.InsertItem(&tvInsert);
	for (int i = 0; i < 4; i++)
	{
		strText.Format(_T("Item %d"), i);
		tvInsert.hParent = hRoot;
		tvInsert.hInsertAfter = TVI_LAST;
		tvInsert.item.mask = TVIF_TEXT;
		tvInsert.item.pszText = strText.GetBuffer(strText.GetLength());

		//在树控件中插入项
		HTREEITEM hParent = m_ctrlTree.InsertItem(&tvInsert);
		strText.ReleaseBuffer();
		for(int j = 0; j < 5; j++)
		{
			strText.Format(_T("SubItem %d %d"), i, j);
			tvInsert.hParent = hParent;
			tvInsert.hInsertAfter = TVI_LAST;
			tvInsert.item.mask = TVIF_TEXT;
			tvInsert.item.pszText = strText.GetBuffer(strText.GetLength());
			//在树控件中插入项
			HTREEITEM hParent = m_ctrlTree.InsertItem(&tvInsert);
			strText.ReleaseBuffer();
		}
		//展开树项
		m_ctrlTree.Expand(hParent, TVE_EXPAND);
	}
}
void CDemoDlg::OnTest1()
{
	//获得树控件的当前选择项;
	HTREEITEM hSel = m_ctrlTree.GetSelectedItem();

	if( hSel == NULL)
	{
		return;
	}

	//判断该项是否有子项
	if (m_ctrlTree.ItemHasChildren(hSel))
	{
		CString strMessage = _T("当前选择项包含子项,是否删除?");
		if (MessageBox(strMessage, _T(""), MB_YESNO) == IDNO)
		{
			return;
		}
	}

	//从树控件中删除该项
	m_ctrlTree.DeleteItem(hSel);
}
/* 遍历下级子项 */
void CDemoDlg::Search(HTREEITEM hParent, int nOffset)
{
	CString strText = _T("");

	HTREEITEM hChild = NULL;

	//获得该项的子项
	hChild = m_ctrlTree.GetChildItem(hParent);

	while (hChild != NULL)
	{
		//获得子项的文本
		strText = m_ctrlTree.GetItemText(hChild);

		//根据偏移量在字符串中插入空格
		for (int n = 0; n < nOffset; n++)
		{
			strText.Insert(0, _T("   "));
		}

		//在列表框中添加文本
		m_ctrlList.AddString(strText);

		//子项是否有子项
		if (m_ctrlTree.ItemHasChildren(hChild))
		{
			//遍历下级子项
			Search(hChild, nOffset + 1);
		}

		//获得子项的兄弟项
		hChild = m_ctrlTree.GetNextItem(hChild, TVGN_NEXT);
	}
}

1.21 CTabCtrl

void CDemoDlg::InitTab()
{
	//向标签控件中插入项
	for (int n = 0 ; n < 5; n++)
	{
		CString strText = _T("");
		strText.Format(_T("Tab %d"), n + 1);

		m_ctrlTab.InsertItem(n, strText);
	}
}

1.22 时间控件CDateTimeCtrl

//选择短日期显示格式
	m_ctrlDateTime1.ModifyStyle(0, DTS_SHORTDATEFORMAT, 0);
	//选择时间显示格式
	m_ctrlDateTime2.ModifyStyle(0, DTS_TIMEFORMAT, 0);

	//获得当前时间
	CTime time = CTime::GetCurrentTime();

	//设置时间
	m_ctrlDateTime1.SetTime(&time);
	m_ctrlDateTime2.SetTime(&time);
//获得日期和时间
m_ctrlDateTime1.GetTime(time1);
m_ctrlDateTime2.GetTime(time2);

CString strText = _T("");
strText.Format(_T("%04d-%02d-%02d\n%02d:%02d:%02d"),
	time1.GetYear(), time1.GetMonth(), time1.GetDay(),
	time2.GetHour(), time2.GetMinute(), time2.GetSecond());

1.22 富文本CRichEditCtrl

CRichEditCtrl* pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT);
pRichEdit->SetWindowText(strText);
posted @ 2018-08-15 17:34  osbreak  阅读(3777)  评论(0编辑  收藏  举报