MFC-自绘控件(CComboBox篇)

MFC-自绘控件(CComboBox篇)

  • MFC(CComboBox)简单自绘
    • 注意事项
    • 代码

MFC(CComboBox)简单自绘

使用CComboBox实现自绘下拉框,内容包含文字和图形。

注意事项

对话框控件属性Behavior/Owner Draw 设为Variable

代码

// 自绘
void DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	CDC dc;
	dc.Attach(lpDIS->hDC);
	CRect rect(lpDIS->rcItem);
	int nItem = lpDIS->itemID;
	COLORREF cLine = GetSysColor(COLOR_WINDOWTEXT);
	COLORREF cBack = GetSysColor(COLOR_WINDOW);
	if (lpDIS->itemState & ODS_SELECTED)
	{
		cLine = GetSysColor(COLOR_HIGHLIGHTTEXT);
		cBack = GetSysColor(COLOR_HIGHLIGHT);
	}
	dc.FillSolidRect(rect, cBack);
	CPen pen(PS_SOLID, 1, cLine);
	CPen* pOldPen = dc.SelectObject(&pen);
	CPen pendot(PS_DOT, 1, cLine);
	CRect rectx;
	CRect recty;
	CFont font;	
	font.CreateFont(
		15, // nHeight
		0, // nWidth
		0, // nEscapement
		0, // nOrientation
		FW_BOLD, // nWeight
		FALSE, // bItalic
		FALSE, // bUnderline
		0, // cStrikeOut
		ANSI_CHARSET, // nCharSet
		OUT_DEFAULT_PRECIS, // nOutPrecision
		CLIP_DEFAULT_PRECIS, // nClipPrecision
		DEFAULT_QUALITY, // nQuality
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial") // nPitchAndFamily Arial
		);
	CFont* def_font;
	if (nItem == 0)
	{
		dc.DrawText(_T("无"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5 * 4);
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() / 3, rect.top, rect.right - rect.Width() / 3, rect.top + rect.Height() * 2 / 5);
		dc.DrawText(_T("x"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		rectx.SetRect(rect.left, rect.top + rect.Height() / 5, rect.left + rect.Width() * 4 / 3, rect.top + rect.Height() / 5 * 4);
		dc.DrawText(_T("y"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
	}
	if (nItem == 1)
	{
		dc.DrawText(TEXT("线切角"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() *0.4, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 0.65);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5 * 4);
		dc.SelectObject(&pendot);
		dc.MoveTo(rect.left + rect.Width() *0.4, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 0.65);
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() *0.4, rect.top, rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 2 / 5);
		dc.DrawText(_T("x"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		rectx.SetRect(rect.left, rect.top + rect.Height() / 5, rect.left + rect.Width() * 4 / 3, rect.top + rect.Height() * 0.65);
		dc.DrawText(_T("y"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
		dc.SelectObject(&pen);
	}
	if (nItem == 2)
	{
		dc.DrawText(TEXT("外圆角"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2 - rect.Height()*0.4, rect.top + rect.Height() / 5);
		dc.Arc(rect.left + rect.Width() / 3 * 2 - rect.Height()*0.8, rect.top + rect.Height() / 5,
			rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 1,
			rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 0.6,
			rect.left + rect.Width() / 3 * 2 - rect.Height()*0.4, rect.top + rect.Height() / 5);
		dc.MoveTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 0.6);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5 * 4);
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() / 3 * 2 - rect.Height()*0.2, rect.top + rect.Height() / 5,
			rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 0.6);
		dc.DrawText(_T("r"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
	}
	if (nItem == 3)
	{
		dc.DrawText(TEXT("内圆角"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2 - rect.Height()*0.4,
			rect.top + rect.Height() / 5);
		dc.Arc(rect.left + rect.Width() / 3 * 2 - rect.Height()*0.4, rect.top - rect.Height() / 5,
			rect.left + rect.Width() / 3 * 2 + rect.Height()*0.4, rect.top + rect.Height() * 0.6,
			rect.left + rect.Width() / 3 * 2 - rect.Height()*0.4, rect.top + rect.Height() / 5 + 1,
			rect.left + rect.Width() / 3 * 2 + 1, rect.top + rect.Height() * 0.6 - 1);
		dc.MoveTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() * 0.6);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5 * 4);
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() / 3 * 2 - rect.Height()*0.3, rect.top + rect.Height() * 0.3,
			rect.left + rect.Width() / 3 * 2 - rect.Height()*0.1, rect.top + rect.Height() * 0.7);
		dc.DrawText(_T("r"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
	}
	if (nItem == 4)
	{
		dc.DrawText(TEXT("弧点"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 2 - 0.3*rect.Height(), rect.bottom - rect.Height() *0.2);
		dc.LineTo(rect.left + rect.Width() / 2 - 0.3*rect.Height(), rect.bottom - rect.Height() *0.6);
		dc.Arc(rect.left + rect.Width() / 2 - 0.424264 * rect.Height(), rect.bottom - rect.Height() *0.724264,
			rect.left + rect.Width() / 2 + 0.424264*rect.Height(), rect.bottom + rect.Height() *0.224264,
			rect.left + rect.Width() / 2 + 0.3*rect.Height(), rect.bottom - rect.Height() *0.6 + 1,
			rect.left + rect.Width() / 2 - 0.3*rect.Height(), rect.bottom - rect.Height() *0.6 + 1);
		dc.MoveTo(rect.left + rect.Width() / 2 + 0.3*rect.Height(), rect.bottom - rect.Height() *0.2);
		dc.LineTo(rect.left + rect.Width() / 2 + 0.3*rect.Height(), rect.bottom - rect.Height() *0.6);
	}
	if (nItem == 5)
	{
		dc.DrawText(TEXT("垂直"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() * 4 / 9, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() * 4 / 9, rect.top + rect.Height() * 3 / 5);
		dc.LineTo(rect.left + rect.Width() * 11 / 18, rect.top + rect.Height() * 2 / 5);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() / 5 * 4);
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() * 3 / 9, rect.top + rect.Height() / 5,
			rect.left + rect.Width() * 5 / 9, rect.top + rect.Height() * 3 / 5);
		dc.DrawText(_T("x"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		rectx.SetRect(rect.left + rect.Width() * 4 / 9, rect.top + rect.Height() * 4 / 5,
			rect.left + rect.Width() * 11 / 18, rect.top + rect.Height() * 1 / 5);
		dc.DrawText(_T("y"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
	}
	if (nItem == 6)
	{
		dc.DrawText(TEXT("平行"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() / 5);
		dc.LineTo(rect.left + rect.Width() * 7 / 18-1, rect.top + rect.Height() *0.2);
		dc.LineTo(rect.left + rect.Width() * 9 / 18, rect.top + rect.Height() *0.6);
		dc.LineTo(rect.left + rect.Width() * 11 / 18, rect.top + rect.Height() *0.6);		
		dc.LineTo(rect.left + rect.Width() *12 / 18, rect.top + rect.Height()*0.8);
		dc.SelectObject(&pendot);
		dc.MoveTo(rect.left + rect.Width() * 7 / 18, rect.top + rect.Height() *0.2);
		dc.LineTo(rect.left + rect.Width() * 9 / 18, rect.top + rect.Height() *0.2);
		dc.LineTo(rect.left + rect.Width() * 11 / 18, rect.top + rect.Height() *0.6);		
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() * 7 / 18, rect.top + rect.Height() *0,
			rect.left + rect.Width() * 9 / 18, rect.top + rect.Height() *0.4);
		dc.DrawText(_T("x"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		rectx.SetRect(rect.left + rect.Width() * 9 / 18, rect.top + rect.Height() *0.2,
			rect.left + rect.Width() * 11 / 18, rect.top + rect.Height() *0.6);
		dc.DrawText(_T("y"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
		dc.SelectObject(&pen);
	}
	if (nItem == 7)
	{
		dc.DrawText(TEXT("Line圆"), rect, DT_VCENTER | DT_SINGLELINE);
		dc.MoveTo(rect.left + rect.Width() / 3, rect.top + rect.Height() *0.3);
		dc.LineTo(rect.left + rect.Width() / 3 + rect.Height()*0.4, rect.top + rect.Height() *0.3);
		dc.Arc(rect.left + rect.Width() / 3 + rect.Height()*0.4, rect.top + rect.Height() * 0,
			rect.left + rect.Width() / 3 + rect.Height() * 1, rect.top + rect.Height() *0.6,
			rect.left + rect.Width() / 3 + rect.Height()*0.4, rect.top + rect.Height() *0.3 + 1,
			rect.left + rect.Width() / 3 + rect.Height()*0.8, rect.top + rect.Height() *0.6 + 2);
		dc.MoveTo(rect.left + rect.Width() / 3 + rect.Height()*0.8 - 1, rect.top + rect.Height() *0.6);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() *0.6);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() *0.8);
		dc.SelectObject(&pendot);
		dc.MoveTo(rect.left + rect.Width() / 3 + rect.Height()*0.4, rect.top + rect.Height() *0.3);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() *0.3);
		dc.LineTo(rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() *0.6);
		dc.SetTextColor(RGB(255, 0, 0));
		def_font = dc.SelectObject(&font);
		dc.SetBkMode(TRANSPARENT);
		rectx.SetRect(rect.left + rect.Width() / 3 + rect.Height()*0.4, rect.top + rect.Height() * 0,
			rect.left + rect.Width() / 3 * 2, rect.top + rect.Height() *0.3);
		dc.DrawText(_T("x"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		rectx.SetRect(rect.left + rect.Width() / 3 * 2 + rect.Height()*0.1, rect.top + rect.Height() *0,
			rect.left + rect.Width() / 3 * 2 + rect.Height()*0.3, rect.top + rect.Height() *0.9);
		dc.DrawText(_T("y"), rectx, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// Done with the font. Delete the font object.
		dc.SelectObject(def_font);
		font.DeleteObject();
		dc.SetTextColor(RGB(0, 0, 0));
		dc.SelectObject(&pen);
	}
	dc.Detach();
	if (!GetSafeHwnd())	return;
	CClientDC curDC(this);
	CRect rectClient;
	GetClientRect(rectClient);
}
// 调整控件高度
void MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
	lpMIS->itemHeight = 40;
	SendMessage(CB_SETITEMHEIGHT, -1, 40);
}

你可能感兴趣的:(MFC,mfc)