Sendkeys 和 Sendmessage 使用技巧一例

本文介绍了一种使用VB编程语言实现的计算任意数值平方根的方法。该方法通过调用Windows API来操作计算器应用程序,实现了对浮点数平方根的精确计算,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Function sqrx(ByVal x As Double) As String '计算平方根(比VB DOUBLE 类型精度高)
   Dim temp As String, i As Long, j As Long
   Shell "Calc.EXE", vbMinimizedNoFocus '运行计算器
   temp = x & "y0.5="
  
For i = 1 To Len(temp)
   SendKeys Mid(temp, i, 1), True '向计算器顺序发送按键消息X (X^Y) 0.5=
Next

temp = String(64, Chr(0))
i = FindWindow(vbNullString, "计算器") '窗口句柄
j = FindWindowEx(i, ByVal 0&, "Edit", vbNullString) '编辑框句柄
SendMessage j, &HD, Len(temp), ByVal temp '发送编辑框文本至temp
SendKeys "%{F4}", True '调用ALT+F4关闭计算器窗口
sqrx = temp
End Function

Private Sub Command1_Click()
    Dim i As Integer
    For i = 17 To 24
      Debug.Print "sqrx(" & i & ")=" & sqrx(i)
    Next
End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值