如何获得Listview的标题高度 - c#

Bra*_*rad 12 .net listview

sombody可以告诉我如何获得列表视图的标题高度.

谢谢

Coi*_*oin 13

这可能有点hacky但你可以这样做:

listView.Items[0].Bounds.Top
Run Code Online (Sandbox Code Playgroud)

这仅在列表中只有一个项目时才有效.因此,您可能希望在首次创建列表时临时添加一个并保留高度值.

否则,您可以随时使用:

listView.TopItem.Bounds.Top
Run Code Online (Sandbox Code Playgroud)

要随时进行测试,但您仍需要列表中的至少一个项目.


Pha*_*rus 11

以下是使用Win32 Interop调用获取listview标题高度的方法.

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT 
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

const long LVM_FIRST = 0x1000;
const long LVM_GETHEADER = (LVM_FIRST + 31);

[DllImport("user32.dll", EntryPoint="SendMessage")]
private static extern IntPtr SendMessage(IntPtr hwnd, long wMsg, long wParam, long lParam);

[DllImport("user32.dll")]
private static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);

RECT rc = new RECT();
IntPtr hwnd = SendMessage(ListView1.Handle, LVM_GETHEADER, 0, 0);
if (hwnd != null) 
{
    if (GetWindowRect(new HandleRef(null, hwnd), out rc)) 
    {
        int headerHeight = rc.Bottom - rc.Top;
    }
}
Run Code Online (Sandbox Code Playgroud)