明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3658|回复: 4

[函数]获取AutoCAD命令行窗口句柄的函数

[复制链接]
发表于 2003-12-3 10:31:00 | 显示全部楼层 |阅读模式
  1. Option Explicit
  2. '相关 API 函数声明
  3. Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
  4.         ByVal lpString As String, ByVal cch As Long) As Long '取得指定窗口的标题
  5. Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long '取得窗口句柄
  6. Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long '获取父窗口句柄

  7. Const GW_CHILD = 5
  8. Const GW_HWNDNEXT = 2
  9. Const WM_GETTEXT = &HD
  10. Const WM_GETTEXTLENGTH = &HE

  11. '获取命令行窗口句柄的示例
  12. Sub MC_GetCommandLineSample()
  13.     Debug.Print GetCommandLineWindows(GetAcadHwnd)
  14. End Sub

  15. '获取AutoCAD主窗口句柄
  16. Public Function GetAcadHwnd() As Long
  17.     GetAcadHwnd = GetParent(GetParent(ThisDrawing.hwnd))
  18. End Function


  19. '遍查子窗口,判断窗口是否为命令行窗口,并返回命令行窗口句柄
  20. Public Function GetCommandLineWindows(Window_Hwnd As Long) As Long
  21.     Dim Buf As String
  22.     Dim BufLen As Long
  23.     Dim Child_hwnd As Long
  24.     Dim Children() As Long
  25.     Dim Num_Children As Integer
  26.     Dim i As Integer
  27.     Dim TitleText As String * 1024
  28.     Dim TitleLeng As Long
  29.     Dim Title As String
  30.     Dim CHwnd As Long
  31.    
  32.     TitleLeng = GetWindowText(Window_Hwnd, TitleText, Len(TitleText))
  33.     Title = Left$(TitleText, TitleLeng)
  34.     If Left(Title, 3) = "命令行" Or Left(Title, 12) = "Command Line" Then
  35.         GetCommandLineWindows = Window_Hwnd
  36.         Exit Function
  37.     End If
  38.    
  39.    
  40.     Num_Children = 0
  41.     Child_hwnd = GetWindow(Window_Hwnd, GW_CHILD) '取得第 1 个子窗口的句柄
  42.     Do While Child_hwnd <> 0 '如果有子窗口
  43.         Num_Children = Num_Children + 1
  44.         ReDim Preserve Children(1 To Num_Children)
  45.         Children(Num_Children) = Child_hwnd
  46.         Child_hwnd = GetWindow(Child_hwnd, GW_HWNDNEXT) '取得下一个兄弟窗口的句柄
  47.     Loop
  48.    
  49.     For i = 1 To Num_Children
  50.         CHwnd = GetCommandLineWindows(Children(i))
  51.         If CHwnd <> 0 Then GetCommandLineWindows = CHwnd
  52.     Next i
  53.    
  54. End Function

评分

参与人数 1金钱 +20 收起 理由
cbmpeter + 20 赞一个!

查看全部评分

 楼主| 发表于 2003-12-3 10:35:00 | 显示全部楼层
该函数进行一些改造,还可以取得AutoCAD窗口中任何一个控件的句柄。
发表于 2003-12-11 13:00:00 | 显示全部楼层
获得后可用来做什么?
发表于 2012-12-14 09:53:00 | 显示全部楼层
这是一个好方法啊,只是我想获取命令行中所输入的内容应该怎么做呢?请教一下老大。
发表于 2013-1-1 23:47:47 | 显示全部楼层
谢谢找控件的分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-12-22 20:11 , Processed in 0.329395 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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