C# 如何获取Listview的标题高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/538906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the header height of a Listview
提问by Brad
Can somebody tell me how to get the header height of a ListView.
有人能告诉我如何获取 ListView 的标题高度吗?
采纳答案by Coincoin
This might be a little bit hacky but you can do:
这可能有点hacky,但你可以这样做:
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.
随时进行测试,但您仍然需要列表中的至少一项。
回答by Phaedrus
Here's how to get the listview header height using Win32 Interop calls.
以下是如何使用 Win32 Interop 调用获取列表视图标头高度。
[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;
}
}
回答by Casey Sheehan
Verified this works in my Win32++ application:
验证这在我的 Win32++ 应用程序中有效:
CHeader* hdr = GetHeader();
CRect rcHdr = hdr->GetWindowRect();
Header height is rcHdr.Height()
标题高度为 rcHdr.Height()
回答by raiserle
@Phaedrus
@Phaedrus
..long long time ago.. but: PInvokeStackImbalanceis called
..很久以前..但是: PInvokeStackImbalance被称为
The signature of SendMessage is (long != Uint32):
SendMessage 的签名是 (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;
}
回答by Martin.Martinsson
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;
}
}