gpt4 book ai didi

c# - 使用 LVM_GETITEMRECT 消息获取桌面图标大小

转载 作者:行者123 更新时间:2023-11-30 16:27:56 29 4
gpt4 key购买 nike

我整晚都在为这个问题而战。我只是不知道哪里出了问题。我正在尝试获取桌面图标 + 标题大小。我有一些函数可以找到桌面句柄并将其放入名为 SysListView32_hwnd 的变量中。我已经用 Windows spy 程序验证了它是正确的。这是我遇到问题的部分。

Rectangle rct = new Rectangle();
IntPtr pRct = Marshal.AllocHGlobal(Marshal.SizeOf(rct));
Marshal.StructureToPtr(rct, pRct, true);

SendMessage(SysListView32_hwnd, LVM_GETITEMRECT, (IntPtr)0, pRct);

Rectangle Rect = (Rectangle)Marshal.PtrToStructure(pRct, typeof(Rectangle));
Marshal.FreeHGlobal(pRct);
Debug.WriteLine(Rect.Height + " " + Rect.Width);

它每次都会让资源管理器崩溃。C# Visual Studio 2010Windows 7 x64,我正在编译为 64 位程序

Here如果需要,是我的完整应用程序

最佳答案

这解决了在对问题进行广泛编辑时提出的其他问题。

It seems to me like we are working with LVITEMs but VirtualAllocEx specifies sizeof(uint).

VirtualAllocEx()dwSize 参数的文档,在 MSDN 上,指出:

The size of the region of memory to allocate, in bytes.

If lpAddress is NULL, the function rounds dwSize up to the next page boundary.

If lpAddress is not NULL, the function allocates all pages that contain one or more bytes in the range from lpAddress to lpAddress+dwSize. This means, for example, that a 2-byte range that straddles a page boundary causes the function to allocate both pages.

换句话说,它分配整个页面。因为一个页面是 4KB,所以你很幸运。您应该确保 dwSize 足够大以容纳您需要的缓冲区。

Why does pszText = (IntPtr)((int)lpBaseAddress + Marshal.SizeOf(typeof(LVITEM)))? Wouldn't that just point to the end of the structure.

是的,它确实指向结构的末尾。重点是 LVITEMpszText 字段必须指向调用者分配的缓冲区。编写此代码的人选择使用结构末尾的备用缓冲区空间。由于该结构声明 pszText 包含 256 个字节,这很容易适合 4KB 页面的剩余部分。所以它有效,但它有点不透明。

关于c# - 使用 LVM_GETITEMRECT 消息获取桌面图标大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7447941/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com