一、动态生成控件
1.CButton
Create(_T("查询"), WS_CHILD|BS_PUSHBUTTON, rect, this, 1);
2.CComboBox
Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWNLIST, rect, this, 1);
3.CTreeCtrl
Create(WS_VISIBLE | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | WS_TABSTOP | TVS_INFOTIP | TVS_SHOWSELALWAYS, rect, this, 1);
4.CListCtrl
Create(WS_CHILD|WS_BORDER|LVS_REPORT|LVS_SHOWSELALWAYS|LVS_SINGLESEL, rect, this, 1);
SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_TRACKSELECT);
5.CTabCtrl
Create(TCS_TABS | TCS_FIXEDWIDTH | WS_CHILD | TCS_HOTTRACK | TCS_SINGLELINE | TCS_RAGGEDRIGHT | WS_EX_NOPARENTNOTIFY, rect, this, 1);
6.CEdit
Create(WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, rect, this, 1);
二、字体设置
1.创建对象(对象必须:在.h文件中,或者声明为全局变量)
CFont m_font;
2.创建字体
两种方式:
(1)m_font.CreateFont(
12, // 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, // nPitchAndFamily
"宋体");
(2)m_font.CreatePointFont(87, "宋体_GB2312");
3.设置控件字体
控件对象->SetFont(&m_font);
三、常见设置
1.CComboBox下拉List的宽度,使其根据列表中的字体长度变化。
void CMyTabCtrl::SetComboBoxDropDownWidth(CMyComboBox* pComboBox)
{
int dx = 0;
TEXTMETRIC tm;
CDC* pDC = pComboBox->GetDC();
CFont* pFont = pComboBox->GetFont();// Select the listbox font, save the old font
CFont* pOldFont = pDC->SelectObject(pFont);
// Get the text metrics for avg char width
pDC->GetTextMetrics(&tm);for (int i = 0; i < pComboBox->GetCount(); ++i)
{
CString str;
pComboBox->GetLBText(i, str);
CSize sz = pDC->GetTextExtent(str);// Add the avg width to prevent clipping
sz.cx += tm.tmAveCharWidth;if (sz.cx > dx)
dx = sz.cx;
}
// Select the old font back into the DC
pDC->SelectObject(pOldFont);
pComboBox->ReleaseDC(pDC);// Adjust the width for the vertical scroll bar and the left and right border.
dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE);// Set the width of the list box so that every item is completely visible.
pComboBox->SetDroppedWidth(dx);
}
2.控件位置
一般控件:SetWindowPos(NULL, x, y, cx, cy, 0);
CComboBox控件:SetWindowPos(NULL, x, y, cx, 80, 0); // 这里的80是下拉框的高度