WM_GETICON 有时不返回图标句柄

2024-03-15

我试图显示所有窗口标题,包括相应的图标,就像 Windows 任务管理器那样。但这仅在一定程度上有效 - 尽管我能够获取窗口的标题栏文本,但该图标并不总是可用。

为了获得图标,我通过了WM_GETICON发消息给SendMessage (source http://cjwdev.wordpress.com/2010/06/04/vb-net-get-window-icons/):

Public Const WM_GETICON As UInteger = &H7F
Public Function GetWindowIcon(ByVal WindowHandle As IntPtr) As Icon
    Dim IconHandle As IntPtr = SendMessage(WindowHandle, WM_GETICON, 0, 0)
    If Not IconHandle = IntPtr.Zero Then
        Return Icon.FromHandle(IconHandle)
    Else
        Return Nothing
    End If
End Function

对于某些窗口,这仅返回正确的图标。对于其他人来说,它会返回Nothing since IconHandle等于0。在 Windows 任务管理器和任务栏上,它们显示得很好。

造成这种情况的原因是什么?我该如何解决这个问题?


使用一些复制粘贴和混乱,我最终得到了以下代码......但它现在适用于所有窗口。

基本上,它尝试WM_GETICON获得一个大图标。如果失败,它会调用GetClassLong,有时包括图标。否则,WM_GETICON用于获取小图标。在前两种情况下,我必须将其转换为Bitmap,将其大小调整为 16x16 (我需要该大小),然后将其转换回Icon.

Public Function GetClassLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
    If IntPtr.Size > 4 Then
        Return GetClassLongPtr64(hWnd, nIndex)
    Else
        Return New IntPtr(GetClassLongPtr32(hWnd, nIndex))
    End If
End Function

<DllImport("user32.dll", EntryPoint:="GetClassLong")> _
Public Function GetClassLongPtr32(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As UInteger
End Function

<DllImport("user32.dll", EntryPoint:="GetClassLongPtr")> _
Public Function GetClassLongPtr64(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
End Function

<DllImport("user32.dll")> _
Public Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Boolean, ByVal lParam As Int32) As Integer
End Function


Public Const WM_GETICON As UInteger = &H7F
Public Function GetWindowIcon(ByVal WindowHandle As IntPtr) As Icon
    Dim IconHandle As IntPtr = SendMessage(WindowHandle, WM_GETICON, 1, 0)
    If Not IconHandle = IntPtr.Zero Then
        Dim _icon = Icon.FromHandle(IconHandle)
        Dim bmp = _icon.ToBitmap
        Dim scale_factor As Single = 16 / _icon.Size.Width

        ' Make a bitmap for the result.
        Dim bm_dest As New Bitmap( _
            CInt(bmp.Width * scale_factor), _
            CInt(bmp.Height * scale_factor))

        ' Make a Graphics object for the result Bitmap.
        Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

        ' Copy the source image into the destination bitmap.
        gr_dest.DrawImage(bmp, 0, 0, _
            bm_dest.Width + 1, _
            bm_dest.Height + 1)

        Return MakeIcon(bm_dest, 16, False)
        'Return Icon.FromHandle(IconHandle)
    Else
        IconHandle = GetClassLongPtr(WindowHandle, -34)
        If Not IconHandle = IntPtr.Zero Then
            Dim _icon = Icon.FromHandle(IconHandle)

            Dim bmp = _icon.ToBitmap
            Dim scale_factor As Single = 16 / _icon.Size.Width

            ' Make a bitmap for the result.
            Dim bm_dest As New Bitmap( _
                CInt(bmp.Width * scale_factor), _
                CInt(bmp.Height * scale_factor))

            ' Make a Graphics object for the result Bitmap.
            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            ' Copy the source image into the destination bitmap.
            gr_dest.DrawImage(bmp, 0, 0, _
                bm_dest.Width + 1, _
                bm_dest.Height + 1)

            Return MakeIcon(bm_dest, 16, False)
        Else
            IconHandle = SendMessage(WindowHandle, WM_GETICON, 1, 0)
            If Not IconHandle = IntPtr.Zero Then
                Dim _icon = Icon.FromHandle(IconHandle)
                Dim bmp = _icon.ToBitmap
                Dim scale_factor As Single = 16 / _icon.Size.Width

                ' Make a bitmap for the result.
                Dim bm_dest As New Bitmap( _
                    CInt(bmp.Width * scale_factor), _
                    CInt(bmp.Height * scale_factor))

                ' Make a Graphics object for the result Bitmap.
                Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

                ' Copy the source image into the destination bitmap.
                gr_dest.DrawImage(bmp, 0, 0, _
                    bm_dest.Width + 1, _
                    bm_dest.Height + 1)

                Return MakeIcon(bm_dest, 16, False)
            Else
                Return Nothing
            End If
        End If
    End If
End Function



''' <summary>
''' Converts an image into an icon.
''' </summary>
''' <param name="img">The image that shall become an icon</param>
''' <param name="size">The width and height of the icon. Standard
''' sizes are 16x16, 32x32, 48x48, 64x64.</param>
''' <param name="keepAspectRatio">Whether the image should be squashed into a
''' square or whether whitespace should be put around it.</param>
''' <returns>An icon!!</returns>
Private Function MakeIcon(ByVal img As Image, ByVal size As Integer, ByVal keepAspectRatio As Boolean) As Icon
    Dim square As New Bitmap(size, size)
    ' create new bitmap
    Dim g As Graphics = Graphics.FromImage(square)
    ' allow drawing to it
    Dim x As Integer, y As Integer, w As Integer, h As Integer
    ' dimensions for new image
    If Not keepAspectRatio OrElse img.Height = img.Width Then
        ' just fill the square
        x = 0
        y = 0
        ' set x and y to 0
        ' set width and height to size
        w = size
        h = size
    Else
        ' work out the aspect ratio
        Dim r As Single = CSng(img.Width) / CSng(img.Height)

        ' set dimensions accordingly to fit inside size^2 square
        If r > 1 Then
            ' w is bigger, so divide h by r
            w = size
            h = CInt(Math.Truncate(CSng(size) / r))
            x = 0
            ' center the image
            y = (size - h) \ 2
        Else
            ' h is bigger, so multiply w by r
            w = CInt(Math.Truncate(CSng(size) * r))
            h = size
            y = 0
            ' center the image
            x = (size - w) \ 2
        End If
    End If

    ' make the image shrink nicely by using HighQualityBicubic mode
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default
    g.DrawImage(img, x, y, w, h)
    ' draw image with specified dimensions
    'g.Flush()
    ' make sure all drawing operations complete before we get the icon
    ' following line would work directly on any image, but then
    ' it wouldn't look as nice.
    Return Icon.FromHandle(square.GetHicon())
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WM_GETICON 有时不返回图标句柄 的相关文章

  • Windows7上python3.5无法安装BeautifulSoup4

    我已经从下载了 beautifulsoup4 4 5 3 tar gzhttps www crummy com software BeautifulSoup bs4 download 4 5 https www crummy com sof
  • 如何从任何进程关闭 Windows 上的套接字(ipv4 和 ipv6)连接?

    如何在 Windows 上关闭 tcp v4 和 tcp v6 连接 我不想终止具有开放连接的整个进程 因为这显然会将其他人踢出该进程 我需要从一个单独的进程执行此操作 因此无法访问套接字句柄等 我正在使用 Windows API 来获取
  • 代码退出-1073741515 (0xc0000135)“未找到依赖的 dll”

    我正在尝试编写一个简单的程序 与 2019 年相比 Windows 10 64 位 调试 gt x64 遵循 将 Visual C 项目配置为面向 64 位平台 1 include
  • 如何在 C++ 中急于提交分配的内存?

    总体情况 带宽 CPU 使用率和 GPU 使用率都极其密集的应用程序需要每秒从一个 GPU 向另一个 GPU 传输约 10 15GB 的数据 它使用 DX11 API 来访问 GPU 因此上传到 GPU 只能在每次上传都需要映射的缓冲区中进
  • python+win32:检测窗口拖动

    有没有办法检测何时使用 python pywin32 在窗口中拖动不属于我的应用程序的窗口 我想对其进行设置 以便当我拖动标题与桌面边缘附近的图案匹配的窗口时 当松开鼠标时它会捕捉到边缘 我可以编写代码 以便在释放鼠标时将所有具有该标题的窗
  • 使用命名互斥体的存在作为指示符是个好主意吗?

    我使用命名互斥体来检测应用程序的其他实例并相应地退出 并发现有两种方法可以执行此操作 创建互斥锁 忽略它是否已经存在的指示 尝试获得它 使用获取成功 失败的事实 创建互斥锁 使用指示是否已经存在 我无法决定是否获取互斥锁 并在退出时释放 一
  • Qt(在 Windows 上)将权限级别设置为“requireAdministrator”

    我正在使用 Qt Creator 并努力制作 exe文件默认以管理员身份运行 在线阅读所有解决方案我试图将这一行放入我的 pro file QMAKE LFLAGS MANIFESTUAC level requireAdministrato
  • 从命令行运行 R 代码 (Windows)

    我在名为 analysis r 的文件中有一些 R 代码 我希望能够从命令行 CMD 运行该文件中的代码 而无需通过 R 终端 并且我还希望能够传递参数并在我的代码中使用这些参数 例如就像下面的伪代码 C gt execute r scri
  • Git 扩展 - 无法在 Windows 上推送到网络驱动器中的 git bare 存储库

    我正在 Windows 上学习 git 我已经安装了 Git 扩展 版本 2 47 3 并使用了它 我在我的 C 单元中创建了一个裸存储库 作为中央存储库 并在硬盘中的其他任何位置创建了个人存储库 我对硬盘中的这两个存储库进行提交 推送和拉
  • 访问图像的 Windows“标签”元数据字段

    我正在尝试进行一些图像处理 所以现在我正在尝试读取图像 exif 数据 有 2 个内置函数可用于读取图像的 exif 数据 问题是我想读取图像标签 exifread and imfinfo这两个函数都不显示图像标签 Is there any
  • 如何使用命令行压缩文件?

    我想使用批处理文件命令 Windows XP 批处理文件 压缩目录 例如 如果我想解压缩一个文件意味着我可以使用jar xf file zip java bat 文件命令 就像我想要一个命令行批处理来压缩目录一样 如果您使用的是 Ubunt
  • Qt 支持 Windows 蓝牙 API 吗?

    谁能告诉我 Qt 是否支持 Windows 蓝牙 API 如果是这样 您能否分享一些有关如何使用它的信息 自上次答复以来 这个问题的答案发生了一些变化 Qt 5 2 版为 Linux BlueZ 和 BlackBerry 设备实现了蓝牙 A
  • 在 Windows 上不使用 OpenSSL 从 pfx 文件或证书存储中提取私钥

    正如标题所示 我想在不使用 OpenSSL 或任何其他第三方工具的情况下导出我的私钥 如果我需要一个 cer文件或 pfx我可以通过 MMC 或 PowerShell 轻松导出这些文件pkiclient但我找不到获取私钥的方法 https
  • 比较数组中的文件、从文本文件中删除行、函数、日志记录

    所以我创建了这两个数组 Approved Shares 和 Current Shares Reads Approvedshare txt and makes the txt file into an array public objFSO
  • 相当于Linux中的导入库

    在 Windows C 中 当您想要链接 DLL 时 您必须提供导入库 但是在 GNU 构建系统中 当您想要链接 so 文件 相当于 dll 时 您就不需要链接 为什么是这样 是否有等效的 Windows 导入库 注意 我不会谈论在 Win
  • 为什么我只能用管理员权限才能导入Python中的某些模块?

    我正在努力解决 Python 2 7 中的一些奇怪问题 我写了一个很长的工具 在其中导入不同的模块 我必须首先使用它安装pip 该工具将在公司内部共享 不同的用户在其特定机器上拥有不同的权限 当另一个用户登录我的计算机 我在那里拥有管理员权
  • SetCurrentDirectoryW 中的错误 206

    在我之后之前不清楚的问题 https stackoverflow com questions 44389617 long path name in setcurrentdirectoryw 我以某种方式能够创建一个具有长路径名的目录 但是
  • 如何查看网络连接状态是否发生变化?

    我正在编写一个应用程序 用于检查计算机是否连接到某个特定网络 并为我们的用户带来一些魔力 该应用程序将在后台运行并执行检查是否用户请求 托盘中的菜单 我还希望应用程序能够自动检查用户是否从有线更改为无线 或者断开连接并连接到新网络 并执行魔
  • VB.NET 中的模块变量何时实例化?

    我想知道在程序的生命周期中 模块中的变量将被初始化 如下例所示 Module Helper Friend m Settings As New UserSettings Sub Foo End Sub Sub Bar End Sub End
  • UDP SocketException - 通常只允许每个套接字地址使用一次

    尽管这里有很多非常相似的问题 但提供的答案都没有帮助我 这让我很难过 我有一个非常大的管理系统 我的任务是为其编写一些 UDP 数据包发送 接收 我已经编写了一个原型 一切都很好 所以我开始将我的代码合并到所述系统中 然而 我现在弹出了一个

随机推荐