VBA Cbool是什么意思? (以实际英语)

我似乎无法理解第二行:

If Not CBool(GetKeyState(vbKeyRButton) And &H8000) 

你会善良,请用简单的英语解释一下这是什么意思? 我所能理解的是“如果不是”和“而且”我对这里的所有VBA巫师都有坚定的信念! 请帮帮我!

完整的代码如下:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not CBool(GetKeyState(vbKeyRButton) And &H8000) Then If IsEmpty(strBoardSize) Then Exit Sub End If Else End if Sub end 

strBoardSize是一个表的大小,并在以前

 Dim strBoardSize as string 

根据这个页面 ,CBool​​方法接受任何input,并尝试对该值进行布尔比较。

所以,英文:

 If the value returned by (GetKeyState(vbKeyRButton) And &H8000) is not true, then: If IsEmpty(strBoardSize) Then Exit Sub End If Else End if 

另一种看待这个问题的方法是:

 Dim LCompare as Boolean LCompare = CBool(GetKeyState(vbKeyRButton) And &H8000) 'Sets the value returned from CBool in LCompare If Not LCompare Then If IsEmpty(strBoardSize) Then Exit Sub End If Else End if 

这篇文章解释了GetKeyState的input是什么,以及它们如何影响正在进行的按位运算符。

希望这有助于一点点…

GetKeyState是一个Windows API函数,在https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=vs.85).aspx上描述。 它返回一个整数,如前所述:“如果高位为1,则closures,否则closures。 (这里,感兴趣的键是由常量vbKeyRButton指定的鼠标右键。)

&H8000是一个只有高位设置的整数,所以And操作返回0或者&H8000。 &H8000是高级位掩码(以白话文表示)。

CBool​​函数将0转换为False,将任何非零值转换为True,所以在这里,如果按下键则返回True,否则返回False。

CBool​​字面意思是“转换为布尔值”(这是一个强制转换),因为GetKeyState和&H8000不是types布尔值,它需要转换或转换。

 If Not CBool(GetKeyState(vbKeyRButton) And &H8000) 

所以对于你的代码,这将意味着:

 If vbKeyRButton AND &H8000 are both false then ... 

对于其余的代码,

 If IsEmpty(strBoardSize) 

这将检查stringstrBoardSize是否为空。 所以这是如果strBoardSize是空的去代码的下一行,否则结束if语句,并继续到其他(但你没有什么)