ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3699|回复: 6

调用系统自带计算器的代码是什么?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2006-12-26 21:24 | 显示全部楼层 |阅读模式
RT

TA的精华主题

TA的得分主题

发表于 2006-12-26 21:27 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-12-26 21:31 | 显示全部楼层
QUOTE:
以下是引用taller在2006-12-26 21:27:57的发言:
shell("calc.exe")

学习了。请问能否把计算结果直接赋到单元格中?

TA的精华主题

TA的得分主题

发表于 2006-12-26 21:36 | 显示全部楼层
QUOTE:
以下是引用yuanzhuping在2006-12-26 21:31:35的发言:

学习了。请问能否把计算结果直接赋到单元格中?

Excel本身就可以计算,为什么要舍近求远呢

TA的精华主题

TA的得分主题

发表于 2006-12-26 22:52 | 显示全部楼层
QUOTE:
以下是引用taller在2006-12-26 21:36:20的发言:

Excel本身就可以计算,为什么要舍近求远呢

精度不同,比较一下:

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

Sub macro1()
Dim temp As String, i As Long, j As Long
Application.ScreenUpdating = False
Shell "Calc.EXE", vbMinimizedNoFocus '运行计算器
For j = 1 To 20
temp = j + 100 & "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, "计算器") '窗口句柄
i = FindWindowEx(i, ByVal 0&, "Edit", vbNullString) '编辑框句柄
SendMessage i, &HD, Len(temp), ByVal temp '发送编辑框文本至temp
Cells(j, 1) = j + 100 & "的平方根"
Cells(j, 2) = "'" & Sqr(j + 100)
Cells(j, 3) = "'" & temp
Next
[a:c].Columns.AutoFit
SendKeys "%{F4}", True '调用ALT+F4关闭计算器窗口
Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

发表于 2006-12-26 23:09 | 显示全部楼层
谢谢版主!收下慢慢消化。

TA的精华主题

TA的得分主题

发表于 2024-7-5 19:11 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
northwolves 发表于 2006-12-26 22:52
QUOTE:以下是引用taller在2006-12-26 21:36:20的发言:Excel本身就可以计算,为什么要舍近求远呢精度不同, ...

大佬请教一下,win10版本下运行计算器,怎么能取到运行结果呢?我试过用ctrl+v,但是报错【自动化错误2147221040 (800401d0)】
另外,子窗口好像并不是必须的,至少对输入字符来说不是。

  1. Private Declare PtrSafe Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  2. Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  3. Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As LongPtr, ByVal hWndChildAfter As LongPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
  4. Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPtr
  5. Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongPtr
  6. Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

  7. Private Const sleepTime = 100   'ms
  8. Private Const WM_SETFOCUS = &H7
  9. Private Const WM_GETTEXT = &HD
  10. Private Const WM_GETTEXTLENGTH = &HE
  11. Private Const WM_CLOSE = &H10
  12. Private Const WM_QUIT = &H12
  13. Private Const WM_COPYDATA = &H4A

  14. Function macro() As String
  15.     Dim hwnd As Long
  16.     Dim hwndEx As Long
  17.     Dim result As String
  18.     Dim resultLen As Long
  19.    
  20.     Shell "Calc.exe", vbHide
  21.     Sleep sleepTime

  22.     hwnd = FindWindow("ApplicationFrameWindow", vbNullString)
  23.     hwndEx = FindWindowEx(hwnd, 0&, "ApplicationFrameInputSinkWindow", vbNullString)

  24.     ShowWindow hwnd, 1
  25.     SetForegroundWindow hwnd
  26.     Sleep sleepTime
  27.    
  28.     SendKeys "%2"
  29.     Sleep sleepTime

  30.     SendKeys "^v"
  31.     Sleep sleepTime

  32.     SendKeys "~"
  33.     Sleep sleepTime

  34.     resultLen = SendMessage(hwndEx, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0&)
  35.     result = Space$(lngLen)
  36.     SendMessage hwndEx, WM_GETTEXT, ByVal resultLen, result
  37.    
  38.     SendKeys "%{F4}", True
  39.    
  40.     macro = result

  41. End Function
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 23:57 , Processed in 0.046782 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表