简体   繁体   English

VBA Cbool是什么意思? (以英语为准)

[英]VBA What does Cbool mean? (in ACTUAL English)

I can't seem to understand the second line: 我似乎无法理解第二行:

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

Would you be kind and please explain what this is saying in plain English? 请问您要客气吗,请用朴素的英语解释一下。 All I can understand from this is "If not" and "And" I have strong faith in all the VBA wizards here! 我只能从中了解到“ If not”和“ And”,我对这里的所有VBA向导都充满信心! Please help me! 请帮我!

The full code is as below: 完整的代码如下:

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 is the size of a table and is previously strBoardSize是表的大小,以前是

Dim strBoardSize as string

According to this page , the CBool method takes any input and tries to do a boolean comparison of the value. 根据此页面 ,CBool​​方法接受任何输入并尝试对值进行布尔比较。

So, in English: 因此,用英语:

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

Another way to look at this is: 另一种查看方式是:

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

This other article explains what the inputs for GetKeyState are and how they affect the bitwise operators going on. 另一篇文章解释了GetKeyState的输入是什么以及它们如何影响按位运算符。

Hopefully this helps out a bit... 希望这会有所帮助...

GetKeyState is a Windows API function described at https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=vs.85).aspx . GetKeyState是在https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms646301(v=vs.85).aspx中描述的Windows API函数。 It returns an Integer, and as stated, "If the high-order bit is 1, the key is down; otherwise, it is up." 它返回一个整数,并说:“如果高位为1,则该键按下;否则,它按下。” (Here, the key of interest is the right mouse button, specified by the constant vbKeyRButton.) (此处,感兴趣的键是鼠标右键,由常量vbKeyRButton指定。)

The &H8000 is an Integer with only the high-order bit set, so the And operation returns either 0 or &H8000. &H8000是仅设置了高阶位的整数,因此And操作返回0或&H8000。 &H8000 is a mask (in the vernacular) for the high-order bit. &H8000是高阶位的掩码(在本地)。

The CBool function converts 0 to False and any non-zero value to True, so here, it returns True if the key is pressed and False otherwise. CBool​​函数将0转换为False,将任何非零值转换为True,因此在这里,如果按下该键,它将返回True,否则返回False。

CBool literally means "Convert To Boolean"(It is a cast) because GetKeyState and &H8000 aren't type boolean it needs to be converted or cast. CBool​​的字面意思是“转换为布尔值”(强制转换),因为GetKeyState和&H8000的类型不是布尔值,因此需要转换或强制转换。

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

So for your code it would mean: 因此,对于您的代码而言,这意味着:

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

For the rest of the code, 对于其余的代码,

If IsEmpty(strBoardSize)

This checks if the String strBoardSize is empty. 这将检查String strBoardSize是否为空。 So it's If strBoardSize is empty go to the next line of code, otherwise end the if statement and go on to the else (But you have nothing there) 因此,如果strBoardSize为空,则转到下一行代码,否则结束if语句并继续执行else(但那里什么也没有)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM