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

时间:2009-02-11 21:31:20

标签: .net listview

有人可以告诉我如何获得列表视图的标题高度。谢谢

5 个答案:

答案 0 :(得分:13)

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

listView.Items[0].Bounds.Top

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

否则,您可以随时使用:

listView.TopItem.Bounds.Top

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

答案 1 :(得分: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;
    }
}

答案 2 :(得分:2)

@Phaedrus

很久很久以前..但是: PInvokeStackImbalance 被称为

SendMessage的签名是(long!= Uint32):

LRESULT WINAPI SendMessage(
    _In_  HWND hWnd,
    _In_  UINT Msg,
    _In_  WPARAM wParam,
    _In_  LPARAM lParam
)

全部更改为:

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

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

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool GetWindowRect(System.Runtime.InteropServices.HandleRef hwnd, out RECT lpRect);

int youtFuncToGetHeaderHeight()
{
    RECT rc = new RECT();
    IntPtr hwnd = SendMessage((IntPtr)this.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
    if (hwnd != null)
    {
        if (GetWindowRect(new System.Runtime.InteropServices.HandleRef(null, hwnd), out rc))
        {
            int headerHeight = rc.Bottom - rc.Top;
        }
    }
    return -1;
}

答案 3 :(得分:0)

已验证这适用于我的Win32 ++应用程序:

CHeader* hdr = GetHeader();
CRect rcHdr = hdr->GetWindowRect();

标题高度为rcHdr.Height()

答案 4 :(得分:0)

正确的代码:

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

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

    [DllImport("user32.dll", EntryPoint="SendMessage")]
    private static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr 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;
        }
    }