Binding a CPen to a list box - c++

Does anyone know how to bind a CPen object to a listbox in VS2005 C++?
Can I do it as a ToString with some sort of conversion?
I am creating a custom list of different pens for the user to select.
Thanks.
COLORREF rgbRED = (255,0,0);
CPen penRed(PS_SOLID,3,rgbRED);
CDialog::OnInitDialog();
ShowWindow(SW_SHOW);
UpdateData();
lbLineWeight.InsertString(penRed);

2 options.
(simple) Use a normal CListBox with strings as the items, and keep the link between the string to the actual CPen as free functions (or member of some other classes) and you will have to do a one-to-one association between the current selected item (usually an index number) and the CPen information you have.
(a bit more complex) Derive your own class from CListBox and keep the CPen data internally, you will still have to keep a list of valid CPen in that new class, and do the one-to-one association between the selected item and the actual CPen; as a bonus you can make you derived CListBox owner-drawn and instead of using string, you could draw some sort of representation of each pen in the list items.
Another tought, you could add the CPen as a user data to each CListBox item (CListBox::SetItemData) to make the link between the item and the actual item a bit more easy.
Good luck.
Max.

Assuming I understand you correctly you want to have a CListBox which allows the user to select a CPen for use elsewhere.
I would probably make a little helper class:
struct PenHelper
{
CString m_displayName;
LOGPEN m_penProps;
bool CreatePen(CPen* pPen)
{
return pPen->CreatePenIndirect(&m_penProps) == 1;
}
};
The idea being you could have a container like std::map of multiple PenHelper each with a names like "Solid Red" and a corresponding LOGPEN struct with properties that match the name. In the CListBox you add the display name string. When they select one you can look it up by name and use the create function to actually make the corresponding CPen
Just one of a million ways to skin a cat.
Edit: Quick note. In order to handle ON_LBN_SELCHANGE in your message map for when they make a selection in your CListBox make sure you gave it the LBS_NOTIFY style in the Create call otherwise it won't fire.

Related

C++ Windows Owner Drawn Main Menu -- itemID

I am attempting to create an owner-drawn main menu, in Windows. I understand setting:
menuiteminfo.ftype = MFT_OWNERDRAW
I also know about handling the WM_MEASUREITEM and WM_DRAWITEM messages.
However, how do I know which main menu item is sending the message? (so that I can fill-in the appropriate box size and text) "itemID" seems to be the only unique identifier. But, how can I associate this pointer/handle to the item in question? I can use "lParam" to determine it is a menu item. But, I can't determine which menu item. "GetMenuItemID" is useless, as it returns "-1" for all main-menu items.
Or, am I going about this all-wrong? I have been searching for answers, for weeks. Really, all I want to do is change the text color of the main menu, from black, to white or light gray, so I can use a dark background.
The itemID field of the MEASUREITEMSTRUCT and DRAWITEMSTRUCT structs tells you exactly which menu item is being measured/drawn. This is the ID that you specify when you create/modify a menu item. That ID is specified via either:
the uIDNewItem parameter of AppendMenu(), InsertMenu(), or ModifyMenu().
the item parameter of InsertMenuItem() or SetMenuItemInfo()
the wID field of the MENUITEMINFO struct that you pass to InsertMenuItem() or SetMenuItemInfo().
Use whatever IDs you want, as long as they are unique for your menus.
You can also use the itemData field of MEASUREITEMSTRUCT and DRAWITEMSTRUCT to receive any custom data you want for owner-drawn menu items, if you so desire (like, for example, a pointer to a buffer that contains a menu item's text string). This custom value can be anything you want that is meaningful for you. You set this value in the dwItemData field of the MENUITEMINFO struct that you pass to InsertMenuItem() or SetMenuItemInfo().
This is all covered in the documentation:
Using Menus: Creating Owner Drawn Menu Items
Thank you Remy. Through the items that you mentioned, and researching the documentation for each, I was able to find a buried secret. For a Main Menu item, the "itemID" in both the MEASUREITEMSTRUCT and DRAWITEMSTRUCT is the handle to the drop-down menu of that item. From that, I added this line of code to WM_CREATE, to associate the itemID with the numerical (zero-based) position:
mItemID[i] = int(GetSubMenu(hMenu, i));
'i' is the numerical position, from left to right. I can then use a comparison statement like this, in WM_MEASUREITEM and WM_DRAWITEM:
lpmis=(LPMEASUREITEMSTRUCT)lParam; if(lpmis->itemID==mItemID[i])

How to get the currently selected text in a CComboBoxEx?

My first approach to the problem was to call the GetWindowsText method on the CComboBoxEx control, but I found that there is no associated text. After analyzing the control with Spy++ and reading some documentation on CComboBoxEx, I realised that these type of controls are only the parent of a classic ComboBox:
I tried using the GetLBText() method on the child ComboBox, passing GetCurSel() as an argument, but I only get some wrong text (the correct text should be "English"):
Am I missing something? Thanks in advance!
What you want to do is map the control to a int variable using Class Wizard:
Now it is easy to access the selected text at any time. You need to use the GetItem function. For example (code not tested):
COMBOBOXEXITEM cmbItem;
CString strText;
cmbItem.mask = CBEIF_TEXT;
cmbItem.iItem = m_cbItemIndex;
cmbItem.pszText = strText.GetBuffer(_MAX_PATH);
m_cbMyCombo.GetItem(&cmbItem);
strText.ReleaseBuffer();
In short, you need to use the COMBOBOXEXITEM and initialise it with the right flags to state what information you want to get from the extended combo. That, and the item index. Job done!
I realise that you have your own inherited class, but the mechanics are the same. You don't use GetLBText. You use the structure with the index and GetItem to get the selected text.
In the end I managed to retrieve the correct name; as you can see in the image below, the ComboBox is only a child of a CombBoxEx32:
I retrieved the pointer to the parent ComboBoxEx32 from the child ComboBox, and searched for the text this way:
CString szText;
CComboBoxEx cbParentCombo ;
cbParentCombo.Attach( GetParent()->GetSafeHwnd()) ;
cbParentCombo.GetLBText( GetCurSel(), szText) ;
cbParentCombo.Detach() ;
My mistake was that I was calling GetLBText() directly from the child ComboBox, instead of the parent CComboBoxEx; because of that, all I was getting was some random gibberish. GetLBText() was indeed the correct solution.

Virtual CListCtrl Auto Size

I want to resize the columns of a virtual ClistCtrl (LVS_OWNERDATA flag) automatically.
I found in some forums that virtual lists can not use the "LVSCW_AUTOSIZE" option. Some suggest to implement an algorithm instead.
But once loaded my ClistCtrl without any resize option, a double-click on the header divider correctly resizes the visible columns.
So, how I can perform the function that is called by "HDN_DIVIDERDBLCLICKW"?
The autosizing suggested by Clements works for normal list controls, but not for virtual ones (because the control doesn't know anything about the column data). You have to provide the data column width yourself.
From this Codeproject article, you should be able to autosize a column with something like:
pListCtrl->SetColumnWidth(i, LVSCW_AUTOSIZE);
int nColumnWidth = pListCtrl->GetColumnWidth(i);
pListCtrl->SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
int nHeaderWidth = pListCtrl->GetColumnWidth(i);
pListCtrl->SetColumnWidth(i, max(nColumnWidth, nHeaderWidth));
You may need to handle the LVN_GETDISPINFO notification to provide the necessary data to the virtual list control, though...

How to create a multicolumn Listbox?

I'm working on a program, which should list all files and it's size(for now...). I created a simple application, which should write the data to a listbox. I'm trying to write the data in two columns(the first should be the name, and next to it, in an other column, it's size), but i can't figure out, how should i do this.
Can someone help me?
Thanks in advance!
kampi
Update:
I try to use ListControl., but unfortunately i can't. I can succesfully compile my App, but i can only see, the empty rectangle. Does someone know what i am doing wrong?
BOOL CGetFileListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
LVITEM lvItem;
LVCOLUMN lvColumn;
int nCol;
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_CENTER;
lvColumn.cx = 10;
lvColumn.pszText = _T("Filename");
ListView_InsertColumn( m_List, 0, &lvColumn );
ListView_SetItemText( m_List, 0, 0, _T("TEST") );
return TRUE; // return TRUE unless you set the focus to a control
}
The list box control does support multiple columns, but it only supports a single series of entries; the multiple column support just makes the items continue onto the next columns so that vertical scrolling is not necessary.
As Kornel has suggested, a list view control may be more appropriate. After creating a list view control, use ListView_InsertColumn to create the columns. Then use ListView_SetItemText to insert items.
EDIT:
My apoligies; you should use ListView_InsertItem to insert an item (a row) and then use ListView_SetItemText to alter the subitems (columns). If the list view is still just a blank box without any headings, have you initialised common controls? This can be done using InitCommonControlsEx, specifying the ICC_LISTVIEW_CLASSES constant. This should be done before creating the control.
See Microsoft's documentation on list view controls.
Don't use a List Box, use a List Control with LVS_REPORT style.
Maybe to use DataGridView with object as data source.
Three important parameters to be checked are
List Box or the List Control (List Control is to be used)
View parameter must be Report mode
Owner Data must be set to False
The screenshot shows these
The programming flow to add data to list control are Change the list Control to extended list view(ListView_SetExtendedListViewStyle), Create the layout(By adding columns), Add Item data (for each row needed) & add finally add sub-item to each column (for each item data added previously).

MFC: Displaying a tabulated display of text items

This should be simple it seems but I can't quite get it to work. I want a control (I guess CListBox or CListCtrl) which displays text strings in a nice tabulated way.
As items are added, they should be added along a row until that row is full, and then start a new row. Like typing in your wordprocessor - when the line is full, items start being added to the next line, and the control can scroll vertically.
What I get when trying with a list-mode CListCtrl is a single row which just keeps growing, with a horizontal scroll bar. I can't see a way to change that, there must be one?
You probably need a list control wth LVS_REPORT. If you expect the user to add items interactively using a keyboard, you probably need a data grid, not a list. Adding editing to list control subitems is not easy, and it would be easier to start from CWnd. Search "MFC Data Grid" to find some open source class libraries that implemented the feature.
If you can afford adding /clr to your program, you can try the data grid classes in Windows Forms using MFC's Windows Form hosting support. You will find a lot more programming resources on data grid classes in Windows Forms than any other third-party MFC data grid class library.
If you use CRichEditCtrl you can set it to word-wrap, take a look at this snippet extracted from:
http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.ui/2004-03/0111.html
(I've derived my own QRichEditCtrl from the MFC CRichEditCtrl,
and here's the relevant code:)
void QRichEditCtrl::SetWordWrap(bool bWrap)
{
RECT r;
GetWindowRect(&r);
CDC * pDC = GetDC();
long lLineWidth = 9999999; // This is the non-wrap width
if (bWrap)
{
lLineWidth = ::MulDiv(pDC->GetDeviceCaps(PHYSICALWIDTH),
1440, pDC->GetDeviceCaps(LOGPIXELSX));
}
SetTargetDevice(*GetDC(), lLineWidth);
}