MFC CListCtrl - how to move item text to the left - c++

I'm trying to implement single-column CListCtrl (or CMFCListCtrl, doesn't matter) in a way, that some rows might have checkboxes and some might not (I don't want to use neither CListBox, nor CCheckListBox, because in the future I'm planning to use multiple columns). I'm using LVS_EX_CHECKBOXES style, but that forces every item to have a checkbox. Then I manually delete the checkbox with a custom draw handler, but then I'm having trouble moving the item's text to the left side so that it takes place of the erased checkbox.
This is what my list control looks like:
But I need it to look like this (item2 is aligned to the left border, taking place of the erased checkbox):
I create my list control dynamically like this:
list->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_NOCOLUMNHEADER
rect, this, SOME_ID);
list->SetExtendedStyle(list->GetExtendedStyle() | LVS_EX_CHECKBOXES);
And my custom draw handler function looks like this:
I create my list control dynamically like this:
void MyCListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = CDRF_DODEFAULT;
LPNMLVCUSTOMDRAW lpn = (LPNMLVCUSTOMDRAW)pNMHDR;
if (CDDS_PREPAINT == lpn->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYITEMDRAW; // get notification for every row (item)
}
else if (CDDS_ITEMPREPAINT == lpn->nmcd.dwDrawStage)
{
int row = lpn->nmcd.dwItemSpec;
if (row == 1) { // we are in the first row (item2)
lpn->nmcd.rc.left -= 20; // doesn't do anything
lpn->rcText.left -= 20; // doesn't do anything
// this erases checkbox from the current row
SetItemState(row, INDEXTOSTATEIMAGEMASK(0), LVIS_STATEIMAGEMASK);
}
}
}
Is there any way to achieve the desired result? Am I doing it the right way, or is it better to use CListCtrl without the LVS_EX_CHECKBOXES and draw the checkboxes myself where I want to? If so, how? Thanks in advance.

You could try indentation with minus value (I didn't tried though):
LV_ITEM lvItem;
lvItem.iItem = nYourItem;
lvItem.iSubItem = 0;
lvItem.mask = LVIF_INDENT;
lvItem.iIndent = -1;
VERIFY(SetItem(&lvItem));

If it would be an option for you, why don't you just leave it as it is?
The first version looks much more uncluttered, anyway.
The second version breaks guidance of the eyes.

Related

MFC/C++ ComboBox: disable drawing of Dropdown closing & opening (UI freeze)

I've just added an Item-Filter-Feature to a CComboBox derived class called
ComboBoxFbp in an old MFC application.
BOOL CComboBoxFbp::OnEditChange()
{
CString csText;
if (m_wFbpMode & _FbpMode_UserTextFiltersList) {
GetWindowText(csText);
// This makes the DropDown "flicker"
// ShowDropDown(false);
// Just insert items that match
FilterItems(csText);
// Open DropDown (does nothing if already open)
ShowDropDown(true);
}
return FALSE; // Notification weiterleiten
}
void CComboBoxFbp::FilterItems(CString csFilterText)
{
CString csCurText;
int nCurItem;
DWORD wCurCursor;
// Text/selection/cursos restore
GetWindowText(csCurText);
nCurItem = GetCurSel();
if (nCurItem != CB_ERR && nCurItem >= 0 && nCurItem < GetCount()) {
CString csCurItemText;
GetLBText(nCurItem, csCurItemText);
if (csCurItemText == csCurText) csCurText = csCurItemText;
else nCurItem = CB_ERR;
} else {
nCurItem = CB_ERR;
}
wCurCursor = GetEditSel();
// Delete all items
ResetContent();
csFilterText.MakeLower();
// Add just the items (from the vector of all possibles) that fit
for (auto item : m_vItems)
{
CString csItemText = item.first;
csItemText.MakeLower();
if (!csFilterText.IsEmpty() && csItemText.Find(csFilterText) < 0)
continue;
const int i = AddString(item.first);
SetItemData(i, item.second);
}
// Text/selection/cursos restore
if (nCurItem != CB_ERR) SelectString(-1, csCurText);
else SetWindowText(csCurText);
SetEditSel(LOWORD(wCurCursor), HIWORD(wCurCursor));
}
So when the user types, the long list of items in the DropDown gets filtered accordingly. Everything's fine so far.
The size/height of the ListBox/DropDown doesn't change once its open. It does change accordingly when die DropDown opens. Meaning if there are only 2 items the DropDown is only 2 items high.
My issue
When the user enters a text where just one item fits the DropDown is only 1 item in height (this happens with some user workflows, i.e. user manually closes & opens the DropDown).
Now when the user now changes the text so multiple items are fitting the height stays 1 item and it looks weird as even the scrollbar doesn't look correct as it doesn't fit.
What I've tried so far
I cannot use CComboBox::SetMinVisibleItems (or the MSG behind it) as it only works in a Unicode CharacterSet (which I'm not able to change in this old application) and from WinVista onwards (app runs on WinXP).
The only other option is to close and open the DropDown so it gets redrawn correctly with the correct height (see // This makes the DropDown "flicker" in Source Code above).
Now going with option 2 I don't want the user to see the closing and opening ("flicker") of the DropDown after every key he is pressing.
To prevent this I've tried a couple of solutions I've found but none works in my case with a ComboBox-DropDown. Here's a list of methods I've put just before the ShowDropDown(false) and just after the ShowDropDown(true).
EnableWindow(false/true);
(Un)LockWindowUpdate();
SendMessage(WM_SETREDRAW, FALSE/TRUE, 0)
With all three calls I still see the DropDown closing/opening.
Do you guys have other ideas how I can prevent this flicker?
Thanks in advance
Soko
This is an XY question.
It should be easier to use the following approach to adjust the height of the ComboBox
Use GetComboBoxInfo to get the handle of the list control.
Use OnChildNotify or ON_CONTROL_REFLECT and capture CBN_DROPDOWN.
In the handler of the message resize the window as needed Use SetWindowPos and just change the size.

No LVIF_TEXT in LVN_GETDISPINFO message using LVS_OWNERDATA mode

I've encountered strange behavior in MFC when using list control (CListCtrl) with LVS_REPORT style in "virtual mode" i.e. with LVS_OWNERDATA style. List view itself is placed on a modeless dialog.
In dialog's OnInitDialog method I add two columns -- "Column 1" and "Column 2" in two different ways -- using LVCOLUMN and ListView_InsertColumn macro (1) and using CHeaderCtrl class with HDITEM structure (2).
When using first method (1), in LVN_GETDISPINFO message handler (message is handled by dialog, simple ON_NOTIFY) I receive a mask (NMLVDISPINFO.item.mask member) with different bits set (LVIF_TEXT, LVIF_IMAGE, LVIF_STATE, LVIF_INDENT etc.), after filling appropriate fields in NMLVDISPINFO.item structure, everything works fine.
But, when using second method (2) involving CHeaderCtrl class, the only bit in mask that is set is LVIF_INDENT, I never receive mask with anything else set.
Here are pieces of code that I use to add those columns:
Method one (1):
LVCOLUMN col;
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.pszText = _T("Column 1");
col.cx = 100;
ListView_InsertColumn(m_MyList, 0, &col); //m_MyList is of CListCtrl type
col.pszText = _T("Column 2");
ListView_InsertColumn(m_MyList, 1, &col);
And method two (2):
HDITEM hdItem;
hdItem.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
hdItem.fmt = HDF_STRING;
hdItem.cxy = 100;
hdItem.pszText = _T("Column 1");
hdItem.cchTextMax = _tcslen(hdItem.pszText);
m_MyList.GetHeaderCtrl()->InsertItem(0, &hdItem);
others_info.pszText = _T("Column 2");
others_info.cchTextMax = _tcslen(hdItem.pszText);
m_MyList.GetHeaderCtrl()->InsertItem(1, &hdItem);
What could possibly cause such behavior?
Method 2 is incorrectly using others_info for some operations but not others. It looks like the first InsertItem is using a text length that is possibly 0. The second call to InsertItem reuses the same hdItem structure which hasn't been changed since the first call.

Changing color of a specific character in an item in CListCtrl in MFC

I have a CListCtrl and I need to change the color of A SPECIFIC character/set of characters (which I choose by comparison) from the text of every cell in the list.
I know how to change the color of the entire text of the cell when I find the character/set of characters (by using 'strstr' command), but I can't find an example which shows how to change ONLY the character/set of characters.
Here is a sample of my code:
void Agenda::OnCustomdrawMyList( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)pNMHDR;
*pResult = CDRF_DODEFAULT;
if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYITEMDRAW;
return;
}else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return;
}else if ( (CDDS_SUBITEM | CDDS_ITEMPREPAINT) == pLVCD->nmcd.dwDrawStage )
{
// So right now I am in the stage where a SUBITEM is PREPAINTED
int nItem = pLVCD->nmcd.dwItemSpec;
int nSubItem = pLVCD->iSubItem;
char a[100];
listControl.GetItemText(nItem,nSubItem,a,100);
COLORREF textColorFound, textColorDefault;
textColorDefault = RGB(0,0,0);
pLVCD->clrText = textColorDefault;
char* startingFrom;
if( (startingFrom = strstr(a,filterText)) != NULL ) {
// Could I set a pointer here or something like that so
// the coloring could start only from 'startingFrom'
// and stop at 'strlen(filterText)' characters?
textColorFound = RGB(205,92,92);
pLVCD->clrText = textColorFound;
}
*pResult = CDRF_DODEFAULT;
}
}
listControl is the variable for my CListCtrl
the other things are pretty self-explanatory
No, you cannot do this. What you will have to do is custom-draw the text in question. This will be tricky because you will have to do it with two different calls, between which you will have to manually adjust the color and the drawing location to account for the intercharacter spacing etc. And you better hope that you don't need to do multi-line output.
Take a look at the article Neat Stuff to Do in List Controls Using Custom Draw by Michael Dunn on CodeProject to get some ideas on how to proceed.
Alternatively, if you can use the Toolkit Pro toolkit from CodeJock you can leverage their "XAML" support (I use quotes because it's not really XAML, but their own implementation of a subset of XAML) and let them do all the hard work.
Digging on the same issue; But I wouldn't go so far as modifying/adding to the default Windows behaviour for painting strings... apparently that would be the endpoint of having it owner-drawn.(aici am murit si eu :).

When deleting item in Combobox the secong one pop-up MFC

I have combo box and delete button. I want to make next combo box item pop-up when delete button pressed and when last item deleted clean combo box selected item.
I tried several methods with indexes but even one wont help me.
there is my code:
if(IDYES == MessageBox(L"Delete save?",L"Delete", MB_YESNO|MB_ICONQUESTION)){
CString pFileName = L"Save\\"+str+".dat";
CFile::Remove(pFileName);
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SAVE);
pComboBox->ResetContent();
}
How I can to make next combo box item pop-up when delete button pressed and when last item deleted clean combo box selected item?
I found a solution:
void CL2HamsterDlg::OnBnClickedButtonDelete(){
if(Validate()){
if(IDYES == MessageBox(L"Delete save?",L"Delete", MB_YESNO|MB_ICONQUESTION)){
CString pFileName = L"Save\\"+str+".dat";
CFile::Remove(pFileName);
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SAVE);
lookforfile();
int nIndex = pComboBox->GetCurSel();
if (nIndex == CB_ERR)
pComboBox->SetCurSel(0);
else{
pComboBox->SetEditSel(0, -1);
pComboBox->Clear();
}
}
LoadSave(false);
}else
AfxMessageBox(L"Please select or write correct name!");
}
the function look for file refreshes index
void CL2HamsterDlg::lookforfile()
{
Value.GetWindowText(str);
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SAVE);
pComboBox->ResetContent();
GetCurrentDirectory(MAX_PATH,curWorkingDir);
_tcscat_s(curWorkingDir, MAX_PATH, _T("\\Save\\*.dat"));
BOOL bWorking = finder.FindFile(curWorkingDir);
while (bWorking){
bWorking = finder.FindNextFile();
if (!finder.IsDots())
pComboBox->AddString(finder.GetFileTitle());
}
GetDlgItem(IDC_COMBO_SAVE)->SetWindowText(str);
}
so, in this case you do not need to use ResetContent(). Provided you already know the currently selected Item in the combobox (I think somewhere along the track you would have used the line int iSel = pComboBox->GetCurSel();) you could use this code IN PLACE OF YOUR pComboBox->ResetContent();:
pComboBox->DeleteString(iSel);
if(iSel < pComboBox->GetCount())
pComboBox->SetCurSel(iSel);
else if(iSel > 0)
pComboBox->SetCurSel(iSel-1);
However, I think this will not be necessary. I think the item will move by itself. So, forget about the code above, just use this:
pComboBox->DeleteString(pComboBox->GetCurSel())

MFC ListView Change Item Image

I have added items to ListControl, they have images. Now I want to change them, I tried to do GetItem and SetItem, but I was not able. At least I don't know how to get an Item I want. How I can change Image of an item in ListView?
Thanks
P.S.
I've managed to solve it. Here is solution:
This is how to loop
LVITEMW pitem;
ZeroMemory(&pitem, sizeof(pitem));
pitem.mask = LVIF_TEXT | LVIF_IMAGE;
pitem.iItem = <SET INDEX OF YOUR ITEMS HERE, YOU CAN LOOP HERE>;
pitem.iSubItem = 0;
pitem.pszText = new wchar_t[256];
pitem.cchTextMax = 255;
mlist.GetItem(&pitem);
And after selecting an item, you can change it's image like this:
pitem.iImage = newindex;
mlist.SetItem(&pitem);
Using CListCtrl::SetItem is right. You have to set the nMask parameter to LVIF_IMAGE and provide the index of the image in der image-list in the iImage parameter.
The solution highlighted in the first post was not working for me. After a short look to Microsoft Documentation, the signature of the SetItem function is:
BOOL SetItem(const LVITEM* pItem);
pItem should be a pointer to a LVITEM which is const, which is not really the case in this situation...
However, the next solution is working for me:
// Let's say my CListCtrl is named m_listCtrl
// Loop on items of CListCtrl
for( int i = 0; i < m_listCtrl.GetItemCount(); i++ )
{
// And then you define a new image with the index iImage for the item i
m_listCtrl.SetItem(i, 0, LVIF_IMAGE, NULL,
iImage, 0, 0, 0);
}