Bitmap images not showing in CListCtrl report view - c++

I'm trying to create a dialog in MFC that contains a CListCtrl to display a list of items with associated images. However, the images are being displayed as blank white squares. They are there, or at least, there is a space where they should be.
I am trying to load the bitmap from a file (although I have also tried loading from a resource ID which has the same effect) and I am storing it in a CImageList. This image list is then given to the CListCtrl. I'm fairly certain that the bitmap is being correctly loaded as I have managed to load the same bitmap successfully elsewhere in my project using the same code.
I'm not sure whether this is important, but this is part of a context menu shell extension and the dialog is raised when the user clicks on one of the items in the explorer context menu. Also, I'm relatively new to MFC so apologies if I've just missed something really obvious.
The following is my code for initialising the CListCtrl in report view with two columns and one item which should have the image in the first column and some text in the second:
// Get reference to list control
CListCtrl m_list_control = (CListCtrl*)GetDlgItem(IDC_LISTCONTROL);
// Create image list
CImageList image_list;
image_list.Create(32, 32, ILC_COLOR4, 0, 3);
HANDLE hBitMap = ::LoadImage(0, L"E:\pathtomybitmap\bitmap1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap bitmap;
bitmap.Attach((HBITMAP)hBitMap);
image_list.Add(&bitmap, RGB(255, 0, 255));
// Add the image list to the list control
// (LVSIL_NORMAL didn't seem to show anything at all)
m_list_control->SetImageList(&image_list, LVSIL_SMALL);
// Add columns
LVCOLUMN column;
column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_IMAGE;
column.fmt = LVCFMT_LEFT | LVCFMT_IMAGE;
column.cx = 100;
column.pszText = (LPWSTR)&L"Image";
column.iImage = 0;
m_list_control->InsertColumn(0, &column);
m_list_control->InsertColumn(1, _T("Text"), LVCFMT_LEFT, 100);
int index = m_list_control->InsertItem(0, _T(""), 0);
m_list_control->SetItemText(0, 1, _T("My text"));
Any idea what I'm doing wrong?

Try changing create statement so that the size you put is smaller than the image size
image_list.Create(31, 31, ILC_COLOR4, 0, 3);
Try setting the mask argument of insert item to:
InsertItem(LVIF_TEXT | LVIF_IMAGE,...

Related

winapi - Date time picker, change size of prev/next button

I have this code:
HWND hwndDP = CreateWindowEx(0,
DATETIMEPICK_CLASS,
TEXT("DateTime"),
WS_BORDER | WS_CHILD | WS_VISIBLE,
20, 50, 400, 60,
hDlg,
NULL,
NULL,
NULL);
HFONT hFont = CreateFont(40, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");
SendMessage(hwndDP, WM_SETFONT, WPARAM(hFont), TRUE);
which creates date time picker like in image below. AS you can see, I change the size of font, so the font of date time picker is bigger. I have problem, that buttons which are circled (next/prev month) don't change the size and are small. Is it possible to change the size of this buttons. I need it because I use this date time picker on monitor with touch and is hard to click on this two buttons.
Unfortunately there's no easy way to change this that I can see. Those controls aren't actually real buttons - they're just drawn on the window, and the calendar control bases the size of them on the value of NONCLIENTMETRICS.iScrollHeight - it doesn't let you change them.
The only way to change this is globally which would affect all scrollbars in the system, probably not something you want to do.
One idea I have for you would be to sub-class the control to put your own (real) button controls over the top of the existing ones.
Then, when your sub-class procedure gets the WM_COMMAND message to say they've been clicked, send the calendar control a WM_KEYDOWN message for VK_NEXT or VK_PRIOR - those keys will cause it to scroll a month in either direction.

Draw 3 images and text in listview

I'm not using mfc.
I make my own list-view (to shown data in table)
hListView = CreateWindowEx(0, WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT |
LVS_EDITLABELS, 0, 0, h_rcl.right - h_rcl.left, h_rcl.bottom - h_rcl.top,
hwnd_main, (HMENU)1000, hInstance, NULL);
But in one of column i want to show data like:
"Text | Text " (ofc as i want to put a img)
So my table would look like:
# | column1 | column 2
1 | "text <img> | text <img>" | text
Best Regards
Hahaha:D Right...
How to draw this images with this text?
I dont think that it is possible with:
ListView_SetItemText(hListView, items_num, 1, "test");
You can either:
enable the LVS_EX_SUBITEMIMAGES window style and then specify a per-item per-column image index into an ImageList that you associate with the ListView.
custom-draw the ListView items.

win32 List View Abbreviating Text

I'm using win32 to create a list view with downloaded icons, however, the text is abbreviated at approximately 19 characters (as about size 12 font, Segoe UI). I have included the CreateWindow and item creation code I'm using for it.
Any advice would be appreciated.
HWND airlinelist = CreateWindow(WC_LISTVIEW,L"",WS_CHILD | LVS_LIST | WS_TABSTOP | WS_BORDER,18,104,323,74,hwnd,(HMENU)3,hinst,NULL);
LVITEM newi;
ZeroMemory(&newi,sizeof(LVITEM));
const wchar_t* n = L"Client Website Name, website.com"
newi.pszText = newc;
newi.mask = LVIF_TEXT | LVIF_IMAGE;
newi.iImage = 0;
ListView_InsertItem(airlinelist,&newi);
The above would create a list view with the icon and something to the effect of "Client Website Nam..." despite it only taking up half of the list view's width.
I'll assume you're using LVS_LIST mode because the style is shown in your code sample. You can use the LVM_SETCOLUMNWIDTH message to adjust the column size once you've added items to the list control. You can also use the ListView_SetColumnWidth macro. E.g.:
SendMessage(airlinelist, LVM_SETCOLUMNWIDTH, 0, 300);
This would set the columns to 300 pixels wide. If you're actually using LVS_REPORT mode you would need to set the width for each column individually.

HSCROLL and VSCROLL in Combobox

I have successfully created a combobox with HSROLL as follows :
HWND find = CreateWindowEx(0, WC_COMBOBOX, _T(""), CBS_DROPDOWN | WS_VISIBLE | WS_CHILD |
CBS_AUTOHSCROLL | WS_HSCROLL | WS_VSCROLL,0, 1, 100 30,
hwndToolbar, (HMENU)0, ghinst, NULL);
SendMessage(find, CB_SETHORIZONTALEXTENT, (WPARAM)1000, 0);
But it also gives the VSCROLL, even if there are only 2 items. This is very ugly. If I drop the WS_VSCROLL, it solves the problem.
On the other hand, I also used :
SendMessage(find, CB_SETMINVISIBLE, (WPARAM)20, 0);
to show 20 items only, other items are showed by VSCROLL. The VScroll bar will appear only if there are more than 20 items. So I can not drop WS_VSCROLL.
Do you have any idea about how to use 'CB_SETHORIZONTALEXTENT' without VSCROLL bar when there are fewer items ?
It seems the height of the combo box is too small to accommodate the number of items. As many items will be shown on drop down, as the height specified. If items cannot fit into this height, then the vertical scroll bar will appear.

Understanding wxWidgets sizers

I'm still getting used to the sizers in wxWidgets, and as such can't seem to make them do what I want.
I want a large panel that will contain a list of other panels/boxes, which each then contain a set of text fields
----------------------
| label text box |
| label2 text box2 |
----------------------
----------------------
| label text box |
| label2 text box2 |
----------------------
----------------------
| label text box |
| label2 text box2 |
----------------------
I also need to be able to add (at the end), and remove(anywhere) these boxes.
If there's too many to fit in the containing panel a vertical scroll bar is also required.
This is what I've tried so far, it works for the first box that's created with the containing panel, but additional added items are just a small box in the top left of the main panel, even though the sizer code is the same for all boxes.
//itemsList is a container containg a list of *Item pointers to each box/panel/whatever the right name is
Items::Items(wxWindow *parent)
:wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_SUNKEN)
{
//one sstarting item
OnAdd(wxCommandEvent());
}
void Items::OnAdd(wxCommandEvent &event)
{
unsigned id = itemsList .size();
Item *item = new Item(this,id);
itemsList .push_back(item);
RebuildSizer();
}
void Items::RebuildSizer()
{
this->SetSizer(0,true);
wxBoxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
for(std::vector<Item*>::iterator it = itemsList .begin(); it != itemsList .end(); ++it)
sizerV->Add(*it, 1, wxEXPAND | wxLEFT | wxRIGHT, 5);
SetSizer(sizerV);
}
void Items::OnRemove (wxCommandEvent &event, unsigned itemId)
{
delete itemsList [itemId];
itemsList .erase(items.begin()+itemId);
for(std::vector<Item*>::iterator it = itemsList .begin()+itemId; it != itemsList .end(); ++it)
(*it)->ChangeId(itemId++);
RebuildSizer();
}
Also what's the best way to lay out the contents of each box? I was thinking of using a 2 by 2 grid sizer but I'm not sure how to make the text boxes to expand to be as wide as possible while making the labels stay as small as possible (but also maintaining the alignment between the 2 text boxes)?
"If theres too many to fit in the containing panel a vertical scroll bar is also required."
You could have a look at wxScrolledWindow.
"additional added items are just a small box in the top left of the main panel"
I am not sure, but, maybe a call to wxSizer::Layout() will help.
"Also whats the best way to lay out the contents of each box?"
Have a look at this sizerdemo. If it is not mandatory, that the labels stay as small as possible, you could give the labels a fixed width and only let the text boxes grow. If you want to adapt the size when adding or removing new boxes, you could implement the OnSize() event handler.
May I suggest to you to post this question to one of the wxWidgets mailing list? They will be able to help you very quickly.
Can I also recommend the wxForum, I have found it very useful for wxWidgets questions in the past.
More specifically for scrolling wxScrolledWindow should help, use wxScrolledWindow->SetSizer with your top level sizer (the one that contains your controls) to set up a scrolled area, also check out the samples called scroll, scrollsub and vscroll included in wxWidgets (in the samples directory of your wxwidgets install).