How do I get the header height of a Listview

前端 未结 5 836
太阳男子
太阳男子 2020-12-31 09:58

Can somebody tell me how to get the header height of a ListView.

相关标签:
5条回答
  • 2020-12-31 10:03

    This might be a little bit hacky but you can do:

    listView.Items[0].Bounds.Top
    

    This will only work if there is only one item in the list. So you might want to temporarily add one when you first create the list and keep the height value.

    Else, you can always use:

    listView.TopItem.Bounds.Top
    

    To make the test at any moment, but you still need at least one item in the list.

    0 讨论(0)
  • 2020-12-31 10:08

    Here's how to get the listview header height using Win32 Interop calls.

    [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;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 10:08

    Correct code:

            [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;
            }
        }
    
    0 讨论(0)
  • 2020-12-31 10:09

    @Phaedrus

    ..long long time ago.. but: PInvokeStackImbalance is called

    The signature of SendMessage is (long != Uint32):

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

    Change all to:

    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;
    }
    
    0 讨论(0)
  • 2020-12-31 10:19

    Verified this works in my Win32++ application:

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

    Header height is rcHdr.Height()

    0 讨论(0)
提交回复
热议问题