|
其实无须定义计算器执行文件的路径就可以调用计算器的:
以下是我的计算器模块:
Option Compare Database
Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Const CF_TEXT = 1
Function calculator()
'Dim ctlActiveControl As Control
'Dim strControlName As String
'Dim hwnd As Long
Dim strCalculate As String
'hwnd = GetActiveWindow() '获取活动窗口句柄
'Set ctlActiveControl = Screen.ActiveControl '获取活动控件
'strControlName = ctlActiveControl.Name '焦点控件名字
SendKeys "+{HOME}", True '全选计算算式
SendKeys "^x", True '剪切计算算式
Dim ReturnValue
ReturnValue = Shell("Calc.EXE", 1) ' 运行计算器。
Call Sleep(500) '延时让计算器有时间打开
AppActivate ReturnValue ' 激活计算器。
SendKeys "%2" '切换到科学模式
SendKeys "+", True '当你的输入法默认是中文的时候需要执行这一条,否则请把它comment掉
strCalculate = GetClipText()
strCalculate = Replace(strCalculate, "^", "y") '以下把常规的科学计算按钮替换为计算器键盘快捷键
strCalculate = Replace(strCalculate, "m-", "{^}q")
strCalculate = Replace(strCalculate, "m+", "{^}p")
strCalculate = Replace(strCalculate, "ms", "{^}m")
strCalculate = Replace(strCalculate, "mr", "{^}r")
strCalculate = Replace(strCalculate, "mc", "{^}l")
strCalculate = Replace(strCalculate, "+-", "{F9}")
strCalculate = Replace(strCalculate, "sqr", "@")
strCalculate = Replace(strCalculate, "degree", "{F3}")
strCalculate = Replace(strCalculate, "rad", "{F4}")
strCalculate = Replace(strCalculate, "grad", "{F5}")
strCalculate = Replace(strCalculate, "inv", "i")
strCalculate = Replace(strCalculate, "mod", "d")
strCalculate = Replace(strCalculate, "sinh", "{^}s")
strCalculate = Replace(strCalculate, "cosh", "{^}o")
strCalculate = Replace(strCalculate, "tanh", "{^}t")
strCalculate = Replace(strCalculate, "ln", "n")
strCalculate = Replace(strCalculate, "int", ";")
strCalculate = Replace(strCalculate, "mc", "{^}l")
strCalculate = Replace(strCalculate, "sin", "s")
strCalculate = Replace(strCalculate, "cos", "o")
strCalculate = Replace(strCalculate, "tan", "t")
strCalculate = Replace(strCalculate, "dms", "m")
strCalculate = Replace(strCalculate, "pi", "p")
strCalculate = Replace(strCalculate, "mc", "{^}l")
strCalculate = Replace(strCalculate, "f-e", "v")
strCalculate = Replace(strCalculate, "mc", "{^}l")
strCalculate = Replace(strCalculate, "exp", "x")
strCalculate = Replace(strCalculate, "sq", "q")
strCalculate = Replace(strCalculate, "cube", "#")
strCalculate = Replace(strCalculate, "log", "l")
strCalculate = Replace(strCalculate, "i^", "{^}y")
strCalculate = Replace(strCalculate, "icube", "{^}b")
strCalculate = Replace(strCalculate, "ilog", "{^}g")
strCalculate = Replace(strCalculate, "(", "{(}") 'senkeys要把+~[]^%()放入{}才可正确发出
strCalculate = Replace(strCalculate, ")", "{)}")
strCalculate = Replace(strCalculate, "+", "{+}")
strCalculate = Replace(strCalculate, "^", "{^}")
strCalculate = Replace(strCalculate, "%", "{%}")
strCalculate = Replace(strCalculate, "~", "{~}")
strCalculate = Replace(strCalculate, "[", "{[}")
strCalculate = Replace(strCalculate, "]", "{]}")
SendKeys strCalculate, True '粘帖计算算式
'Debug.Print strCalculate
Sleep (300)
SendKeys "=", True ' 取得总合。
SendKeys "^c", True '复制结果
SendKeys "%{F4}", True ' 按 ALT+F4 关闭计算器。
'SetForegroundWindow (hwnd) '从新设定活动窗口
'ctlActiveControl.SetFocus '把焦点给回输入框
SendKeys "^v", True '粘帖结果给输入框
SendKeys "{TAB}", True
End Function
Private Function GetClipText() As String
Dim hMem As Long
Dim lpData As Long
Dim nClipSize As Long
Dim bytClipData() As Byte
Dim sClipString As String
If OpenClipboard(ByVal 0&) Then '如果OpenClipboard函数返回非0值,说明成功打开剪贴板
hMem = GetClipboardData(CF_TEXT) '获取剪贴板中以文本格式存在的内存对象的句柄
If CBool(hMem) Then '如果剪贴板中对应的格式不存在,此时的hMem会是0(Null),这里用CBool把它转换成Boolean类型加以判断
lpData = GlobalLock(hMem) '获取内存对象第一个字节的内存地址
nClipSize = GlobalSize(hMem) '获取内存对象的字节长度
ReDim bytClipData(1 To nClipSize) '修改缓冲字节数组的长度,确保能够容纳内存对象的全部数据
CopyMemory bytClipData(1), ByVal lpData, nClipSize '复制内存对象的数据到字节数组中,注意Byval的用法
sClipString = StrConv(bytClipData, vbUnicode) '将字节转化成字符串
GetClipText = sClipString
'MsgBox "当前剪贴板内的文本是:" & vbCrLf & sClipString '将结果显示给用户
Else
MsgBox "当前剪贴板内没有文本"
End If
CloseClipboard
End If
End Function
|
|