ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

WinAPI 操作win记事本notepad相关

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-7-29 11:40 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
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)
Public Declare PtrSafe Function PostMessage Lib "user32.dll" Alias "PostMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare PtrSafe Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
    (ByVal hWnd As Long, ByVal lpString As String) As Long
Private Declare PtrSafe Function FindWindow& Lib "user32.dll" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String)
Private Declare PtrSafe Function FindWindowEx& Lib "user32.dll" Alias "FindWindowExA" _
    (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String)
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal s As Long)
Private Declare PtrSafe Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" _
    (ByVal pszPath As String) As Long
Private Declare PtrSafe Function CreateFont Lib "gdi32" Alias "CreateFontA" ( _
    ByVal h As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, _
    ByVal W As Long, ByVal i As Long, ByVal u As Long, ByVal s As Long, _
    ByVal c As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, _
    ByVal PAF As Long, ByVal f As String) As Long


Sub 记事本操作() 'winapi方式
    Dim hWnd&, hwndz&, hMenu&, MenuID&, a&, b&, c&, i&, j&, k%, st$
    st = "C:\Users\lss\Documents\abc.txt" '文件路径/名称

        Shell "notepad.exe", 1 '打开记事本/相当于新建文本文件
        Sleep 1000
        hWnd = FindWindow("Notepad", vbNullString)  '记事本句柄
        hwndz = FindWindowEx(hWnd, 0, "Edit", vbNullString) '文本编辑框句柄
        '发送文本"abc"//WM_SETTEXT = &HC
        SendMessage hwndz, &HC, 0, ByVal CStr("abc")
        '发送换行符Chr(10)/Chr(13)/vbCrLf/vbCr/vbLf/vbNewline
        SendMessage hwndz, &H102, ByVal &HD, ByVal 0


        PostMessage hWnd, &H111, 4, 0 '打开另存为对话框
        Sleep 1000
        hwnds = FindWindow("#32770", "另存为") '获取另存为对话框句柄
        b = FindWindowEx(hwnds, 0, "Button", vbNullString) '获取保存按钮句柄
        ss = Array("DUIViewWndClassName", "DirectUIHWND", "FloatNotifySink", "ComboBox", "Edit")
        For i = 0 To UBound(ss)
            If i = 0 Then a = hwnds Else a = c
            c = FindWindowEx(a, 0, ss(i), vbNullString) '获取文件名编辑框句柄
        Next


        SendMessage c, &HC, 0, ByVal st '设置文本WM_SETTEXT = &HC
        SendMessage b, &HF5, 0, 0 '自动点击保存按钮WM_CLICK = &HF5
        Sleep 1000
        PostMessage hWnd, &H10, 0, 0 '关闭窗口WM_CLOSE = &H10
    End If
End Sub

TA的精华主题

TA的得分主题

发表于 2024-7-29 18:16 | 显示全部楼层
能否幫忙研究一下cmd窗口,怎么把中文字符发送进去,再把回显提取出来(我不想用"WScript.Shell“取得回显,也不想用‘.../command /log=d:\ftp\123.log’生成log)

就想用API模拟手工一样,操作cmd窗口,比如把中文命令(cd c:\我的文档)写进去,把结果读出来

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-7-29 19:35 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 lss001 于 2024-7-29 19:37 编辑
伶俐的毛豆 发表于 2024-7-29 18:16
能否幫忙研究一下cmd窗口,怎么把中文字符发送进去,再把回显提取出来(我不想用"WScript.Shell“取得回显 ...


其实另一帖子已经给了您链接,您先看看以下链接,有啥问题再提!
https://club.excelhome.net/forum.php?mod=viewthread&tid=1698267&extra=&mobile=2&_dsign=0fc4b28f
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-11-18 07:38 , Processed in 0.033221 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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