Popular Post LarsJ Posted November 15, 2015 Popular Post Share Posted November 15, 2015 (edited) From time to time questions arise about listviews with multi-line headers or headers with increased height. Eg. these two questions: Multi-line listview column headings and Listview header height. To use a multi-line header in a listview the height of the header must be increased to make room for multiple lines, and it must be possible to display multiple text lines in the header items. Increase height of header control Because the header control is contained in a listview, the height of the header must be increased in such a way that the listview will be aware of the increased height. An easy and common method of increasing the height is to define a text font for the header with a suitable height. Increasing the height in this way requires that the header is not provided with any themes. However, it is possible to restore themes after the height is increased. A better but slightly more cumbersome method is based on HDM_LAYOUT messages. A HDM_LAYOUT message (implemented in _GUICtrlHeader_Layout) is used to retrieve information about the size of a header control. To use this message to define the height, the header control must be subclassed. The HDM_LAYOUT method is preferred in the examples. Two examples with the font height method are added in "Using font height" folder. Whichever method is chosen, the header height must be increased before rows are added to the listview. Subclassing controls After Windows XP the recommended way to subclass controls is to use the four functions SetWindowSubclass, GetWindowSubclass, RemoveWindowSubclass and DefSubclassProc (all implemented in WinAPIShellEx.au3). These functions are used in the examples. And they are much easier to use than the old SetWindowLong (_WinAPI_SetWindowLong) function. Since we are subclassing a header control contained in a listview this issue must be taking into account. I have verified that this is also an issue for _WinAPI_DefSubclassProc. The solution is the same as the solution in the link: ;Return _WinAPI_DefSubclassProc( ... ) ; Not so good Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", ... )[0] ; Much better Responding to HDM_LAYOUT messages HDM_LAYOUT messages are generated when columns are added to the listview with _GUICtrlListView_AddColumn. To respond to these messages the header control must be subclassed before columns are added: Local $pHeaderProc = DllCallbackGetPtr( DllCallbackRegister( "HeaderProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) ) _WinAPI_SetWindowSubclass( $hHeader, $pHeaderProc, 9999, $iHeaderHeight ) ; SubclassId = 9999, $pData = $iHeaderHeight If columns are added directly in GUICtrlCreateListView command, this code snippet (copied from _GUICtrlHeader_Create) can be used to generate a HDM_LAYOUT message: Local $tRect = _WinAPI_GetClientRect( $hListView ) Local $tWindowPos = _GUICtrlHeader_Layout( $hHeader, $tRect ) _WinAPI_SetWindowPos( $hHeader, DllStructGetData( $tWindowPos , "InsertAfter" ), _ DllStructGetData( $tWindowPos , "X" ), DllStructGetData( $tWindowPos , "Y" ), _ DllStructGetData( $tWindowPos , "CX" ), DllStructGetData( $tWindowPos , "CY" ), _ DllStructGetData( $tWindowPos , "Flags" ) ) This is the subclass callback function: Func HeaderProc( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $iHeaderHeight ) #forceref $iSubclassId Switch $iMsg Case $HDM_LAYOUT Local $tHdLayout = DllStructCreate( $tagHDLAYOUT, $lParam ) Local $tRect = DllStructCreate( $tagRECT, DllStructGetData( $tHdLayout, "Rect" ) ) Local $tWindowPos = DllStructCreate( $tagWINDOWPOS, DllStructGetData( $tHdLayout, "WindowPos" ) ) ; Modify $tRect and $tWindowPos in $tHdLayout to increase Header height DllStructSetData( $tRect, "Top", $iHeaderHeight ) DllStructSetData( $tWindowPos, "X", DllStructGetData( $tRect, "Left" ) ) DllStructSetData( $tWindowPos, "Y", 0 ) DllStructSetData( $tWindowPos, "CX", DllStructGetData( $tRect, "Right" ) - DllStructGetData( $tRect, "Left" ) ) DllStructSetData( $tWindowPos, "CY", $iHeaderHeight ) DllStructSetData( $tWindowPos, "Flags", 0x0020 ) ; 0x0020 = $SWP_FRAMECHANGED Return True EndSwitch ; Call next function in subclass chain Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0] EndFunc Multiple text lines in header items The items in a standard header control can display a single line of text. There seems not to be any options to change this. There is no word wrap option. Fortunately the header control supports both custom and owner drawn items. Custom drawn items are implemented through NM_CUSTOMDRAW notifications included in WM_NOTIFY messages. NM_CUSTOMDRAW notifications are generated automatically by the code in ComCtl32.dll when the header control is updated. Implementing custom drawn items is a matter of responding to these messages or not. Owner drawn items are implemented through WM_DRAWITEM messages. The HDF_OWNERDRAW flag must be set for the header items to generate WM_DRAWITEM messages. Custom drawn items are preferred in the examples. An example with owner drawn items is added in "Owner drawn" folder. Custom drawn items WM_NOTIFY (and WM_DRAWITEM) messages are send to the parent of the header control. The parent is usually an AutoIt GUI, and messages can be handled by a function registered with GUIRegisterMsg. In this case the listview is the parent of the header control. To catch header messages from the listview, the listview must be subclassed. Implement subclassing for the listview: Local $pListViewProc = DllCallbackGetPtr( DllCallbackRegister( "ListViewProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) ) _WinAPI_SetWindowSubclass( $hListView, $pListViewProc, 9999, 0 ) The subclass callback function looks like this: expandcollapse popupFunc ListViewProc( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData ) #forceref $iSubclassId, $pData Switch $iMsg Case $WM_NOTIFY Local $tNMHDR = DllStructCreate( $tagNMHDR, $lParam ) Local $hWndFrom = HWnd( DllStructGetData( $tNMHDR, "hWndFrom" ) ) Local $iCode = DllStructGetData( $tNMHDR, "Code" ) Switch $hWndFrom Case $hHeader Switch $iCode Case $NM_CUSTOMDRAW Local $tNMCustomDraw = DllStructCreate( $tagNMCUSTOMDRAW, $lParam ) Local $dwDrawStage = DllStructGetData( $tNMCustomDraw, "dwDrawStage" ) Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background) Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts) Local $iIndex = DllStructGetData( $tNMCustomDraw, "dwItemSpec" ) ; Item index Local $hDC = DllStructGetData( $tNMCustomDraw, "hdc" ) ; Device context _WinAPI_SetBkMode( $hDC, $TRANSPARENT ) ; Transparent background DllStructSetData( $tNMCustomDraw, "Left", DllStructGetData( $tNMCustomDraw, "Left" ) + $aHdrInfo[$iIndex][1] ) ; Left margin DllStructSetData( $tNMCustomDraw, "Right", DllStructGetData( $tNMCustomDraw, "Right" ) - $aHdrInfo[$iIndex][1] ) ; Right margin DllStructSetData( $tNMCustomDraw, "Top", DllStructGetData( $tNMCustomDraw, "Top" ) + 2 ) ; 2 pixel top margin DllStructSetData( $tNMCustomDraw, "Bottom", DllStructGetData( $tNMCustomDraw, "Bottom" ) - 4 ) ; 4 pixel bottom margin DllCall( "user32.dll", "int", "DrawTextW", "handle", $hDC, "wstr", $aHdrInfo[$iIndex][0], "int", StringLen( $aHdrInfo[$iIndex][0] ), "struct*", DllStructGetPtr( $tNMCustomDraw, "Left" ), "uint", $aHdrInfo[$iIndex][2] ) ; _WinAPI_DrawText Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch EndSwitch ; Call next function in subclass chain Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0] EndFunc Default code in the $CDDS_ITEMPREPAINT stage draws the item frames and background. Code is added to the $CDDS_ITEMPOSTPAINT stage to draw the multi-line item texts. Since we are forced to use custom drawn (or owner drawn) items in any case, we might as well take the opportunity to use some of the options which custom drawn (or owner drawn) items offers. This includes various text styles and fonts, colored text and background, item icons or bitmaps and item frames. The picture shows some of the possibilities: Examples The examples are divided into four folders: Custom drawn contains the examples based on custom drawing. In these examples there is only added code to the $CDDS_ITEMPOSTPAINT stage to fill the inside of the item frames. Owner drawn folder contains an example that shows how to use an owner drawn header control. Two examples in Using font height shows how to use a font to increase the height of the header. Themes are disabled for the header control while the height is increased. Items are custom drawn. The first example restores the theme and draws multi-line texts in the $CDDS_ITEMPOSTPAINT stage. The second example draws entire header items including frames in the $CDDS_ITEMPREPAINT stage. Frames are drawn with _WinAPI_DrawFrameControl. Two examples in Using HDM_LAYOUT shows how to use HDM_LAYOUT messages to increase the height of the header when columns are added with _GUICtrlListView_AddColumn and directly with GUICtrlCreateListView. In all examples LVS_EX_HEADERDRAGDROP extended style is defined for the listview to be able to rearrange columns by drag-and-drop of header items. ListviewHeader.7z Custom drawn\ 1) Simple multi-line header.au3 2) Simple formatted text.au3 3) Styles, fonts, colors.au3 4) Icons and bitmaps.au3 Pattern.bmp Owner drawn\ 1) Simple multi-line header.au3 DrawItem.au3 Using font height\ 1) Restore header theme.au3 2) Drawing everything.au3 Using HDM_LAYOUT\ 1) Columns added with _GUICtrlListView_AddColumn.au3 2) Columns added with GUICtrlCreateListView.au3 ListviewHeader.7z Update 2015-11-24 I have made some tests with real listviews with more than 10 rows. It turns out that the method for increasing the height of the header has a significant impact on performance when rows are inserted in the listview. Increasing the height by responding to HDM_LAYOUT messages is a slow method. Each row inserted generates a HDM_LAYOUT message. This makes insertion slow if there are many rows. (HDM_LAYOUT method can still be used. But it should not be used when the header is contained in a listview.) Using a font to increase the height of the header does not have this negative effect on performance. The purpose of the examples is to be able to use multi-line headers in real listviews. I have updated the examples to use the faster font method to increase the height. A small UDF (GuiHeaderEx.au3) with one function is added to the zip: #include-once #include <WinAPITheme.au3> Func _GUICtrlHeader_SetItemHeightByFont( $hHeader, $iHeight, $bRestoreTheme = True ) ; Remove Header theme _WinAPI_SetWindowTheme( $hHeader, "", "" ) ; Get font of Header control ; Copied from _GUICtrlGetFont example by KaFu ; See https://www.autoitscript.com/forum/index.php?showtopic=124526 Local $hDC = _WinAPI_GetDC( $hHeader ), $hFont = _SendMessage( $hHeader, $WM_GETFONT ) Local $hObject = _WinAPI_SelectObject( $hDC, $hFont ), $lvLogFont = DllStructCreate( $tagLOGFONT ) _WinAPI_GetObject( $hFont, DllStructGetSize( $lvLogFont ), DllStructGetPtr( $lvLogFont ) ) Local $hHdrfont = _WinAPI_CreateFontIndirect( $lvLogFont ) ; Original Header font _WinAPI_SelectObject( $hDC, $hObject ) _WinAPI_ReleaseDC( $hHeader, $hDC ) ; Set height of Header items by applying text font with suitable height $hFont = _WinAPI_CreateFont( $iHeight, 0 ) _WinAPI_SetFont( $hHeader, $hFont ) _WinAPI_DeleteObject( $hFont ) ; Restore Header theme If $bRestoreTheme Then _ _WinAPI_SetWindowTheme( $hHeader ) ; Return original Header font Return $hHdrfont EndFunc ListviewHeader 2015-11-24.7z Custom drawn\ 1) Simple multi-line header.au3 2) Simple formatted text.au3 3) Styles, fonts, colors.au3 4) Icons and bitmaps.au3 5) Drawing everything.au3 GuiHeaderEx.au3 Pattern.bmp Owner drawn\ 1) Simple multi-line header.au3 GuiHeaderEx.au3 DrawItem.au3 You need AutoIt 3.3.10 or later. Tested on Windows 7 32/64 bit and Windows XP 32 bit. Comments are welcome. Let me know if there are any issues. (Set tab width = 2 in SciTE to line up comments by column) ListviewHeader 2015-11-24.7z Edited September 13, 2017 by LarsJ New image link mLipok, KaFu, BJR and 8 others 9 2 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
JohnOne Posted November 15, 2015 Share Posted November 15, 2015 Superb. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
UEZ Posted November 15, 2015 Share Posted November 15, 2015 Excellent work! Thx for sharing! Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
LarsJ Posted November 16, 2015 Author Share Posted November 16, 2015 Thank you for comments and likes.I have only used GDI functions to create the header items. But you can also use GDIPlus functions if you want something more advanced. As the number of header items are limited, it's possible to add a few more code lines.I spent a bit of time reading documentation to figure out the details. At the same time, I found some answers to other issues also related to listviews. In the coming weeks I'll probably add a few more listview examples. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
LarsJ Posted November 24, 2015 Author Share Posted November 24, 2015 Performance update. See last part of the first post. argumentum 1 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
Zedna Posted December 23, 2015 Share Posted December 23, 2015 Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
mLipok Posted January 1, 2016 Share Posted January 1, 2016 (edited) LarsJ whether it is possible to add ComboBox in Header, and some checking, for which Combo element is choosen ?mLipokEDIT:Also possibility to add ComboBox/Edit in ListView Item would be awesome Feature. Edited January 1, 2016 by mLipok Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24 Link to comment Share on other sites More sharing options...
LarsJ Posted January 8, 2016 Author Share Posted January 8, 2016 I've made a note of this. I'll look at it. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
Gianni Posted July 17, 2016 Share Posted July 17, 2016 Hello I would suggest a change to this "multi-line header" in order to simplify the way of use. In brief, to set the rows of header text, I would use a single string for each column, and each string should also incorporate the styles to be used in a manner similar to the CSS embedding them in braces. (style directive (merely as an example) have been copied from here http://www.w3schools.com/css/css_text.asp ) ie: 'Default font' & @CR & '{font-family: Comic Sans MS;} Comic Sans MS' & @CR & '{font-family: Courier New; color: 0xFF00FF;} Magenta Courier New ' the string above would produce a column header of 3 rows (each row is separated by @cr) and each row will have their own style as indicated by the directives inside the braces. (Even more styles separated by ; for each line are allowed). In this way the whole stuff could also be easly build in a form of UDF. This 'quick' script is more a proof of concept than a finished product, I place it here as a possible suggestion for a future version by mr. @LarsJ expandcollapse popup#include <GUIConstants.au3> #include <FontConstants.au3> #include <WinAPIShellEx.au3> #include <GuiListView.au3> #include "GuiHeaderEx.au3" Opt("MustDeclareVars", 1) Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _ ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct" Global $hHeader Global $aHdrInfo = [ _ ; one element for each column 'Normal style' & @CR & '{font-weight:1000}Bold style' & @CR & '{font-style:1;}Italic style' & @CR & '{text-decoration:1;}Underline style', _ 'Default font' & @CR & '{font-family:Comic Sans MS;}Comic Sans MS' & @CR & '{font-family:Courier New;}Courier New' & @CR & '{font-family:Times New Roman;}Times New Roman', _ 'Default color' & @CR & '{color:0x0000FF;}Red color' & @CR & '{color:0xFF00FF;}Magenta color' & @CR & '{color:0xFF0000;}Blue color' , _ '{background-color:0x00FF00; text-align:$DT_CENTER}Background' & @CR & '{text-align:$DT_CENTER}Green color' & @CR & '{text-align:$DT_CENTER}Line 3' & @CR & '{text-align:$DT_CENTER}Line 4'] Example() Func Example() ; Create GUI GUICreate("Styles, fonts, colors", 420, 320) ; Create ListView Local $idListView = GUICtrlCreateListView("", 10, 10, 400, 300, -1, $WS_EX_CLIENTEDGE + $LVS_EX_FULLROWSELECT + $LVS_EX_HEADERDRAGDROP) Local $hListView = GUICtrlGetHandle($idListView) ; Rearrange columns ; Header control $hHeader = _GUICtrlListView_GetHeader($hListView) Local $iHeaderHeight = 68 ; Header height 68 pixels ; Get the font of the Header control ; Copied from the _GUICtrlGetFont example by KaFu ; See https://www.autoitscript.com/forum/index.php?showtopic=124526 Local $hDC = _WinAPI_GetDC($hHeader), $hFont = _SendMessage($hHeader, $WM_GETFONT), $hObject = _WinAPI_SelectObject($hDC, $hFont) Global $tLogFont = DllStructCreate($tagLOGFONT) ; _WinAPI_GetObject($hFont, DllStructGetSize($tLogFont), DllStructGetPtr($tLogFont)) _WinAPI_SelectObject($hDC, $hObject) _WinAPI_ReleaseDC($hHeader, $hDC) ; Set height of Header items by applying a text font with a suitable height Local $hHdrFont = _GUICtrlHeader_SetItemHeightByFont($hHeader, $iHeaderHeight) ; Add columns to ListView _GUICtrlListView_AddColumn($hListView, "", 99) ; Delete header texts to avoid that they are _GUICtrlListView_AddColumn($hListView, "", 99) ; drawn in $CDDS_ITEMPREPAINT stage below. _GUICtrlListView_AddColumn($hListView, "", 99) _GUICtrlListView_AddColumn($hListView, "", 99) ; Fill ListView For $i = 0 To 9 GUICtrlCreateListViewItem($i & "/1|" & $i & "/2|" & $i & "/3|" & $i & "/4", $idListView) Next ; Register callback function to subclass ListView Local $pListViewProc = DllCallbackGetPtr(DllCallbackRegister("ListViewProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr")) _WinAPI_SetWindowSubclass($hListView, $pListViewProc, 9999, $hHdrFont) ; SubclassId = 9999, $pData = $hHdrFont ; Show GUI GUISetState(@SW_SHOW) ; Message loop While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ; Cleanup _WinAPI_RemoveWindowSubclass($hListView, $pListViewProc, 9999) GUIDelete() EndFunc ;==>Example ; This function receives one string for each column with embedded styles ; for each @cr a new line will be printed on the ListView Header ; for each line the styles are embedded between { and } ; even more than one style per line can be setted, just separe more styles by a ; Func _StyleParser(ByRef $sString) ; one string for each column Local $aStyles Local $aLines = StringSplit($sString, @CR) ; split lines for this column header Local $aStyleInfo[$aLines[0]][2] ; declare array $aStyleInfo For $i = 1 To $aLines[0]; scan all lines ; following regexp found here: http://www.rexegg.com/regex-cookbook.html#captureparen $aStyles = StringRegExp($aLines[$i], "\{([^()]*)\}", 1) ; xtract styles from between { and } If @error Then $aStyles = StringSplit('Style:normal;', '¦', 2) ; if no style use StringSplit just to create the array $aStyles[0] = StringStripWS($aStyles[0], 7) If StringRight($aStyles[0], 1) = ";" Then $aStyles[0] = StringLeft($aStyles[0], StringLen($aStyles[0]) - 1) ; remove last ; $aStyleInfo[$i - 1][0] = StringSplit($aStyles[0], ";", 2) ; array of styles to apply to this line $aStyleInfo[$i - 1][1] = StringMid($aLines[$i], StringInStr($aLines[$i], "}") + 1) ; text for this line Next Return $aStyleInfo ; array of styles in $aStyleInfo[n][0], text in $aStyleInfo[n][1] ( where n is line number of header) EndFunc ;==>_StyleParser ; ListViewProc callback function Func ListViewProc($hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $hHdrFont) #forceref $iSubclassId Switch $iMsg Case $WM_NOTIFY Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam) Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) Local $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hHeader Switch $iCode Case $NM_CUSTOMDRAW Local $tNMCustomDraw = DllStructCreate($tagNMCUSTOMDRAW, $lParam) Local $dwDrawStage = DllStructGetData($tNMCustomDraw, "dwDrawStage") Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background) Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts) Local $iIndex = DllStructGetData($tNMCustomDraw, "dwItemSpec") ; Item index (column number 0 ... up) Local $hDC = DllStructGetData($tNMCustomDraw, "hdc") ; Device context _WinAPI_SelectObject($hDC, $hHdrFont) ; Set text font _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; Transparent background ; --- here we go ------------------------------------------------------------------------------------- Local $aRows, $aStyles, $aSplit, $iAllign = $DT_LEFT, $iNrStls, $logicalFont, $brush Local $sDefFaceName = DllStructGetData($tLogFont, "FaceName") Local $tDefLogFont = _WinAPI_CreateFontIndirect($tLogFont) Local $iDefWeight = DllStructGetData( $tLogFont, "Weight" ) $aRows = _StyleParser($aHdrInfo[$iIndex]) For $iRow = 0 To UBound($aRows) - 1 ; for each line in a column header For $i = 0 To UBound($aRows[$iRow][0]) - 1 ; For all Styles within a single line $aSplit = StringSplit(($aRows[$iRow][0])[$i], ":", $STR_NOCOUNT) ; StyleName:Value pairs Switch StringStripWS($aSplit[0], 8) ; wanted action Case "color" _WinAPI_SetTextColor($hDC, $aSplit[1]) Case "font-family" DllStructSetData($tLogFont, "FaceName", $aSplit[1]) Case "font-style" ; normal, italic DllStructSetData($tLogFont, "Italic", $aSplit[1]) Case "font-weight" ; bold DllStructSetData($tLogFont, "Weight", $aSplit[1]) Case "text-align" $iAllign = Execute($aSplit[1]) Case "text-decoration" ; underline, none DllStructSetData($tLogFont, "Underline", $aSplit[1]) Case "background-color" _WinAPI_SelectObject($hDC, $tDefLogFont) $brush = _WinAPI_CreateSolidBrush($aSplit[1]) _WinAPI_SetBkColor($hDC, $brush) Local $tRECT = DllStructCreate($tagRECT) DllStructSetData($tRECT, 1, DllStructGetData($tNMCustomDraw, 6) + 1) DllStructSetData($tRECT, 2, DllStructGetData($tNMCustomDraw, 7) + 1) DllStructSetData($tRECT, 3, DllStructGetData($tNMCustomDraw, 8) - 2) DllStructSetData($tRECT, 4, DllStructGetData($tNMCustomDraw, 9) - 2) _WinAPI_FillRect($hDC, $tRECT, $brush) Case "normal" ; do nothing EndSwitch Next ; Next Style ; _WinAPI_DrawText $logicalFont = _WinAPI_CreateFontIndirect($tLogFont) _WinAPI_SelectObject($hDC, $logicalFont) DllCall("user32.dll", "int", "DrawTextW", "handle", $hDC, "wstr", $aRows[$iRow][1], "int", StringLen($aRows[$iRow][1]), "struct*", DllStructGetPtr($tNMCustomDraw, "Left"), "uint", $iAllign) DllStructSetData($tNMCustomDraw, "Top", DllStructGetData($tNMCustomDraw, "Top") + Abs(DllStructGetData($tLogFont, "Height")) + 3) ; lowers the upper margin of the header ; DllStructSetData($tNMCustomDraw, "Bottom", DllStructGetData($tNMCustomDraw, "Bottom") + 16) ; reset all Styles back to normal $iAllign = $DT_LEFT _WinAPI_SelectObject($hDC, $tDefLogFont) _WinAPI_SetTextColor($hDC, 0x000000) DllStructSetData($tLogFont, "Weight", $iDefWeight) DllStructSetData($tLogFont, "Underline", False) DllStructSetData( $tLogFont, "Italic", False ) DllStructSetData($tLogFont, "FaceName", $sDefFaceName) Next ; Next row Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch EndSwitch ; Call next function in subclass chain Return DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)[0] EndFunc ;==>ListViewProc Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
LarsJ Posted July 18, 2016 Author Share Posted July 18, 2016 This is also one of the examples to be coded as a UDF to make it easier to use. The standard AutoIt method for transferring information to a UDF function is to use simple parameters. And that's probably the way I'll use. More or less in the same manner as is done in Colors and fonts in ListViews. Once a UDF is provided it should be relatively easy to make a wrapper function to use a formatted string to specify all the attributes for a single header item. I also think that the idea of being able to use eg. a combobox in the header control as proposed by mLipok in post 7 should be implemented. Then for example you can use a specific month instead of a Month column. But right now I've no time for coding. I'm busy upgrading my Windows 7 laptop to Windows 10 and installing a new Windows 7 PC. I think I can finish it the next weekend. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
dmob Posted April 15, 2017 Share Posted April 15, 2017 On 7/18/2016 at 6:17 PM, LarsJ said: I also think that the idea of being able to use eg. a combobox in the header control as proposed by mLipok in post 7 should be implemented. Then for example you can use a specific month instead of a Month column. Eagerly awaiting this functionality, if any development still on-going? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 15, 2017 Moderators Share Posted April 15, 2017 dmob, My GUIListViewEx UDF already has the "combo in a header" functionality. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dmob Posted April 15, 2017 Share Posted April 15, 2017 Ow wow, great news since I am already using that UDF Link to comment Share on other sites More sharing options...
Jess Posted September 18, 2017 Share Posted September 18, 2017 expandcollapse popup#include <GUIConstants.au3> #include <WinAPIShellEx.au3> #include <FontConstants.au3> #include <GuiListView.au3> #include <GuiButton.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include "GuiHeaderEx.au3" Opt( "MustDeclareVars", 1 ) Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _ ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct" Global $hHeader ; Left/right margin Global $iHdrLins = 3 ; Line 1 Line 2 Line 3 | Line height Global $aHdrInfo = [ [ "Column 1", "", "", 4, 16, $DT_CENTER, "Styles", 0, 0, 0], _ ; Item 1 [ "Column 2", "Line 2", "", 4, 16, $DT_CENTER, "Fonts", 0, 0, 0, 0], _ ; Item 2 [ "Column 3", "Line 2", "Line 3", 4, 16, $DT_CENTER, "ForeColors", 0, 0, 0, 0 ], _ ; Item 3 [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 1", "", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ ; Item 4 [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ], _ [ "Column 4", "Line 2", "", 4, 16, $DT_CENTER, "BackColor", 0, 0, 0, 0 ]] Example() Func Example() ; Create GUI GUICreate(" My GUI input acceptfile", 900, 570, -1, -1) GUISetBkColor(0x3e65a0) ; Create ListView Global $idListview = GUICtrlCreateListView("", 2, 185, 900, 330, -1) ;$WS_EX_CLIENTEDGE+$LVS_EX_FULLROWSELECT+$LVS_EX_HEADERDRAGDROP ) _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FLATSB, $LVS_REPORT,$LVS_EX_DOUBLEBUFFER)) Local $hListView = GUICtrlGetHandle( $idListView ) ;GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ; GUISetState(@SW_SHOW) ; Header control $hHeader = _GUICtrlListView_GetHeader( $hListView ) Local $iHeaderHeight = 68 ; Header height 50 pixels Local $hDC = _WinAPI_GetDC( $hHeader ), $hFont = _SendMessage( $hHeader, $WM_GETFONT ), $hObject = _WinAPI_SelectObject( $hDC, $hFont ), $tLogFont = DllStructCreate( $tagLOGFONT ) _WinAPI_GetObject( $hFont, DllStructGetSize( $tLogFont ), DllStructGetPtr( $tLogFont ) ) _WinAPI_SelectObject( $hDC, $hObject ) _WinAPI_ReleaseDC( $hHeader, $hDC ) ; Background color for Header item 4 $aHdrInfo[3][7] = 0x00FF00 ; Green background, BGR $aHdrInfo[3][8] = _WinAPI_CreateSolidBrush( $aHdrInfo[3][7] ) ; Brush with background color ; Set height of Header items by applying a text font with a suitable height Local $hHdrFont = _GUICtrlHeader_SetItemHeightByFont( $hHeader, $iHeaderHeight ) ; Add columns to ListView _GUICtrlListView_AddColumn( $hListView, "", 99 ) ; Delete header texts to avoid that they are _GUICtrlListView_AddColumn( $hListView, "", 99 ) ; drawn in $CDDS_ITEMPREPAINT stage below. _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) _GUICtrlListView_AddColumn( $hListView, "", 99 ) Local $pListViewProc = DllCallbackGetPtr( DllCallbackRegister( "ListViewProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) ) _WinAPI_SetWindowSubclass( $hListView, $pListViewProc, 9999, $hHdrFont ) ; SubclassId = 9999, $pData = $hHdrFont ; Show GUI GUISetState( @SW_SHOW ) ; Message loop While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ; Cleanup _WinAPI_RemoveWindowSubclass( $hListView, $pListViewProc, 9999 ) GUIDelete() EndFunc Func ListViewProc( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $hHdrFont ) #forceref $iSubclassId Switch $iMsg Case $WM_NOTIFY Local $tNMHDR = DllStructCreate( $tagNMHDR, $lParam ) Local $hWndFrom = HWnd( DllStructGetData( $tNMHDR, "hWndFrom" ) ) Local $iCode = DllStructGetData( $tNMHDR, "Code" ) Switch $hWndFrom Case $hHeader Switch $iCode Case $NM_CUSTOMDRAW Local $tNMCustomDraw = DllStructCreate( $tagNMCUSTOMDRAW, $lParam ) Local $dwDrawStage = DllStructGetData( $tNMCustomDraw, "dwDrawStage" ) Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background) Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts) Local $iIndex = DllStructGetData( $tNMCustomDraw, "dwItemSpec" ) ; Item index Local $hDC = DllStructGetData( $tNMCustomDraw, "hdc" ) ; Device context _WinAPI_SelectObject( $hDC, $hHdrFont ) ; Set text font _WinAPI_SetBkMode( $hDC, $TRANSPARENT ) ; Transparent background Switch $aHdrInfo[$iIndex][7] Case "Styles" _WinAPI_SelectObject( $hDC, $aHdrInfo[$iIndex][8] ) Case "Fonts" _WinAPI_SelectObject( $hDC, $aHdrInfo[$iIndex][8] ) Case "ForeColors" _WinAPI_SelectObject( $hDC, $aHdrInfo[0][9] ) ; Bold _WinAPI_SetTextColor( $hDC, $aHdrInfo[$iIndex][8] ) Case "BackColor" _WinAPI_SelectObject( $hDC, $aHdrInfo[0][8] ) _WinAPI_SetBkColor( $hDC, $aHdrInfo[$iIndex][8] ) Local $tRECT = DllStructCreate( $tagRECT ) DllStructSetData( $tRECT, 1, DllStructGetData( $tNMCustomDraw, 6 ) + 1 ) DllStructSetData( $tRECT, 2, DllStructGetData( $tNMCustomDraw, 7 ) + 1 ) DllStructSetData( $tRECT, 3, DllStructGetData( $tNMCustomDraw, 8 ) - 2 ) DllStructSetData( $tRECT, 4, DllStructGetData( $tNMCustomDraw, 9 ) - 2 ) _WinAPI_FillRect( $hDC, $tRECT, $aHdrInfo[$iIndex][8] ) EndSwitch ; First line DllStructSetData( $tNMCustomDraw, "Left", DllStructGetData( $tNMCustomDraw, "Left" ) + $aHdrInfo[$iIndex][3] ) ; Left margin DllStructSetData( $tNMCustomDraw, "Right", DllStructGetData( $tNMCustomDraw, "Right" ) - $aHdrInfo[$iIndex][3] ) ; Right margin DllStructSetData( $tNMCustomDraw, "Top", DllStructGetData( $tNMCustomDraw, "Top" ) + 2 ) ; 2 pixel top margin DllStructSetData( $tNMCustomDraw, "Bottom", DllStructGetData( $tNMCustomDraw, "Top" ) + $aHdrInfo[$iIndex][4] ) ; Line height DllCall( "user32.dll", "int", "DrawTextW", "handle", $hDC, "wstr", $aHdrInfo[$iIndex][0], "int", StringLen( $aHdrInfo[$iIndex][0] ), "struct*", DllStructGetPtr( $tNMCustomDraw, "Left" ), "uint", $aHdrInfo[$iIndex][6] ) ; _WinAPI_DrawText ; Line two, three and four For $i = 1 To 2 Switch $aHdrInfo[$iIndex][6] Case "Styles" _WinAPI_SelectObject( $hDC, $aHdrInfo[$iIndex][8+$i] ) Case "Fonts" _WinAPI_SelectObject( $hDC, $aHdrInfo[$iIndex][8+$i] ) Case "ForeColors" _WinAPI_SetTextColor( $hDC, $aHdrInfo[$iIndex][8+$i] ) EndSwitch DllStructSetData( $tNMCustomDraw, "Top", DllStructGetData( $tNMCustomDraw, "Top" ) + $aHdrInfo[$iIndex][4] ) ; 2 pixel top margin DllStructSetData( $tNMCustomDraw, "Bottom", DllStructGetData( $tNMCustomDraw, "Bottom" ) + $aHdrInfo[$iIndex][4] ) DllCall( "user32.dll", "int", "DrawTextW", "handle", $hDC, "wstr", $aHdrInfo[$iIndex][$i], "int", StringLen( $aHdrInfo[$iIndex][$i] ), "struct*", DllStructGetPtr( $tNMCustomDraw, "Left" ), "uint", $aHdrInfo[$iIndex][5] ) ; _WinAPI_DrawText Next Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch EndSwitch ; Call next function in subclass chain Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0] EndFunc Hi, I am trying to give background color to the headers in the listview. I have posted the code which I tried but its not working. Please review it. thanks. Link to comment Share on other sites More sharing options...
LarsJ Posted September 18, 2017 Author Share Posted September 18, 2017 If you only want background colors you don't need all the code: expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <GUIConstants.au3> #include <FontConstants.au3> #include <WinAPIShellEx.au3> #include <GuiListView.au3> #include "GuiHeaderEx.au3" Opt( "MustDeclareVars", 1 ) Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _ ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct" Global $hHeader Global $hHdrBrush ; Line 4 Global $iHdrLins = 3 ; Line 1 Line 2 Line 3 (Not used) Format Global $aHdrInfo = [ [ "Column 1", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 1 [ "Column 2", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 2 [ "Column 3", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 3 [ "Column 4", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 4 [ "Column 5", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 5 [ "Column 6", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 6 [ "Column 7", "Line 2", "Line 3", "Line 4", $DT_CENTER ], _ ; Item 7 [ "Column 8", "Line 2", "Line 3", "Line 4", $DT_CENTER ] ] ; Item 8 Example() Func Example() ; Create GUI GUICreate( "Example", 840, 420 ) ; Create ListView Local $idListView = GUICtrlCreateListView( "", 10, 10, 820, 400, -1, $WS_EX_CLIENTEDGE+$LVS_EX_FULLROWSELECT+$LVS_EX_HEADERDRAGDROP ) Local $hListView = GUICtrlGetHandle( $idListView ) ; Rearrange columns ; Header control $hHeader = _GUICtrlListView_GetHeader( $hListView ) Local $iHeaderHeight = $iHdrLins * 16 + 4 ; Get the font of the Header control ; Copied from the _GUICtrlGetFont example by KaFu ; See https://www.autoitscript.com/forum/index.php?showtopic=124526 Local $hDC = _WinAPI_GetDC( $hHeader ), $hFont = _SendMessage( $hHeader, $WM_GETFONT ), $hObject = _WinAPI_SelectObject( $hDC, $hFont ), $tLogFont = DllStructCreate( $tagLOGFONT ) _WinAPI_GetObject( $hFont, DllStructGetSize( $tLogFont ), DllStructGetPtr( $tLogFont ) ) _WinAPI_SelectObject( $hDC, $hObject ) _WinAPI_ReleaseDC( $hHeader, $hDC ) ; Header brush $hHdrBrush = _WinAPI_CreateSolidBrush( 0xCCFFFF ) ; Yellow, BGR ; Set height of Header items by applying a text font with a suitable height Local $hHdrFont = _GUICtrlHeader_SetItemHeightByFont( $hHeader, $iHeaderHeight ) ; Add columns to ListView For $i = 0 To 7 _GUICtrlListView_AddColumn( $idListView, "", 99 ) ; Delete header texts to avoid that they are Next ; drawn in $CDDS_ITEMPREPAINT stage below. ; Fill ListView For $i = 0 To 99 GUICtrlCreateListViewItem( $i & "/1|" & $i & "/2|" & $i & "/3|" & $i & "/4|" & _ $i & "/5|" & $i & "/6|" & $i & "/7|" & $i & "/8", $idListView ) Next ; Register callback function to subclass ListView Local $pListViewProc = DllCallbackGetPtr( DllCallbackRegister( "ListViewProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) ) _WinAPI_SetWindowSubclass( $hListView, $pListViewProc, 9999, $hHdrFont ) ; SubclassId = 9999, $pData = $hHdrFont ; Show GUI GUISetState( @SW_SHOW ) ; Message loop While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ; Cleanup _WinAPI_RemoveWindowSubclass( $hListView, $pListViewProc, 9999 ) GUIDelete() EndFunc ; ListViewProc callback function Func ListViewProc( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $hHdrFont ) Switch $iMsg Case $WM_NOTIFY Local $tNMHDR = DllStructCreate( $tagNMHDR, $lParam ) Local $hWndFrom = HWnd( DllStructGetData( $tNMHDR, "hWndFrom" ) ) Local $iCode = DllStructGetData( $tNMHDR, "Code" ) Switch $hWndFrom Case $hHeader Switch $iCode Case $NM_CUSTOMDRAW Local $tNMCustomDraw = DllStructCreate( $tagNMCUSTOMDRAW, $lParam ) Local $dwDrawStage = DllStructGetData( $tNMCustomDraw, "dwDrawStage" ) Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background) Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts) Local $iIndex = DllStructGetData( $tNMCustomDraw, "dwItemSpec" ) ; Item index Local $hDC = DllStructGetData( $tNMCustomDraw, "hdc" ) ; Device context _WinAPI_SelectObject( $hDC, $hHdrFont ) ; Set text font _WinAPI_SetBkMode( $hDC, $TRANSPARENT ) ; Transparent background Local $tRECT = DllStructCreate( $tagRECT ) DllStructSetData( $tRECT, 1, DllStructGetData( $tNMCustomDraw, 6 ) + 1 ) DllStructSetData( $tRECT, 2, DllStructGetData( $tNMCustomDraw, 7 ) + 1 ) DllStructSetData( $tRECT, 3, DllStructGetData( $tNMCustomDraw, 8 ) - 2 ) DllStructSetData( $tRECT, 4, DllStructGetData( $tNMCustomDraw, 9 ) - 2 ) _WinAPI_FillRect( $hDC, $tRECT, $hHdrBrush ) ; Background color ; First line DllStructSetData( $tNMCustomDraw, "Left", DllStructGetData( $tNMCustomDraw, "Left" ) + 4 ) ; Left margin DllStructSetData( $tNMCustomDraw, "Right", DllStructGetData( $tNMCustomDraw, "Right" ) - 4 ) ; Right margin DllStructSetData( $tNMCustomDraw, "Top", DllStructGetData( $tNMCustomDraw, "Top" ) + 2 ) ; 2 pixel top margin DllStructSetData( $tNMCustomDraw, "Bottom", DllStructGetData( $tNMCustomDraw, "Top" ) + 16 ) ; Line height DllCall( "user32.dll", "int", "DrawTextW", "handle", $hDC, "wstr", $aHdrInfo[$iIndex][0], "int", StringLen( $aHdrInfo[$iIndex][0] ), "struct*", DllStructGetPtr( $tNMCustomDraw, "Left" ), "uint", $aHdrInfo[$iIndex][4] ) ; _WinAPI_DrawText ; Line two and three For $i = 1 To 2 DllStructSetData( $tNMCustomDraw, "Top", DllStructGetData( $tNMCustomDraw, "Top" ) + 16 ) DllStructSetData( $tNMCustomDraw, "Bottom", DllStructGetData( $tNMCustomDraw, "Bottom" ) + 16 ) DllCall( "user32.dll", "int", "DrawTextW", "handle", $hDC, "wstr", $aHdrInfo[$iIndex][$i], "int", StringLen( $aHdrInfo[$iIndex][$i] ), "struct*", DllStructGetPtr( $tNMCustomDraw, "Left" ), "uint", $aHdrInfo[$iIndex][4] ) ; _WinAPI_DrawText Next Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch EndSwitch ; Call next function in subclass chain Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0] #forceref $iSubclassId EndFunc Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
Jess Posted September 19, 2017 Share Posted September 19, 2017 Thank you @LarsJ. Link to comment Share on other sites More sharing options...
Jess Posted September 19, 2017 Share Posted September 19, 2017 Why the color code is not giving the same color for which the code is. It is for blue and it shows yellow. And how can I give first half headers one color and second half another color. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now