0

我需要允许用户选择一个图标,所以我从 shell32 实现了 PickIconDlg 函数。问题是,如果用户选择的路径比我声明的初始路径长,则结果值是用户选择的路径被截断为初始路径的长度。

例如,如果我将初始路径设置为“C:\Windows\System32\shell32.dll”并且用户选择“C:\Users\Public\Documents\TheIcons\Library.dll”,则更新后的字符串值返回为“C:\Users\Public\Documents\TheIc”(即用户选择路径的前 31 个字符,因为初始路径是 31 个字符长)。

我尝试调整传递给 PickIconDlg 的“nMaxFile”值,据我所知,它应该设置路径变量的最大长度。这似乎没有什么不同。

Declare Unicode Function PickIconDlg Lib "Shell32" Alias "PickIconDlg" (ByVal hwndOwner As IntPtr, ByVal lpstrFile As String, ByVal nMaxFile As Integer, ByRef lpdwIconIndex As Integer) As Integer

Public Function GetIconLoc(frmform As Form) As Object()
    Dim iconfile As String = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\shell32.dll"
    Dim iconindex As Integer ' Will store the index of the selected icon

    If PickIconDlg(frmform.Handle, iconfile, 50, iconindex) = 1 Then
        MessageBox.Show(iconfile)
        Return {iconfile, iconindex}
    Else
        Return Nothing
    End If
End Function

我希望字符串变量 iconfile 包含用户选择的完整路径,因为它的长度小于定义的最大 50 个字符。相反,如上所述,仅返回路径的一部分。

4

1 回答 1

0

在原始文件名后附加一个 Null 字符和空格,以创建一个足够大的字符串缓冲区以包含结果。

Dim iconfile As String = _
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll") & _
    vbNullChar & Space(256)

然后在调用函数时传递总长度

PickIconDlg(frmform.Handle, iconfile, Len(iconfile), iconindex)

最后从结果中删除额外的空间

iconfile = Left(iconfile, InStr(iconfile, vbNullChar) - 1)

此外,使用Path.Combine而不是自己连接路径。Path.Combine自动添加缺失或删除多余的反斜杠 ( \)。

于 2019-06-24T15:58:27.830 回答