Vista has introduced a new API to display a text in the list view control when it doesn’t have any items. As the MSDN library states, I should process the LVN_GETEMPTYMARKUP notification.
In the inherited ListView control the WndProc method is overriden:
protected override void WndProc(ref Message m) {
try {
if(m.Msg == 78 /* WM_NOTIFY */) {
var nmhdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
if(nmhdr.code == -187 /* LVN_GETEMPTYMARKUP */) {
var nmlvemptymarkup =
(NMLVEMPTYMARKUP)Marshal.PtrToStructure(m.LParam, typeof(NMLVEMPTYMARKUP));
nmlvemptymarkup.szMarkup = "The ListView is empty.";
m.Result = (IntPtr)1;
}
}
} finally {
base.WndProc(ref m);
}
}
However, it doesn’t work (although it doesn’t throw any exception). Actually I never get nmhdr.code equals to -187. Any ideas?
I struggled a lot with this one myself.
To get the code in the original question to work, mark the NMLVEMPTYMARKUP struct with [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] (the CharSet.Unicode is important).
Also, after setting the markup-values, call Marshal.StructureToPtr(nmlvemptymarkup, m.LParam, false) to copy the data back to the buffer pointed to by LParam.