需要 .NET 中 Shell32 的 ExtractAssociatedIcon 函数的工作示例
我需要 Shell32.dll 中的 ExtractAssociatedIcon 函数的工作示例。我无法让它发挥作用,我没有想法。我需要另一双眼睛来看看下面的代码。加载表单时,其图标应设置为 Visual Studio 图标,但我得到的只是默认的系统图标。
Imports System.Runtime.InteropServices
Public Class Form1
Public Function ExtractIcon(ByVal path As String, ByVal handle As IntPtr) As Icon
Dim oResult As Icon
Dim hIcon As IntPtr
Dim iIndex As Integer
Dim oPath As New System.Text.StringBuilder(260, 260)
oPath.Append(path)
hIcon = ExtractAssociatedIcon(handle, oPath, iIndex)
'hIcon = ExtractAssociatedIcon(handle, path, iIndex)
Dim oIcon As Icon = Icon.FromHandle(hIcon)
oResult = DirectCast(oIcon.Clone, Icon)
DestroyIcon(hIcon)
Return oResult
End Function
Public Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
ByVal hInst As IntPtr, _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As System.Text.StringBuilder, _
ByRef lpiIcon As Integer) As IntPtr
'Public Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
' ByVal hInst As IntPtr, _
' <MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As String, _
' ByRef lpiIcon As Integer) As IntPtr
Friend Declare Auto Function DestroyIcon Lib "user32" (<[In]()> ByVal hIcon As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'ExtractAssociatedIcon uses ExtractAssociatedIcon that is in Shell32.dll.
'This works, so why doesn't mine? What am I missing?
'Me.Icon = System.Drawing.Icon.ExtractAssociatedIcon("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe")
Me.Icon = ExtractIcon("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe", Me.Handle)
End Sub
End Class
更正了
问题。它应该是 LPTStr
,而不是 LPStr
。
I need a working example of the ExtractAssociatedIcon function in Shell32.dll. I cannot get it working and I am out of ideas. I need another set of eyes on the following code. When the form loads, its icon should be set to the Visual Studio icon, but all I get is the default system icon.
Imports System.Runtime.InteropServices
Public Class Form1
Public Function ExtractIcon(ByVal path As String, ByVal handle As IntPtr) As Icon
Dim oResult As Icon
Dim hIcon As IntPtr
Dim iIndex As Integer
Dim oPath As New System.Text.StringBuilder(260, 260)
oPath.Append(path)
hIcon = ExtractAssociatedIcon(handle, oPath, iIndex)
'hIcon = ExtractAssociatedIcon(handle, path, iIndex)
Dim oIcon As Icon = Icon.FromHandle(hIcon)
oResult = DirectCast(oIcon.Clone, Icon)
DestroyIcon(hIcon)
Return oResult
End Function
Public Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
ByVal hInst As IntPtr, _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As System.Text.StringBuilder, _
ByRef lpiIcon As Integer) As IntPtr
'Public Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
' ByVal hInst As IntPtr, _
' <MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As String, _
' ByRef lpiIcon As Integer) As IntPtr
Friend Declare Auto Function DestroyIcon Lib "user32" (<[In]()> ByVal hIcon As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'ExtractAssociatedIcon uses ExtractAssociatedIcon that is in Shell32.dll.
'This works, so why doesn't mine? What am I missing?
'Me.Icon = System.Drawing.Icon.ExtractAssociatedIcon("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe")
Me.Icon = ExtractIcon("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe", Me.Handle)
End Sub
End Class
Corrected
<MarshalAs(UnmanagedType.LPStr)>
was the problem. It should have been LPTStr
, not LPStr
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不简单地使用 .NET 本机
Icon.ExtractAssociatedIcon
方法,它似乎可以做同样的事情?附:
我尝试了你的代码,结果与你描述的相同,错误的结果。上面的代码片段似乎可以工作。
Why not simply use the .NET native
Icon.ExtractAssociatedIcon
method, which seems to do the same thing?P.S.:
I tried your code, with the same, wrong result that you described. The above code snippet seems to work though.
传入句柄肯定是错误的,EAI 的第一个参数是模块句柄,而不是图标句柄。这很有效:
如果模块句柄为空,则不需要 StringBuilder。如果您没有“index”参数,请不要使用此代码。 Icon.ExtractAssocationIcon 也能正常工作。
Passing in the handle is definitely wrong, the 1st argument to EAI is a module handle, not an icon handle. This worked well:
No need for StringBuilder if the module handle is null. Don't use this code if you don't the "index" argument. Icon.ExtractAssocationIcon will work just as well.
尝试 stakx 提到的原生 .NET 方法 - 我很幸运地保存了 ICO 文件(如果你保存为另一种格式,比如位图,它通常可以工作)
对于 pinvoke 这样的东西应该可以工作:(警告,未测试)
引用: http:// Social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ecb6137c-8bda-4468-b5e0-359caeb202b1
Try the native .NET method stakx mentions - I've had back luck with it saving the ICO file though (if you save as another format, say bitmap, it normally works though)
For a pinvoke somethign like this should work: (caveat, not tested)
referenced: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ecb6137c-8bda-4468-b5e0-359caeb202b1