void CColorCombo::InitDialog(HWND hWndCtrl, bool bDefaultColors, bool bAllowOffOption) { if (!hWndCtrl) return; SubclassWindow(hWndCtrl); AddComboToList(); long lWidth = GetDroppedWidth(); long lRet = SetDroppedWidth(dtoi(2.75 * lWidth)); // Stuff the combobox with colors // Add a place holder for the Custom option (the color picker) int nItem = AddString(""); SetItemData(nItem, CLR_NONE); if (bAllowOffOption) { nItem = AddString(""); SetItemData(nItem, CLR_DEFAULT); } if (bDefaultColors) { for (int i = 0; i < NDEFAULTCOLORS; i++) { nItem = AddString(""); SetItemData(nItem, CColorCombo::m_DefaultColors[i].Color); } } }
int COXMultiComboBox::AdjustToFitSize() { int nResult; if(m_bFitToSize) { int nTotalWidth=GetTotalWidth()+2*GetSystemMetrics(SM_CXBORDER); int nTotalHeight=GetTotalHeight(); int nBorderHeight=GetSystemMetrics(SM_CYBORDER); int nScrollbarHeight=GetSystemMetrics(SM_CYHSCROLL); CRect rcDrop; GetDroppedControlRect(&rcDrop); if((nTotalHeight > rcDrop.Height()-2*nBorderHeight) || ((nTotalWidth > rcDrop.Width()) && (nTotalHeight > rcDrop.Height()-nScrollbarHeight-2*nBorderHeight))) { nTotalWidth+=::GetSystemMetrics(SM_CXVSCROLL); } int nScreenWidth=::GetSystemMetrics(SM_CXSCREEN); nTotalWidth=nTotalWidth>nScreenWidth ? nScreenWidth : nTotalWidth; nResult=SetDroppedWidth(nTotalWidth); } else nResult=SetDroppedWidth(1); if(nResult!=CB_ERR) { // ... To indicate that the scrollbars has to be re-adjusted m_fSizeChanged = TRUE; // Redraws the listbox if(::IsWindow(m_ComboLBox.m_hWnd)) { m_ComboLBox.RedrawWindow(NULL,NULL, RDW_ERASE|RDW_INVALIDATE|RDW_ERASENOW|RDW_UPDATENOW|RDW_FRAME); } } return nResult; }
//------------------------------------------------------------------------ //! CBN_DROPDOWN message handler called when the CComboBox control //! is expanded into a dropdown list. Used to restrict the width of //! the dropdown list to the max width. //------------------------------------------------------------------------ void CGridEditorComboBox::OnDropDown() { int itemHeight = GetItemHeight(-1); int nNumEntries = GetCount(); // Resize combobox according to actual element count int visibleItemCount = m_MaxHeightItems < nNumEntries ? m_MaxHeightItems : nNumEntries; // min(m_MaxHeightItems, nNumEntries); CRect rectExpanded; GetClientRect(rectExpanded); rectExpanded.bottom += visibleItemCount * GetItemHeight(0); rectExpanded.bottom += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges SetWindowPos(NULL, // not relative to any other windows 0, 0, // TopLeft corner doesn't change rectExpanded.Width(), rectExpanded.Height(), // existing width, new height SWP_NOMOVE | SWP_NOZORDER // don't move box or change z-ordering. ); // Resize combo-box width to fit contents int nMaxItemWidth = 0; CString str; // Find max-width of the elements CDC* pDC = GetDC(); CFont* pFont = GetFont(); CFont* pOldFont = pDC->SelectObject(pFont); for (int i = 0; i < nNumEntries; i++) { GetLBText(i, str); int nLength = pDC->GetTextExtent(str).cx; nMaxItemWidth = nMaxItemWidth > nLength ? nMaxItemWidth : nLength; // max(nMaxItemWidth, nLength); if (nMaxItemWidth > m_MaxWidthPixels) { nMaxItemWidth = m_MaxWidthPixels; break; } } // Check if there are so many elements that we need space for a vertical scrollbar CRect rect; GetDroppedControlRect(&rect); if (rect.Height() <= nNumEntries*GetItemHeight(0)) nMaxItemWidth +=::GetSystemMetrics(SM_CXVSCROLL); // Add margin space to the calculations nMaxItemWidth += pDC->GetTextExtent(_T("0")).cx; pDC->SelectObject(pOldFont); ReleaseDC(pDC); SetDroppedWidth(nMaxItemWidth); SetItemHeight(-1, itemHeight); }
void CFontComboBox::OnDropdown() /* ============================================================ Function : CFontComboBox::OnDropdown Description : Sets the dropped down width of the combo control to the max width of the string in the list Access : Protected Return : void Parameters : none Usage : Called from MFC ============================================================*/ { int scroll = ::GetSystemMetrics(SM_CXVSCROLL); SetDroppedWidth( m_maxWidth + scroll ); }
void CFontComboBox::OnDropdown() { int nNumEntries = GetCount(); int nWidth = 0; CString str; CClientDC dc(this); int nSave = dc.SaveDC(); dc.SelectObject(GetFont()); int nScrollWidth = ::GetSystemMetrics(SM_CXVSCROLL); //取滚动条宽度 for (int i = 0; i < nNumEntries; i++) { GetLBText(i, str); int nLength = dc.GetTextExtent(str).cx + nScrollWidth; nWidth = max(nWidth, nLength); } nWidth += dc.GetTextExtent(_T("0")).cx; dc.RestoreDC(nSave); if (!m_pFontVec.empty()) SetDroppedWidth(nWidth + FNTIMG_XSIZE); //设置宽度值 }
BOOL CInPlaceList::OnDropdown() { // TODO: Add your control notification handler code here SetCursor(LoadCursor(NULL, IDC_ARROW)); int dx = 0; CSize sz(0,0); TEXTMETRIC tm; CString sLBText; CDC* pDC = GetDC(); CFont* pFont = 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); const int nCount = GetCount(); for(int i = 0; i < nCount; ++i) { GetLBText(i, sLBText); sz = pDC->GetTextExtent(sLBText); // 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); ReleaseDC(pDC); // Adjust the width for the vertical scroll bar and the left and right border. dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE); if(GetDroppedWidth() < dx) SetDroppedWidth(dx); return Default() != 0; }
CInPlaceList::CInPlaceList(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID, int nRow, int nColumn, CStringArray& Items, CString sInitText, UINT nFirstChar) { m_nNumLines = 4; m_sInitText = sInitText; m_nRow = nRow; m_nCol = nColumn; m_nLastChar = 0; m_bExitOnArrows = FALSE; //(nFirstChar != VK_LBUTTON); // If mouse click brought us here, // Create the combobox DWORD dwComboStyle = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL| CBS_AUTOHSCROLL | dwStyle; int nHeight = rect.Height(); rect.bottom = rect.bottom + m_nNumLines*nHeight + ::GetSystemMetrics(SM_CYHSCROLL); if (!Create(dwComboStyle, rect, pParent, nID)) return; // Add the strings for (int i = 0; i < Items.GetSize(); i++) AddString(Items[i]); // Get the maximum width of the text strings int nMaxLength = 0; CClientDC dc(GetParent()); CFont* pOldFont = dc.SelectObject(pParent->GetFont()); for (i = 0; i < Items.GetSize(); i++) nMaxLength = max(nMaxLength, dc.GetTextExtent(Items[i]).cx); nMaxLength += (::GetSystemMetrics(SM_CXVSCROLL) + dc.GetTextExtent(_T(" ")).cx*2); dc.SelectObject(pOldFont); if (nMaxLength > rect.Width()) rect.right = rect.left + nMaxLength; // Resize the edit window and the drop down window MoveWindow(rect); SetFont(pParent->GetFont()); SetItemHeight(-1, nHeight); SetDroppedWidth(nMaxLength); SetHorizontalExtent(0); // no horz scrolling // Set the initial text to m_sInitText if (SelectString(-1, m_sInitText) == CB_ERR) SetWindowText(m_sInitText); // No text selected, so restore what was there before // Subclass the combobox edit control if style includes CBS_DROPDOWN if ((dwStyle & CBS_DROPDOWNLIST) != CBS_DROPDOWNLIST) { m_edit.SubclassDlgItem(IDC_COMBOEDIT, this); SetFocus(); switch (nFirstChar) { case VK_LBUTTON: case VK_RETURN: m_edit.SetSel((int)_tcslen(m_sInitText), -1); return; case VK_BACK: m_edit.SetSel((int)_tcslen(m_sInitText), -1); break; case VK_DOWN: case VK_UP: case VK_RIGHT: case VK_LEFT: case VK_NEXT: case VK_PRIOR: case VK_HOME: case VK_END: m_edit.SetSel(0,-1); return; default: m_edit.SetSel(0,-1); } SendMessage(WM_CHAR, nFirstChar); } else SetFocus(); }
void CInPlaceList::OnDropdown() { SetDroppedWidth(GetCorrectDropWidth()); }
CInPlaceList::CInPlaceList(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID, int nRow, int nColumn, COLORREF crFore, COLORREF crBack, IEnumerator<tstring> *items, CString sInitText, UINT nFirstChar) { m_crForeClr = crFore; m_crBackClr = crBack; m_nNumLines = 20; m_sInitText = sInitText; m_nRow = nRow; m_nCol = nColumn; m_nLastChar = 0; m_bExitOnArrows = FALSE; //(nFirstChar != VK_LBUTTON); // If mouse click brought us here, // Create the combobox DWORD dwComboStyle = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL| CBS_SORT | CBS_AUTOHSCROLL | dwStyle; int nHeight = rect.Height(); rect.bottom = rect.bottom + m_nNumLines*nHeight + ::GetSystemMetrics(SM_CYHSCROLL); if (!Create(dwComboStyle, rect, pParent, nID)) return; // Add the strings tstring item; items->Reset(); while(items->MoveNext(item)) { AddString(item.c_str()); } SetFont(pParent->GetFont()); SetItemHeight(-1, nHeight); int nMaxLength = GetCorrectDropWidth(); /* if (nMaxLength > rect.Width()) rect.right = rect.left + nMaxLength; // Resize the edit window and the drop down window MoveWindow(rect); */ SetDroppedWidth(nMaxLength); SetHorizontalExtent(0); // no horz scrolling // Set the initial text to m_sInitText if (::IsWindow(m_hWnd) && SelectString(-1, m_sInitText) == CB_ERR) SetWindowText(m_sInitText); // No text selected, so restore what was there before ShowDropDown(); // Subclass the combobox edit control if style includes CBS_DROPDOWN if ((dwStyle & CBS_DROPDOWNLIST) != CBS_DROPDOWNLIST) { m_edit.SubclassDlgItem(IDC_COMBOEDIT, this); SetFocus(); switch (nFirstChar) { case VK_LBUTTON: case VK_RETURN: m_edit.SetSel((int)_tcslen(m_sInitText), -1); return; case VK_BACK: m_edit.SetSel((int)_tcslen(m_sInitText), -1); break; case VK_DOWN: case VK_UP: case VK_RIGHT: case VK_LEFT: case VK_NEXT: case VK_PRIOR: case VK_HOME: case VK_END: m_edit.SetSel(0,-1); return; default: m_edit.SetSel(0,-1); } SendMessage(WM_CHAR, nFirstChar); } else SetFocus(); }
CInPlaceList::CInPlaceList(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID, int nRow, int nColumn, Strings& Items, CString sInitText, UINT nFirstChar) { m_nNumLines = 2; m_sInitText = sInitText; m_nRow = nRow; m_nCol = nColumn; m_nLastChar = 0; m_bEdit = FALSE; // Create the combobox DWORD dwComboStyle = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL| CBS_AUTOHSCROLL | dwStyle; if (!Create(dwComboStyle, rect, pParent, nID)) return; COMBOBOXINFO cbInfo; cbInfo.cbSize = sizeof(COMBOBOXINFO); GetComboBoxInfo(&cbInfo); m_edit.SubclassWindow(cbInfo.hwndItem); m_ListBox.SubclassWindow(cbInfo.hwndList); // Add the strings for (size_t i = 0; i < Items.size(); i++) AddString(Items[i]); // Get the maximum width of the text strings int nMaxLength = 0; CClientDC dc(GetParent()); CFont* pOldFont = dc.SelectObject(pParent->GetFont()); for (size_t i = 0; i < Items.size(); i++) nMaxLength = max(nMaxLength, dc.GetTextExtent(Items[i]).cx); nMaxLength += (::GetSystemMetrics(SM_CXVSCROLL) + dc.GetTextExtent(_T(" ")).cx*2); dc.SelectObject(pOldFont); // Resize the edit window and the drop down window SetFont(pParent->GetFont()); SetDroppedWidth(nMaxLength); SetHorizontalExtent(0); // no horz scrolling // Set the initial text to m_sInitText SetWindowText(m_sInitText); // No text selected, so restore what was there before ShowDropDown(); SetFocus(); // Added by KiteFly. When entering DBCS chars into cells the first char was being lost // SenMessage changed to PostMessage (John Lagerquist) switch (nFirstChar) { case VK_RETURN: break; default: PostMessage(WM_CHAR, nFirstChar); } }