Ver código fonte

1、CCombobox添加水平滚动条:
设置Style &= WS_HSCROLL;
使用SetHorizontalExtent设置水平滚动条的宽度;
2、OnInitialUpdate不会被调用2次,但是出现了其他问题;

scbc.sat2 5 anos atrás
pai
commit
447cf0c9d0

+ 40 - 1
SATHelper/SATHelper/IRControlWnd.cpp

@@ -161,7 +161,7 @@ int CIRControlWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
 	m_wndSignalCheck.m_nFlatStyle = CMFCButton::BUTTONSTYLE_FLAT;
 	m_wndSignalCheck.SizeToContent();
 
-	if (!m_wndSignalCombo.Create(dwViewStyle| WS_VSCROLL, rectDummy, this, ID_COMBOBOX_SIGNAL_FILE))
+	if (!m_wndSignalCombo.Create(dwViewStyle| WS_VSCROLL| WS_HSCROLL, rectDummy, this, ID_COMBOBOX_SIGNAL_FILE))
 	{
 		TRACE0("未能创建信号集下拉框 \n");
 		return -1;      // 未能创建
@@ -632,6 +632,7 @@ void CIRControlWnd::UpdateSignalBtnStatus()
 
 void CIRControlWnd::AutoSetDroppedWidth()
 {
+	return AddHScroll();
 	// Set the height of every item so the item
    // is completely visible.
 	CString str;
@@ -657,3 +658,41 @@ void CIRControlWnd::AutoSetDroppedWidth()
 	}
 	m_wndSignalCombo.ReleaseDC(pDC);
 }
+
+void CIRControlWnd::AddHScroll()
+{
+	// Find the longest string in the combo box.
+	CString str;
+	CSize sz;
+	int dx = 0;
+
+	TEXTMETRIC tm = { 0 };
+	CDC* pDC = m_wndSignalCombo.GetDC();
+	CFont* pFont = m_wndSignalCombo.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 < m_wndSignalCombo.GetCount(); i++)
+	{
+		m_wndSignalCombo.GetLBText(i, str);
+		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);
+	m_wndSignalCombo.ReleaseDC(pDC);
+
+	// Set the horizontal extent so every character of all strings can
+	// be scrolled to.
+	m_wndSignalCombo.SetHorizontalExtent(dx);
+}

+ 2 - 0
SATHelper/SATHelper/IRControlWnd.h

@@ -127,5 +127,7 @@ public:
 	void UpdateSignalBtnStatus();
 	// 自动根据Combobox选项长度设置宽度;
 	void AutoSetDroppedWidth();
+	// CCombobox添加水平滚动条;
+	void AddHScroll();
 };