Change a ListViewControl's column header in real-time
When you're developing a big script for the mass, one day you'll want to add multilingual support. Most of the time, you can change the text of a control this with
PlusWnd::SetControlText, but what if you have a nice ListViewControl with column headers to display interesting information? Formerly, developers simply re-wrote the XML file to make the change in the ListViewControl's <Columns> element, but this method can slow down your computer and is very impractical. Today, I'll show you a much more interesting way to do it.
The following method uses the LVM_SETCOLUMN message to set the column header's information. Not all options of the LVCOLUMN structure need to be used because we only want to change the text. Therefore, we also need to create a buffer to hold the string. The comments will clarify this.
code:
/*
[bool] SetColumnHeader(
[hWnd] hWnd: Handle of the ListViewControl, retrieved with PlusWnd::GetControlHandle
[number] iCol: Column id, the first column is 0, second is 1 etc.
[string] sHeader: The new column header
)
Returns true if LVM_SETCOLUMNW succeeded, otherwise false.
*/
function SetColumnHeader(hWnd, iCol, sHeader) {
//Message constants
var LVM_FIRST = 0x1000;
var LVM_SETCOLUMNW = (LVM_FIRST + 96); //We'll use the Unicode instead of the ANSI version to have more characters supported
var LVCF_TEXT = 0x4;
//Create a buffer for the header
var pszText = Interop.Allocate((sHeader.length+1)*2); //Make a DataBloc where the header fits in perfectly
pszText.WriteString(0, sHeader); //Write the header in the DataBloc
//Create an LVCOLUMN structure
var LVCOLUMN = Interop.Allocate(16);
LVCOLUMN.WriteDWord(0, LVCF_TEXT); //mask: pszText is valid
LVCOLUMN.WriteDWord(12, pszText.DataPtr); //pszText: pointer to our buffer
//Call LVM_SETCOLUMNW to change the header and save the result
var Result = Interop.Call("user32", "SendMessageW", hWnd, LVM_SETCOLUMNW, iCol, LVCOLUMN);
//Clear the DataBlocs
pszText.Size = 0;
LVCOLUMN.Size = 0
//Return the result
return Result;
}
You may use and edit this snippet for your own scripts, but if you're going to release your script it'd be nice if you put a little credit in your script.
EDIT: Snippet now placed in a function for easier access.