Header file for Listview: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (titles)
 
(14 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Header file for creating Listview control ==
== 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.
In an NSIS Chinese forum "www.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 later. I add some new macros, include some for ComboBox and ListBox.


See this attachment for details, 8 examples included.
See this attachment for details, 8 examples included.
Line 7: Line 7:
<attach>ListView.zip</attach>
<attach>ListView.zip</attach>


All macros in this header:
=== Additional macros for common windows===


* <highlight-nsis>${NSD_RemoveStyle} hWnd Styles</highlight-nsis>
* <highlight-nsis>${NSD_RemoveStyle} hWnd Styles</highlight-nsis>
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).
Removes one or more window styles of a control. Note, the following text come from MSDN: For some styles in certain controls (For example, add the [http://msdn.microsoft.com/en-us/library/bb775464.aspx ES_READONLY] style to an edit control will not make it read-only), 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.


* <highlight-nsis>${NSD_RemoveExStyle} hWnd ExStyles</highlight-nsis>
* <highlight-nsis>${NSD_RemoveExStyle} hWnd ExStyles</highlight-nsis>
Removes one or more extended window style from a control. May not take effect in some cases for the above saying.
Removes one or more extended window styles of a control. It may not take effect in some cases for the above saying.


* <highlight-nsis>${NSD_CB_DelString} hComboBox string</highlight-nsis>
=== Additional macros for ListBox and ComboBox ===
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.
 
* <highlight-nsis>${NSD_CB_DelString} hComboBox String</highlight-nsis>
Deletes the specified string from the ComboBox control. Note: If the string you specified doesn't exist in the ComboBox, the first string that contains what you specified will be deleted. For example: there are 4 items in the ComboBox: First, Second, Seconds, Secondary. If you want to delete the string "sec", the first one which contains "sec" from top - "Second" will be deleted.


* <highlight-nsis>${NSD_CB_Clear} hComboBox</highlight-nsis>
* <highlight-nsis>${NSD_CB_Clear} hComboBox</highlight-nsis>
Deletes all strings from a combobox (no return value).
Deletes all strings of the ComboBox control (no return value).


* <highlight-nsis>${NSD_CB_GetCount} hComboBox outVar</highlight-nsis>
* <highlight-nsis>${NSD_CB_GetCount} hComboBox outVar</highlight-nsis>
Retrieves the number of strings from a combobox.
Retrieves the count of strings in the ComboBox control.


* <highlight-nsis>${NSD_CB_GetSelection} hComboBox outVar</highlight-nsis>
* <highlight-nsis>${NSD_CB_GetSelection} hComboBox outVar</highlight-nsis>
Retrieves the selected string from a combobox.
Retrieves the selected string of the ComboBox control.
 
* <highlight-nsis>${NSD_LB_DelString} hListBox String #redefined in header</highlight-nsis>
Deletes the specified string of the ListBox control. 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 header. Note: If the string you specified is not exist in the ListBox, the first string that contains what you specified will be deleted.


* <highlight-nsis>${NSD_LB_DelString} hListBox String # the redefined one</highlight-nsis>
* <highlight-nsis>${NSD_LB_Clear} hListBox #redefined in header</highlight-nsis>
Deletes the specified string from a list box.
Deletes all strings of the ListBox control (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 like the above one, it doesn't return any value.
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.


* <highlight-nsis>${NSD_LB_Clear} hListBox # the redefined one</highlight-nsis>
=== New macros for ListView control ===
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.


* <highlight-nsis>${NSD_LV_InsertColumn} hListview iCol cx szText</highlight-nsis>
* <highlight-nsis>${NSD_LV_InsertColumn} hListview iCol cx szText</highlight-nsis>
Inserts a new column to a listview control (report view). It is very easy to delete a column from a listview, just sending the LVM_DELETECOLUMN message to the listview - "SendMessage hListview ${LVM_DELETECOLUMN} iCol 0".
Inserts a new column to the ListView control. To delete a column from the ListView control, just send the [http://msdn.microsoft.com/en-us/library/bb774894.aspx LVM_DELETECOLUMN] message: SendMessage hListview ${LVM_DELETECOLUMN} iCol 0.


* <highlight-nsis>${NSD_LV_DeleteColumn} hListview iCol</highlight-nsis>
* <highlight-nsis>${NSD_LV_DeleteColumn} hListview iCol</highlight-nsis>
Deletes a column from a listview control (report view). Same as LVM_DELETECOLUMN message.
Deletes a column of the ListView control. Same as [http://msdn.microsoft.com/en-us/library/bb774894.aspx LVM_DELETECOLUMN] message.


* <highlight-nsis>${NSD_LV_InsertItem} hListview iItem szText</highlight-nsis>
* <highlight-nsis>${NSD_LV_InsertItem} hListview iItem szText</highlight-nsis>
Inserts a new item to a listview control (report view). To delete a column from a listview, just sending the LVM_DELETECOLUMN message to the listview - "SendMessage listview_HWND ${LVM_DELETEITEM} iItem 0".
Inserts a new item to the ListView control. To delete an item from the ListView control, just send the [http://msdn.microsoft.com/en-us/library/bb774895.aspx LVM_DELETEITEM] message: SendMessage hListview ${LVM_DELETEITEM} iItem 0.


* <highlight-nsis>${NSD_LV_DeleteItem} hListview iItem</highlight-nsis>
* <highlight-nsis>${NSD_LV_DeleteItem} hListview iItem</highlight-nsis>
Deletes an item from a listview control (report view). Same as LVM_DELETEITEM message.
Deletes an item of the ListView control. Same as [http://msdn.microsoft.com/en-us/library/bb774895.aspx LVM_DELETEITEM] message.


* <highlight-nsis>${NSD_LV_GetItemText} hListview iItem iSubItem outVar</highlight-nsis>
* <highlight-nsis>${NSD_LV_GetItemText} hListview iItem iSubItem outVar</highlight-nsis>
Retrieves the text of an item or its subitem from an item of listview control (report view). To retrieve the item's text, pass iSubItem with zero.
Retrieves the text of an item or its subitem of the ListView control. To retrieve an item's text, pass iSubItem with 0 (zero).


* <highlight-nsis>${NSD_LV_SetItemText} hListview iItem iSubItem szText</highlight-nsis>
* <highlight-nsis>${NSD_LV_SetItemText} hListview iItem iSubItem szText</highlight-nsis>
Sets the text of an item or its subitem (inserts a new subitem) to an item of listview control (report view). To set the item's text, pass iSubItem with zero.
Sets the text of an item or its subitem (videlicet, inserts a new subitem) of the ListView control. To set the item's text, pass iSubItem with 0 (zero).


* <highlight-nsis>${NSD_LV_SetItemImage} hListview iItem iImage</highlight-nsis>
* <highlight-nsis>${NSD_LV_SetItemImage} hListview iItem iImage</highlight-nsis>
Sets the icon of an item of listview (report view). Before using this you must create an ImageList first that contains one or more bitmaps or icons. See examples in the above attachment for details.
Sets the associated bitmap or icon of an item in the ListView control. Before using this you must create an ImageList first that contains one or more bitmaps or icons, and send [http://msdn.microsoft.com/en-us/library/bb761178.aspx LVM_SETIMAGELIST] message to associate the ImageList with the ListView control. Then you should pass the zero-based index of the image in the ImageList to "iImage". See examples in the above attachment for details.


* <highlight-nsis>${NSD_LV_GetCheckState} hListview iItem outVar</highlight-nsis>
* <highlight-nsis>${NSD_LV_GetCheckState} hListview iItem outVar</highlight-nsis>
Retrieves the state of an item in a listview control with checkboxes extended style. Result may be 1, represents the item is checked, or 0 represents unchecked. Before using this you must send the LVM_SETEXTENDEDLISTVIEWSTYLE message to set the LVS_EX_CHECKBOXES extended style.
Retrieves the check state of an item in a listview control with checkboxes extended style. Result may be 1, which represents the item is checked, or 0, represents unchecked. Before using this you must send the [http://msdn.microsoft.com/en-us/library/bb761165.aspx LVM_SETEXTENDEDLISTVIEWSTYLE] message to set the [http://msdn.microsoft.com/en-us/library/bb774732.aspx LVS_EX_CHECKBOXES] extended style.


* <highlight-nsis>${NSD_LV_SetCheckState} hListview iItem State</highlight-nsis>
* <highlight-nsis>${NSD_LV_SetCheckState} hListview iItem State</highlight-nsis>
Checks or umchecks an item in a listview control with checkboxes extended style. Pass "State" with 1 to check it or 0 to uncheck it. Before using thsi you must send the LVM_SETEXTENDEDLISTVIEWSTYLE message to set the LVS_EX_CHECKBOXES extended style.
Checks or unchecks an item in a listview control with checkboxes extended style. Pass "State" with 1 (one) to check it or 0 (zero) to uncheck it. Before using this you must send the [http://msdn.microsoft.com/en-us/library/bb761165.aspx LVM_SETEXTENDEDLISTVIEWSTYLE] message to set the [http://msdn.microsoft.com/en-us/library/bb774732.aspx LVS_EX_CHECKBOXES] extended style.


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


For example:
For example:
Line 72: Line 74:
SendMessage $HWND ${LVM_SETBKCOLOR} 0 0xFF0000, sets the background color to red.
SendMessage $HWND ${LVM_SETBKCOLOR} 0 0xFF0000, sets the background color to red.


Parameters of each message can be found on msdn.
All messages of listview control: http://msdn.microsoft.com/en-us/library/ff485961.aspx. You can find the function introduction of each message and their parameters on MSDN.


See all messages of listview control: http://msdn.microsoft.com/en-us/library/ff485961.aspx.
The original author of this header adds more controls support and more macros in this header, therefore it is renamed to "CommCtrl.nsh". As more controls support will be added in future, and this page will also be updated. If you have any suggestions you may contact with gfm688: 395177116#qq.com, or me: 550771955#qq.com.


[[Category:Headers]]
[[Category:Headers]]


== Example 1 ==
== Examples ==
=== Example 1 ===


<highlight-nsis>
<highlight-nsis>
Line 103: Line 106:
     ${NSD_CreateListView} 0u 0u 300u 140u "Listview"
     ${NSD_CreateListView} 0u 0u 300u 140u "Listview"
     Pop $1
     Pop $1
     ; ${NSD_LV_InsertColumn} hwnd col_idx_0_based col_width col_text
     ; ${NSD_LV_InsertColumn} hwnd iCol cx szText
     ${NSD_LV_InsertColumn} $1 0 200 "column 0"
     ${NSD_LV_InsertColumn} $1 0 200 "column 0"
     ${NSD_LV_InsertColumn} $1 1 100 "column 1"
     ${NSD_LV_InsertColumn} $1 1 100 "column 1"
     ${NSD_LV_InsertColumn} $1 2 100 "column 2"
     ${NSD_LV_InsertColumn} $1 2 100 "column 2"
     ; ${NSD_LV_InsertItem} hwnd item_idx_0_based item_text
     ; ${NSD_LV_InsertItem} hwnd iItem szText
     ${NSD_LV_InsertItem} $1 0 'Item 0'
     ${NSD_LV_InsertItem} $1 0 'Item 0'
     ; ${NSD_LV_SetItemText} hwnd item_idx_0_based subitem_idx_1_based subitem_text
     ; ${NSD_LV_SetItemText} hwnd iItem iSubItem szText
     ${NSD_LV_SetItemText} $1 0 1 'Subitem 0_1'
     ${NSD_LV_SetItemText} $1 0 1 'Subitem 0_1'
     ${NSD_LV_SetItemText} $1 0 2 'Subitem 0_2'
     ${NSD_LV_SetItemText} $1 0 2 'Subitem 0_2'
Line 128: Line 131:
[[File:Listview1.png]]
[[File:Listview1.png]]


== Example 2 ==
=== Example 2 ===


<highlight-nsis>
<highlight-nsis>
Line 161: Line 164:
     System::Call 'kernel32::LoadLibrary(t"shell32.dll")i.r0'
     System::Call 'kernel32::LoadLibrary(t"shell32.dll")i.r0'
     ; Load the 9th icon of small size in shell32.dll.
     ; Load the 9th icon of small size in shell32.dll.
     System::Call 'user32::LoadImage(ir0,i9,i${IMAGE_ICON},i16,i16,i${LR_SHARED})i.R1'
     System::Call 'user32::LoadImage(ir0,i9,i${IMAGE_ICON},i32,i32,i${LR_SHARED})i.R1'
     ; Create an imagelist, width and height of each image is 16.
     ; Create an imagelist, width and height of each image is 32.
     System::Call 'comctl32::ImageList_Create(i16,i16,i${ILC_MASK}|${ILC_COLOR32},i0,i0)i.R0'
     System::Call 'comctl32::ImageList_Create(i32,i32,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)'

Latest revision as of 11:50, 7 December 2015

Header file for creating Listview control

In an NSIS Chinese forum "www.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 later. I add some new macros, include some for ComboBox and ListBox.

See this attachment for details, 8 examples included.

ListView.zip (328 KB)

Additional macros for common windows

  • ${NSD_RemoveStyle} hWnd Styles

Removes one or more window styles of a control. Note, the following text come from MSDN: For some styles in certain controls (For example, add the ES_READONLY style to an edit control will not make it read-only), 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.

  • ${NSD_RemoveExStyle} hWnd ExStyles

Removes one or more extended window styles of a control. It may not take effect in some cases for the above saying.

Additional macros for ListBox and ComboBox

  • ${NSD_CB_DelString} hComboBox String

Deletes the specified string from the ComboBox control. Note: If the string you specified doesn't exist in the ComboBox, the first string that contains what you specified will be deleted. For example: there are 4 items in the ComboBox: 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} hComboBox

Deletes all strings of the ComboBox control (no return value).

  • ${NSD_CB_GetCount} hComboBox outVar

Retrieves the count of strings in the ComboBox control.

  • ${NSD_CB_GetSelection} hComboBox outVar

Retrieves the selected string of the ComboBox control.

  • ${NSD_LB_DelString} hListBox String #redefined in header

Deletes the specified string of the ListBox control. 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 header. Note: If the string you specified is not exist in the ListBox, the first string that contains what you specified will be deleted.

  • ${NSD_LB_Clear} hListBox #redefined in header

Deletes all strings of the ListBox control (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 like the above one, it doesn't return any value.

New macros for ListView control

  • ${NSD_LV_InsertColumn} hListview iCol cx szText

Inserts a new column to the ListView control. To delete a column from the ListView control, just send the LVM_DELETECOLUMN message: SendMessage hListview ${LVM_DELETECOLUMN} iCol 0.

  • ${NSD_LV_DeleteColumn} hListview iCol

Deletes a column of the ListView control. Same as LVM_DELETECOLUMN message.

  • ${NSD_LV_InsertItem} hListview iItem szText

Inserts a new item to the ListView control. To delete an item from the ListView control, just send the LVM_DELETEITEM message: SendMessage hListview ${LVM_DELETEITEM} iItem 0.

  • ${NSD_LV_DeleteItem} hListview iItem

Deletes an item of the ListView control. Same as LVM_DELETEITEM message.

  • ${NSD_LV_GetItemText} hListview iItem iSubItem outVar

Retrieves the text of an item or its subitem of the ListView control. To retrieve an item's text, pass iSubItem with 0 (zero).

  • ${NSD_LV_SetItemText} hListview iItem iSubItem szText

Sets the text of an item or its subitem (videlicet, inserts a new subitem) of the ListView control. To set the item's text, pass iSubItem with 0 (zero).

  • ${NSD_LV_SetItemImage} hListview iItem iImage

Sets the associated bitmap or icon of an item in the ListView control. Before using this you must create an ImageList first that contains one or more bitmaps or icons, and send LVM_SETIMAGELIST message to associate the ImageList with the ListView control. Then you should pass the zero-based index of the image in the ImageList to "iImage". See examples in the above attachment for details.

  • ${NSD_LV_GetCheckState} hListview iItem outVar

Retrieves the check state of an item in a listview control with checkboxes extended style. Result may be 1, which represents the item is checked, or 0, represents unchecked. Before using this you must send the LVM_SETEXTENDEDLISTVIEWSTYLE message to set the LVS_EX_CHECKBOXES extended style.

  • ${NSD_LV_SetCheckState} hListview iItem State

Checks or unchecks an item in a listview control with checkboxes extended style. Pass "State" with 1 (one) to check it or 0 (zero) to uncheck it. Before using this you must send the LVM_SETEXTENDEDLISTVIEWSTYLE message to set the LVS_EX_CHECKBOXES extended style.

To set the text color, background color, 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.

All messages of listview control: http://msdn.microsoft.com/en-us/library/ff485961.aspx. You can find the function introduction of each message and their parameters on MSDN.

The original author of this header adds more controls support and more macros in this header, therefore it is renamed to "CommCtrl.nsh". As more controls support will be added in future, and this page will also be updated. If you have any suggestions you may contact with gfm688: 395177116#qq.com, or me: 550771955#qq.com.

Examples

Example 1

/* Listview styles are defined in header file */
!include "MUI2.nsh"
!include "CommCtrl.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
 
    StrCmp $0 error 0 +2
    Abort
 
    ${NSD_CreateListView} 0u 0u 300u 140u "Listview"
    Pop $1
    ; ${NSD_LV_InsertColumn} hwnd iCol cx szText
    ${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 iItem szText
    ${NSD_LV_InsertItem} $1 0 'Item 0'
    ; ${NSD_LV_SetItemText} hwnd iItem iSubItem szText
    ${NSD_LV_SetItemText} $1 0 1 'Subitem 0_1'
    ${NSD_LV_SetItemText} $1 0 2 'Subitem 0_2'
    ${NSD_LV_InsertItem} $1 1 'Item 1'
    ${NSD_LV_SetItemText} $1 1 2 'Subitem 1_2'
    ${NSD_LV_InsertItem} $1 2 'Item 2'
    ${NSD_LV_SetItemText} $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 "CommCtrl.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
 
    StrCmp $0 error 0 +2
    Abort
 
    ${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(ir0,i9,i${IMAGE_ICON},i32,i32,i${LR_SHARED})i.R1'
    ; Create an imagelist, width and height of each image is 32.
    System::Call 'comctl32::ImageList_Create(i32,i32,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
    System::Call 'comctl32::ImageList_Destroy(iR0)'
 
FunctionEnd
 
Section -Main
 
SectionEnd

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