看段简单的代码 解决问题马上 100分

42252343 2004-10-20 09:40:53
Dim strPath As String * 255
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
StrURLFile = Trim(strPath) & "-abc"
Print StrURLFile

按我的想法,打印出来的应该是,桌面路径-abc 这样的,可是实际上打印出来却只有桌面路径

是不是这种情况下不能进行字符串连接
...全文
214 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2004-10-21
  • 打赏
  • 举报
回复
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, ByVal lpszPath As String, ByVal nFolder As Long, ByVal fCreate As Long) As Long
Private Const CSIDL_DESKTOP = &H0
Private Sub Command1_Click()
Dim strPath As String * 255
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_DESKTOP, False '取桌面路径
Mid(strPath, InStr(strPath, Chr(0))) = "-abc"
StrURLFile = Trim(strPath)
Print StrURLFile
End Sub
行云边 2004-10-21
  • 打赏
  • 举报
回复
这样试试

Dim strPath As String * 255
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
StrURLFile = GetValidString(strPath) & "-abc"
Print StrURLFile

Private Function GetValidString(strData As String) As String
Dim nLen As Integer
Dim strRet As String
Dim i As Integer
strRet = ""
nLen = Len(strData)
For i = 1 To nLen
If Asc(Mid(strData, i, 1)) = 0 Then
strRet = Left(strData, i - 1)
Exit For
End If
Next
GetValidString = strRet
End Function
beijiya 2004-10-21
  • 打赏
  • 举报
回复
'试试下面代码
Dim LenI As Integer
Dim strPath As String * 255
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
LenI = InStr(strPath, Chr(0))
StrURLFile = Left(strPath, LenI -1)
StrURLFile = StrURLFile & "-abc"
Print StrURLFile

of123 2004-10-21
  • 打赏
  • 举报
回复
定长字符串中填充的是 Null (chr(0)),它用 Trim 是去不掉的。而一个 ANSI 字符串是以 Null 作为结束符的。系统处理字符串时会忽略内存中 Null 之后的东西。
还想懒够 2004-10-21
  • 打赏
  • 举报
回复
Trim(strPath)无效


应当把strPath后面带了的chr(0)给去掉,而不是除去空格
of123 2004-10-21
  • 打赏
  • 举报
回复
Dim strPath As String

strPath = string(255, " ")
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
StrURLFile = Trim(strPath) & "-abc"
Print StrURLFile

tztz520 2004-10-21
  • 打赏
  • 举报
回复
Dim strPath As String * 255
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
StrURLFile = Replace(Trim(strPath) & "-abc", Chr(0), "")
Print StrURLFile
leolan 2004-10-21
  • 打赏
  • 举报
回复
因為運行以下這句代碼後strPath是一個以空字符Chr$(0)結尾的字符串,而空字符代表一個字符串的結尾,所以當strPath & "-abc"時所得到的還是strPath 原來的值.
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
leongwong 2004-10-21
  • 打赏
  • 举报
回复
拐个弯,试一下!
lxcc 2004-10-20
  • 打赏
  • 举报
回复
Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long

StrURLFile = Left(strResult, lstrlen(strPath)) & "-abc"
zahota 2004-10-20
  • 打赏
  • 举报
回复
改成这样:
Dim strPath As String * 255
SHGetSpecialFolderPath Me.hWnd, strPath, CSIDL_COMMON_DESKTOPDIRECTORY, False '取桌面路径
StrURLFile = Replace(strpath,chr(0),"") & "-abc"
Print

trim函数是去除空白字符,对chr(0)没用,在API的字符串是以chr(0)结尾的
helanshan 2004-10-20
  • 打赏
  • 举报
回复
可以这样:
Print StrURLFile & "-abc"
lfshf 2004-10-20
  • 打赏
  • 举报
回复
strPath你没有取出来,还是255个字符(Dim strPath As String * 255),一行打印不了这么多,“abc”在打印区域外了。

这样打印就没错了:
StrURLFile = Trim(strPath)
Print StrURLFile
StrURLFile = "-abc"
Print StrURLFile

另外提示,Trim在这里没有用,不能去除strPath后面的空格,你应该用left函数取出来

yzhouen 2004-10-20
  • 打赏
  • 举报
回复
UP!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧