初始化定义:
MCIStatusLen = 15
MCIStatus = String(MCIStatusLen + 1, " ")
由于MCIStatus '返回"stopped        ",实际要求返回"stopped",中间多了八个空格,所以if语句Replace(Trim(MCIStatus), " ", "") = "stopped" 判断时为False,我用Len(Replace(Trim(MCIStatus), " ", ""))判断其长度,发现还是15,所以请教大家有什么办法让其返回是True!
源代码参考如下:Private Sub Timer1_Timer()
Dim strTitle As String
Dim hdl As Long
Dim MCIStatusLen As Integer
Dim MCIStatus As String
'*********************************************检测播放状态
If IsMusicOn = True Then
MCIStatusLen = 15
MCIStatus = String(MCIStatusLen + 1, " ")
RetValue = mciSendString("STATUS BackgroundMusic MODE", MCIStatus, MCIStatusLen, 0)
End If
'*********************************************检测结束
   strTitle = Space(765) '定义接收字串
   hdl = GetForegroundWindow ' hwnd is the handle to the foreground window
        GetWindowText hdl, strTitle, 1024
            Debug.Print Left(Replace(strTitle, " ", ""), 9) = "Messenger"  '返回true
            Debug.Print Len(Replace(Trim(MCIStatus), " ", ""))          '返回15
            Debug.Print MCIStatus               '返回"stopped        "
        If Left(Replace(strTitle, " ", ""), 9) = "Messenger" And Replace(Trim(MCIStatus) = "stopped" Then    '如何让     MCIStatus = "stopped"这句返回也是True
            Call Startpaly
        ElseIf Left(Replace(strTitle, " ", ""), 9) <> "Messenger" And S = "playing" Then
            Call Stoppaly
        End If
End Sub

解决方案 »

  1.   

    空白字符并不一定都是空格,也可能是Tab等其他字符.
    可以用下边的方式查看字符串中空白字符ASCII码:
    dim i as integer
    for i=1 to len(mcistatus)
       debug.print asc(mid(MCIStatus,i,1))
    next
      

  2.   

    或者你可以使用Like关键字+模式字符串来进行操作:
    if MCIStatus Like "stopped*" then 
      

  3.   

    Trim(MCIStatus) 返回的不是么,为啥还要REPLACE
      

  4.   

    Replace(Trim(MCIStatus) = "stopped" 
    ==>
    LCase(Trim(Replace(MCIStatus,chr(0),""))="stopped"
      

  5.   

    重要的是那些是不是空格。a = "abc"
    For i = 1 To Len(a)
    MsgBox Mid(a,i,1) & " === " & asc(Mid(a,i,1)) 
    Next 比较一下那些空格是不是32
      

  6.   

    If Left(Replace(strTitle, " ", ""), 9) = "Messenger" And Left(MCIStatus, 7) = "stopped" Then