明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3875|回复: 2

CAD里创建非模态窗体

[复制链接]
发表于 2009-10-16 15:23:00 | 显示全部楼层 |阅读模式

CAD二次开发增加窗体时,尽管窗体的show属性为vbmodeless,运行时如果鼠标不在窗体内操作,窗体上的控件都是无法获得焦点的,例如根本无法在窗体内的文本框内输入文字。下面的代码可以做到随意时窗体和CAD获得焦点,方便操作。

================================[模块代码开始]=============

'API函数声明

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Dim ThisHwnd As Long

Public Const GWL_STYLE = -16

Public Const WS_POPUP = &H80000000

' 函数EnumWindowsProc返回值

Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Integer

    Dim title As String * 32

    Call GetWindowText(hwnd, ByVal title, 32)

    If InStr(title, "示例窗体") Then      '根据窗体的"Caption"值来枚举

        ThisHwnd = hwnd              '已找到窗体,则退出

        EnumWindowsProc = False

    Else

        EnumWindowsProc = True       '未找到,继续枚举

    End If

End Function

Public Function SubClass() As Long

    Dim Flags As Long

    Flags = GetWindowLong(ThisHwnd, GWL_STYLE)

    Flags = Flags Xor WS_POPUP

    SetWindowLong ThisHwnd, GWL_STYLE, Flags

End Function
================================[模块代码结束]=============

================================[窗体代码开始]=============

Private Sub UserForm_Activate()
    Call EnumWindows(AddressOf EnumWindowsProc, vbNull)
    Call SubClass
End Sub

================================[窗体代码结束]=============

窗体的showmodal属性值必须设为False.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2009-10-16 16:14:00 | 显示全部楼层

太复杂了,可以很简单实现

SetParent FindWindow(vbNullString, UserForm1.Caption), ThisDrawing.Application.HWND

发表于 2018-7-12 13:52:21 | 显示全部楼层
yyzhan12 发表于 2009-10-16 16:14
太复杂了,可以很简单实现SetParent FindWindow(vbNullString, UserForm1.Caption), ThisDrawing.Applicatio ...

具体怎么用?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-29 02:33 , Processed in 0.172234 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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