Kann mir jemand sagen, wie man die Kopfhöhe einer Listenansicht erhalten.Wie bekomme ich die Kopfhöhe eines Listview - C#
Dank
Kann mir jemand sagen, wie man die Kopfhöhe einer Listenansicht erhalten.Wie bekomme ich die Kopfhöhe eines Listview - C#
Dank
Dies könnte ein wenig hacky, aber Sie tun können:
listView.Items[0].Bounds.Top
Dies funktioniert nur, wenn nur ein Element in der Liste ist. Vielleicht möchten Sie beim Erstellen der Liste vorübergehend einen hinzufügen und den Wert für die Höhe beibehalten.
Else, können Sie immer verwenden:
listView.TopItem.Bounds.Top
Um den Test jederzeit zu machen, aber Sie müssen noch mindestens ein Element in der Liste.
Stellt sich heraus, dass dieser Ansatz nicht ordnungsgemäß funktioniert, wenn das Element aus dem sichtbaren Bereich gescrollt wird. – EricLaw
So erhalten Sie die Höhe der ListView-Header mit Win32 Interop-Aufrufe.
[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;
}
}
Sollen nicht auch wParam und lParam IntPtrs sein, um auf x64 richtig zu funktionieren? Wahrscheinlich spielt es keine Rolle, wie du Nullen passierst, aber irgendjemand wird das kopieren/einfügen, wo es hingehört. – EricLaw
Bestätigte das funktioniert in meinem Win32 ++ Anwendung:
CHeader* hdr = GetHeader();
CRect rcHdr = hdr->GetWindowRect();
Kopfhöhe ist rcHdr.Height()
@Phaedrus
vor langer Zeit ..long .. aber: PInvokeStackImbalance heißt
Die Signatur von Sendmessage ist (long = Uint32!):
LRESULT WINAPI SendMessage(
_In_ HWND hWnd,
_In_ UINT Msg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
wechseln alle zu:
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;
}
Korrekter 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;
}
}
Ist das WinForms, WCF oder ASP.NET ? –