I've been searching the net for different things about the win32 API, but it seems all information on it is rather sparse.
I am looking to create a simple window that shows a list of items, however I want to display different columns of data for each item, in a table-style format, where the user could perhaps be allowed to resize the different column widths.
If at all possible, I'd like to also be able to change the background colors of different rows, in the code, between just a general white, red, yellow, or green.
And the user would also be allowed to right click different rows, and be able to call a function on them, or copy the data to the clipboard (but that part is a different story).
Now, I've found list-viewer objects(?) that can be placed in the window, buttons, and right-click menus... but I cannot figure out how to do a table, using the Win32 API. I haven't even really read up on background colors for anything other than the window itself, either.
Is there a different, better framework I should use for this, or are there some functions or items that I've been missing? All help or guidance on the idea would be appreciated...
I'm using MSVC++ to do... everything that I'm working on.
For Listview examples, nothing beats the clarity of the Classic Sample!
In the meantime, Google Translate along with Unicode + tiny modifications to the rescue for @Alejadro's German link for the Listview- there's no direct translation on offer from search results as the page doesn't contain the appropriate meta tag. Snipped a little for brevity:
Subsequent changes of styles
The style of a ListView can be changed after creation. For this the functions GetWindowLong and SetWindowLong are used. About masks different styles can be defined.
For the following sequence, dwView contains the style to use, such as
LVS_REPORT or LVS_ICON
.Control of the ListView control
Generating a list
A list view is created with the CreateWindow function. The window class uses the constant
WC_LISTVIEW
. To do this, the common control header file must be included.In the dialog, it is simply defined in the resource.
If there are unresolved externals, you should check whether the library for the Common Controls (comctl32.lib) is included.
Columns of the ListView
Before something can be inserted in a
REPORT
, the columns must be defined. A column is described by the structureLVCOLUMN
. The following routine creates a column.The columns can be modified by messages to the ListView or by calling macros that will ultimately execute a SendMessage.
Insert a line
An element of the ListView is described by the structure
LVITEMW
(see below). Each element can be represented as anICON
,SMALLICON
,LIST
element or as the left column of aREPORT
line.The mask field determines which elements of the
LVITEMW
structure are really used. Since it often makes sense to keep a pointer to the memory object that holds the data behind the object, the lParam field is useful. In order for this to be used,LVIF_TEXT | LVIF_PARAM
must be set as a mask.The constants of mask and the fields that enable them:
The further columns of a report
The element itself is always left in the report view and is selectable. To fill more columns, a text is added to the item.
The above
Create2ColItem
is best demonstrated by something along the line of the following statements:The structure LVITEMW
The structure
LVITEMW
(in CommCtrl.h) describes an element of the ListView. The most important elements are briefly described here. First the definition:The
LVM_GETITEMW
andLVM_SETITEMW
messages change the attributes of an element. As a parameter, you get a pointer to aLVITEMW
structure next to theHWND
of the ListView, which must be filled in advance.The structural elements in detail:
Mask: Specifies which elements are used. A combination of the following flags is possible:
iItem Index (0-based) of the item to which the structure relates.
iSubItem Index (1-based) of the subitem to which the structure relates. 0 if the structure refers to an item instead of a subitem.
pszText points to a null-terminated string. If the value is
LPWSTR_TEXTCALLBACK
, it is a callback item. If this changes, pszText must be set toLPSTR_TEXTCALLBACK
and the ListView informed byLVM_SETITEMW
orLVM_SETITEMTEXT
.pszText
must not be set toLPWSTR_TEXTCALLBACK
if the ListView has the styleLVS_SORTASCENDING
orLVS_SORTDESCENDING
.cchTextMax Size of the buffer when the text is read.
iImage Index of the icon of this element from the image list.
lParam 32-bit value that is specific to this element.
Actions with elements
Before inserting multiple items, an
LVM_SETITEMCOUNT
message will be sent to the ListView indicating how many items will ultimately be contained. This allows the ListView to optimize its memory allocation and release. How many elements a ListView contains can be determined withLVM_GETITEMCOUNT
.Editing selected elements
Events The ListView sends
WM_NOTIFY
messages to the parent window. The code can take the following values:Editing the labels The list view must have been created using the
LVS_EDITLABELS
style. Then a label can already be clicked on and inputs are accepted. However, the input is discarded immediately afterwards. To allow changes in the label, you just need to catch theWM_NOTIFY
and returnTRUE
. In order to access the entered text in between, access is made to the text of the item. The example shows the input in a message box.If editing was aborted, the
pszText
element will be 0.If you want to prevent editing, the message
LVN_BEGINLABELEDIT
is caught and returnedTRUE
. Here, too, the item can be accessed in the same way vialParam
and thus, for example, a certain item group can be excluded.Click on the column header in the ListView
Selection Event
The
LVN_ITEMACTIVATE
event is sent when the user activates an item. As with the other ListView events, it achieves the window function as part of aWM_NOTIFY
message.The
LVM_GETSELECTEDCOUNT
message can be used to determine how many items have been activated. TheLVM_GETNEXTITEM
message is sent with theLVNI_SELECTED
attribute and all items have been edited.Windows provides a fairly basic collection of built-in controls, listed here.
If you want something more sophisticated your options are:
If you're stuck with VC++, The Grid Control and The Ultimate Grid are MFC-based.
If you're not using MFC there's BABYGRID or The Win32 SDK Data Grid.
If none of them suit, you'll have more luck searching for "grid" than "table".
Using the windows API and the standard control ListView you can do a table using the style LVS_REPORT
documentation link - unfortunatelly with no code :( -
About List-View Controls
I've found this good article Windows Programmierung: List View the explanation is in german language but a google translation together with the code should be enough to understand it. From the article, to create the window:
then it is explained how to create the columns in the method
how to insert an item (one column)
or insert item with two columns
etc...