2009-02-11 14 views

cevap

13

Bu biraz hacky olabilir ama yapabileceğiniz: Listede yalnızca bir öğe varsa

listView.Items[0].Bounds.Top 

Bu yalnızca çalışacaktır. Bu nedenle, listeyi ilk oluşturduğunuzda ve yükseklik değerini sakladığınızda geçici olarak bir tane eklemek isteyebilirsiniz.

listView.TopItem.Bounds.Top 

her an testi yapmak için, ama yine de listede en az bir öğe gerekir: Else

, her zaman kullanabilirsiniz.

+1

Öğe görünür alanın dışına kaydırılırsa, bu yaklaşımın düzgün şekilde çalışmayacağı ortaya çıkar. – EricLaw

11

Win32 Interop çağrılarını kullanarak liste görünümü üstbilgi yüksekliğini nasıl edinebilirsiniz.

[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

wParam ve lParam burada da x64'te düzgün çalışacak IntPtr'ler olmalı mı? Muhtemelen sıfırları geçerken önemli değil, ama birisi bunu bir yere kopyalayıp yapıştıracaktır. – EricLaw

0

bu benim Win32'nin ++ uygulamasında çalışır Doğrulanmış:

2

@Phaedrus

uzun zaman önce ..long .. ama

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

Başlık yüksekliği rcHdr.Height olan(): PInvokeStackImbalance

.210

SendMessage imzası (uzun = uint32!) 'Dir:

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

Değişim herkese:

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

Doğru kodu:

 [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; 
     } 
    } 
İlgili konular