Header file for Listview: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
Line 146: Line 146:
     ${NSD_LV_InsertItem} $1 1 'Item 1'
     ${NSD_LV_InsertItem} $1 1 'Item 1'
     ${NSD_LV_InsertItem} $1 2 'Item 2'
     ${NSD_LV_InsertItem} $1 2 'Item 2'
    ; Retrieve the recommended width and height of an icon.
    System::Call 'user32::GetSystemMetrics(i${SM_CXICON})i.r7'
    System::Call 'user32::GetSystemMetrics(i${SM_CYICON})i.r8'
     ; Retrieve the handle of shell32.dll.
     ; Retrieve the handle of shell32.dll.
     System::Call 'kernel32::GetModuleHandle(t"shell32.dll")i.r0'
     System::Call 'kernel32::LoadLibrary(t"shell32.dll")i.r0'
     ; Load the 9th icon of recommended size in shell32.dll.
     ; Load the 9th icon of small size in shell32.dll.
     System::Call 'user32::LoadImage(i,t,i,i,i,i)i(r0,i9,${IMAGE_ICON},r7,r8,${LR_SHARED})i.R1'
     System::Call 'user32::LoadImage(i,t,i,i,i,i)i(r0,i9,${IMAGE_ICON},r7,r8,${LR_SHARED}).R1'
     ; Free the handle of shell32.dll.
     ; Free the handle of shell32.dll.
     System::Call 'user32::FreeLibrary(ir0)'
     System::Call 'user32::FreeLibrary(ir0)'
     ; Create an imagelist, width of each image is $7 and height is $8.
     ; Create an imagelist, width and height of each image is 16.
     System::Call 'comctl32::ImageList_Create(ir7,ir8,i${ILC_MASK}|${ILC_COLOR32},i0,i0)i.R0'
     System::Call 'comctl32::ImageList_Create(i16,i16,i${ILC_MASK}|${ILC_COLOR32},i0,i0)i.R0'
     ; Add icon to the imagelist by handle.
     ; Add icon to the imagelist by handle.
     System::Call 'comctl32::ImageList_AddIcon(iR0,iR1)'
     System::Call 'comctl32::ImageList_AddIcon(iR0,iR1)'
Line 167: Line 164:


     ; Destroy the image list
     ; Destroy the image list
    ${NSD_FreeImage} $R1
     System::Call 'comctl32::ImageList_Destroy(iR0)'
     System::Call 'comctl32::ImageList_Destroy(iR0)'



Revision as of 11:26, 13 September 2010

Header file for creating Listview control

In an NSIS forum "dreams8.com", a member named "gfm688" writes a header file to create and handle a listview control with the report view. This header file is modified by me, I add some new macro commands, include some macro commands for ComboBox.

See the attachment for details, 8 examples included.

ListView.zip (328 KB)

All macro commands in this file:

  • ${NSD_RemoveStyle} control_HWND style

Removes one or more window style from a control. From MSDN: For some styles in certain controls (the ES_READONLY style in the edit control, for example), it may not properly change the style because the control may need to perform special internal processing. In these cases, a corresponding message to change the style will be available (EM_SETREADONLY in the example mentioned).

  • ${NSD_RemoveExStyle} control_HWND style

Removes one or more extended window style from a control. May not take effect in some cases for the above saying.

  • ${NSD_CB_DelString} combo_HWND string

Deletes the specified string from a combobox. Note: If the string you specified is not exist in a combo box, the first string that contains what you specified will be deleted. For example: there are 4 items in a combo box: First, Second, Seconds, Secondary. If you want to delete the string "sec", the first one which contains "sec" from top - "Second" will be deleted.

  • ${NSD_CB_Clear} combo_HWND

Deletes all strings from a combobox (no return value).

  • ${NSD_CB_GetCount} combo_HWND output_var

Retrieves the number of strings from a combobox.

  • ${NSD_CB_GetSelection} combo_HWND output_var

Retrieves the selected string from a combobox.

  • ${NSD_LB_DelString} listbox_HWND string # the redefined one

Deletes the specified string from a list box. The macro command NSD_LB_DelString in nsDialogs.nsh always deletes the first string but not the specified one. I write a new macro to replace it, and undefine the original one in this file. Note: If the string you specified is not exist in a list box, the first string that contains what you specified will be deleted.

  • ${NSD_LB_Clear} listbox_HWND # the redefined one

Deletes all strings from a list box (no return value). The macro command NSD_LB_Clear in nsDialogs.nsh needs two parameters, but the readme file of nsDialogs said the macro needs only one parameter, it is exactly the handle of the listbox. This macro is redefined as the above one, it doesn't return a value.

  • ${NSD_LV_InsertColumn} listview_HWND col_idx col_width text

Inserts a new column to a listview (report view). It is very easy to delete a column from a listview, just sending the LVM_DELETECOLUMN message to the listview - "SendMessage listview_HWND ${LVM_DELETECOLUMN} col_idx 0".

  • ${NSD_LV_InsertItem} listview_HWND item_idx text

Inserts a new item to a listview (report view). To delete a column from a listview, just sending the LVM_DELETECOLUMN message to the listview - "SendMessage listview_HWND ${LVM_DELETEITEM} item_idx 0".

  • ${NSD_LV_SetSubItem} listview_HWND item_idx subitem_idx text

Inserts a new subitem to an item of listview (report view).

  • ${NSD_LV_SetItemIcon} listview_HWND item_idx icon_idx

Sets the icon of an item of listview (report view). Before using you must first create an image list that contain the handle of one or more bitmap or icon. See examples in the attachment for details.

  • ${NSD_LV_SetItemChecked} listview_HWND item_idx

Checks an item of a listview with checkboxes extended style. Before using you must send the LVM_SETEXTENDEDLISTVIEWSTYLE message to set the LVS_EX_CHECKBOXES extended style take effect.

  • ${NSD_LV_GetItemText} listview_HWND item_idx subitem_idx output_var

Retrieves the text of an item of a listview. To retrieve the item text, set subitem_idx to zero.

It is very easy to set the text color, background color, delete column, delete item, set column width, etc. you can also using SendMessage simplely.

For example:

SendMessage $HWND ${LVM_DELETEITEM} 2 0, deletes the 2nd (zero-based) item.

SendMessage $HWND ${LVM_DELETECOLUMN} 4 0, deletes the 4th (zero-based) column.

SendMessage $HWND ${LVM_SETBKCOLOR} 0 0xFF0000, sets the background color to red.

Parameters of each message can be found on msdn.

See all messages of listview control: http://msdn.microsoft.com/en-us/library/ff485961

Example 1

/* Listview styles are defined in header file */
!include "MUI2.nsh"
!include "ListView.nsh"
 
Name "List View Example"
OutFile ListView.exe
 
Page custom CreatePage
!insertmacro MUI_LANGUAGE "English"
 
Function CreatePage
 
    !insertmacro MUI_HEADER_TEXT "List view" "A listview control created by nsDialogs"
 
    nsDialogs::Create 1018
    Pop $0
    ${If} $0 == error
        Abort
    ${EndIf}
 
    ${NSD_CreateListView} 0u 0u 300u 140u "Listview"
    Pop $1
    ; ${NSD_LV_InsertColumn} hwnd col_idx_0_based col_width col_text
    ${NSD_LV_InsertColumn} $1 0 200 "column 0"
    ${NSD_LV_InsertColumn} $1 1 100 "column 1"
    ${NSD_LV_InsertColumn} $1 2 100 "column 2"
    ; ${NSD_LV_InsertItem} hwnd item_idx_0_based item_text
    ${NSD_LV_InsertItem} $1 0 'Item 0'
    ; ${NSD_LV_SetSubItem} hwnd item_idx_0_based subitem_idx_1_based subitem_text
    ${NSD_LV_SetSubItem} $1 0 1 'Subitem 0_1'
    ${NSD_LV_SetSubItem} $1 0 2 'Subitem 0_2'
    ${NSD_LV_InsertItem} $1 1 'Item 1'
    ${NSD_LV_SetSubItem} $1 1 2 'Subitem 1_2'
    ${NSD_LV_InsertItem} $1 2 'Item 2'
    ${NSD_LV_SetSubItem} $1 2 1 'Subitem 2_1'
 
    nsDialogs::Show

FunctionEnd
 
Section -Main
 
SectionEnd

Compile the script and run, you will see a page like this: Listview1.png

Example 2

/* Listview styles are defined in header file */
!include "MUI2.nsh"
!include "ListView.nsh"
 
Name "List View Example"
OutFile ListView.exe
 
Page custom CreatePage
!insertmacro MUI_LANGUAGE "English"
 
Function CreatePage
 
    !insertmacro MUI_HEADER_TEXT "List view" "Using icon resource from system dll as image list"
 
    nsDialogs::Create 1018
    Pop $0
    ${If} $0 == error
        Abort
    ${EndIf}
 
    ${NSD_CreateListView} 0u 0u 300u 140u "Listview"
    Pop $1
    ${NSD_LV_InsertColumn} $1 0 200 "column 0"
    ${NSD_LV_InsertColumn} $1 1 200 "column 1"
    ${NSD_LV_InsertItem} $1 0 'Item 0'
    ${NSD_LV_InsertItem} $1 1 'Item 1'
    ${NSD_LV_InsertItem} $1 2 'Item 2'
    ; Retrieve the handle of shell32.dll.
    System::Call 'kernel32::LoadLibrary(t"shell32.dll")i.r0'
    ; Load the 9th icon of small size in shell32.dll.
    System::Call 'user32::LoadImage(i,t,i,i,i,i)i(r0,i9,${IMAGE_ICON},r7,r8,${LR_SHARED}).R1'
    ; Free the handle of shell32.dll.
    System::Call 'user32::FreeLibrary(ir0)'
    ; Create an imagelist, width and height of each image is 16.
    System::Call 'comctl32::ImageList_Create(i16,i16,i${ILC_MASK}|${ILC_COLOR32},i0,i0)i.R0'
    ; Add icon to the imagelist by handle.
    System::Call 'comctl32::ImageList_AddIcon(iR0,iR1)'
    System::Call 'user32::DestroyIcon(iR1)'
 
    SendMessage $1 ${LVM_SETIMAGELIST} ${LVSIL_SMALL} $R0
    SendMessage $1 ${LVM_SETEXTENDEDLISTVIEWSTYLE} 0 ${LVS_EX_FULLROWSELECT}
 
    nsDialogs::Show

    ; Destroy the image list
    System::Call 'comctl32::ImageList_Destroy(iR0)'
 
FunctionEnd
 
Section -Main
 
SectionEnd

Compile the script and run, you will see a page like this: Listview2.png