资源描述
对法恩特插件进行外部操作1
想必大家对法恩特的插件不陌生的;大概是第一款完整的标准件的插件;
空闲的时候做了个小程序,用来对FNT插件进行外部操作,目的是用其他外部程序控制法恩特的软件窗口工作;
首先用到win32 api函数:findwindows;
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
把这句话加到vb程序的声明中,这样在程序里面就能调用findwindows函数来寻找FNT的窗口了,本质上来说每个windows窗口都有一个唯一的句柄,可惜每次运行窗口的时候句柄的值是变动的;
hwnd = FindWindow(vbNullString, "法恩特标准件库--螺栓")
如果“法恩特标准件库--螺栓”这个窗口是打开着的,我们就能利用上面的语句找到这个窗口的句柄;
hwnd这个函数的真值就是句柄;
这是第一步,只有找到了句柄才能够对这个特定的窗口进行操作;
如果需要对其他窗口操作把语句中的窗口名称换掉即可:
如:
hwnd = FindWindow(vbNullString, "法恩特标准件库--轴承")
hwnd = FindWindow(vbNullString, "法恩特标准件库--螺母")
hwnd = FindWindow(vbNullString, "法恩特标准件库--垫圈")
hwnd = FindWindow(vbNullString, "法恩特标准件库--密封件")
这个是随意换的;
如果想操作solidworks窗口,就换成:
hwnd = FindWindow(vbNullString, "Solidworks Office Premium 2007")一样可以得到sw软件的运行窗口;抱歉我用的是sw2007;机器破,没办法;
补充一点:在用上面的hwnd语句之前,要加一个hwnd的定义:
Dim hwnd As Long
这样就把hwnd的类型规定为长整型;
找到hwnd,也就是上面所说的句柄是打算干什么呢?当然是为了找到这个窗口里面显示的其他控件;怎么找呢?
下次再写给大家;
我们说到hwnd是句柄,也就是法恩特窗口的句柄;
我们来看上图:
图中有“确定”“取消”按键,这是我们软件中经常有的东西,这种东西有个专有名称叫控件,是专门做出来的一个软件集合,可以供编程者调用;
图中也还有其他种类的控件,我们可以数一下,一共是12个控件;其中包括“标准件类型”这样的字段也是用控件的形式做出来的。
我们介绍上面控件的目的是要通过窗体的hwnd(句柄)来得到,窗体内控件的hwnd,没想到吧,窗体内的控件也有句柄哦。
那么,我们用一句程序来调用查找控件的模块程序:
Call EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, lParam) '注意 hPwnd 是父窗口的句柄
下面我就要列出查找控件的模块了,基本的意思是用枚举法遍历所有在窗口中的控件;找出控件的hwnd(也就是句柄);
Public Function EnumChildWindowProc(ByVal hwnd As Long, ByRef lParam As Long) As Long
Dim strTitle As String
Dim strClassName As String
lParam = 1
Call GetTitleClass(hwnd, strTitle, strClassName)
那么在这个模块之前,我们当然要声明一下EnumChildWindowProc这个过程了,这个部分有兴趣的朋友可以查阅win32 Api函数,VB里面也有带这个的查找器。
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
那么好了,请看上面最后一句:
Call GetTitleClass(hwnd, strTitle, strClassName)
这明显是调用了另外一段程序,也就是GetTitleClass,
那么我们给出这段程序:
Public Sub GetTitleClass(ByVal hwnd As Long, Title As String, ClassName As String)
Dim nSize As Long
Dim strTitle As String
Dim strClassName As String
nSize = GetWindowTextLength(hwnd)
If nSize > 0 Then
strTitle = Space(255)
Call GetWindowText(hwnd, strTitle, Len(strTitle))
strTitle = Trim(strTitle)
Else
strTitle = "No Title"
End If
strClassName = Space(255)
Call GetClassName(hwnd, strClassName, Len(strClassName))
strClassName = Trim(strClassName)
Title = strTitle
ClassName = strClassName
'MsgBox ClassName
'MsgBox strTitle
End Sub
用上面这段小程序能得到每个控件的三个值:
A、hwnd 句柄值
B、strTitle 就是控件的标题
C、ClassName 控件的类型;
也就是说EnumChildWindowProc要遍历每个控件,然后通过GetTitleClass,得到每个控件的句柄值,控件的标题,控件的类型。
今天我们继续讨论上次我贴出来的一段小程序:
Public Sub GetTitleClass(ByVal hwnd As Long, Title As String, ClassName As String)
Dim nSize As Long
Dim strTitle As String
Dim strClassName As String
nSize = GetWindowTextLength(hwnd)
If nSize > 0 Then
strTitle = Space(255)
Call GetWindowText(hwnd, strTitle, Len(strTitle))
strTitle = Trim(strTitle)
Else
strTitle = "No Title"
End If
strClassName = Space(255)
Call GetClassName(hwnd, strClassName, Len(strClassName))
strClassName = Trim(strClassName)
Title = strTitle
ClassName = strClassName
'MsgBox ClassName
'MsgBox strTitle
End Sub
上段程序中加红色的代码是win32 api,需要进行声明的,如下:
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
我们声明了api函数以后就可以顺利运行程序了,
通过下面两句可以轻松得到每个控件的名字和标题,这样我们就能够有很大收获了。
MsgBox ClassName
MsgBox strTitle
然后可以找张纸把得到的classname和strtiltle记录下来,然后再来分析具体的控件情况。
接下来:我说明一下我们的编程思路:
第一步:找到总窗口的句柄
第二步:通过总窗口的句柄遍历窗口中的控件,得到每个控件的种类和标题
第三步:按控件的种类和标题,对控件进行一一对应,找出我们需要操作的控件,比如“确定”控件(按钮控件)。
第四步:对我们需要操作的控件提取出控件的hwnd(控件句柄);
第五步:用控件句柄hwnd操作控件;
第六步:结合VB的相关其他界面和编程要求对控件实现控制,比如输入、输出、顺序动作等。
下一节,我会将得到的控件列出来,以便大家参考。
(原创)solidworks开发教程27--例题 VB对尺寸的操作
欢迎参与调查
下面的例子是用VB程序对零件的尺寸参数进行操作,注意:目前绝大多数的API函数的单位是米;
Private Sub Command1_Click() ‘建立一个按钮,这是点按钮的事件驱动;
Dim Part As Object '定义Part
dim strFileName as string '定义sTrFileName 串数据类型;
dim longstatus as long '定义长型类型;
dim d1 as integer
dim d2 as integer
dim h as Single
if Dir(strFileName)="" then '检查文件是否存在
MsgBox ("文件不存在,请检查路径:" & vbCrlf & strFileName )
exit sub ‘如果不存在就退出
end if
strFileName =App.Path+"\"+"垫圈.sldprt" ’文件地址;
Set swApp=CreateObject ("SldWorks.Application") '创建swApp
swApp.Visible=True 'swApp 可见
set Part=swApp.OpenDoc4(strFileName,1,0,"",longsstatues) ‘用API打开文件
set Part=swApp.ActivateDoc ("垫圈") ’激活
ProgressBar1.value=4
d1=text1.text ' 分别从text1到text3中取值;text1-3是text控件;
d2=text2.text
h=text3.text
debug.Print d1,d2,h
strFileName="@垫圈.sldprt"
Part.Parameter("d1@草图1"&strFileName).systemValue=d1/1000 '赋值
Part.Parameter("d2@草图1"&strFileName).systemValue=d2/1000 ‘单位是米,所以要除1000
Part.Parameter("h@拉伸1"&strFileName).systemValue=h/1000
part.editRebuild '重建
ProgressBar1.Value=9
Set Part=Nothing
Set swApp=Nothing
ProgressBar1.Vaue=100
StatusBar1.Panels(1).text="更新结束"
end Sub
注意:ProgressBar1是一个显示条控件; StatusBar1是一个状态条控件;
展开阅读全文