|
楼主 |
发表于 2020-7-11 16:55
|
显示全部楼层
▌具体实现代码
①在窗体加载的同时,获取CAD程序,嵌入窗体中。并将窗体移动到Excel程序的合适位置。
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'SetHook()
xlhwnd = xlapp.Hwnd
xldeskHwnd = FindWindowEx(xlhwnd, 0&, "XLDESK", vbNullString)
GetWindowRect(xldeskHwnd, r) '保存窗体原来的位置及大小到变量r
EXCEL7Hwnd = FindWindowEx(xldeskHwnd, 0&, "EXCEL7", vbNullString)
SetParent(Me.Handle, xldeskHwnd) '设置xldesk窗体为母窗体
'------------------------设置窗体按钮panel位置---------------------------
'按钮位置
Me.Panel1.Dock = DockStyle.Right
Me.Panel1.Left = Me.Left + Me.Width - 50
Me.Panel1.Width = 35
MoveWindow(Me.Handle, 0, 0, r.Width / 2, r.Height, True) '移动窗体位置
'--------------------------打开CAD--------------------------------------
Try
app = GetObject(, "AutoCAD.Application")
app.Visible = True
Catch ex As Exception
Try
app = CreateObject("AutoCAD.Application")
Catch dd As Exception
MsgBox("不能启动AutoCAD,是否没有安装?")
End Try
End Try
'----------------------------------------------------------------
app.Visible = True '显示cad程序界面
lHwnd = GetParent(GetParent(app.ActiveDocument.HWND)) '获得CAD窗体的句柄
If lHwnd = 0 Then Exit Sub '如果没有获取大句柄,直接退出
'------------------------设置Excel7位置尺寸---------------------------
'Excel7位置尺寸
MoveWindow(EXCEL7Hwnd, r.Width / 2, 0, r.Width / 2, r.Height, True)
'----------------------------------------------------------------------------
SetParent(lHwnd, Me.Handle) '设置本窗体为CAD的母窗体
MoveWindow(lHwnd, 0, 0, Me.Width - 50, Me.Height, True)
End Sub
②人为拖动窗体尺寸变化,CAD自适应窗口Excel7工作区自适应变化。
Private Sub Form2_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
xlhwnd = xlapp.Hwnd
xldeskHwnd = FindWindowEx(xlhwnd, 0&, "XLDESK", vbNullString)
GetWindowRect(xldeskHwnd, r) '保存窗体原来的位置及大小到变量r
EXCEL7Hwnd = FindWindowEx(xldeskHwnd, 0&, "EXCEL7", vbNullString)
MoveWindow(lHwnd, 0, 0, Me.Width - 50, Me.Height, True)
'Excel7位置尺寸
MoveWindow(EXCEL7Hwnd, Me.Width, 0, r.Width - Me.Width, r.Height, True)
End Sub
③关闭CAD,窗口恢复原样
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
'UnHook()
SetParent(lHwnd, 0)
app.WindowState = AutoCAD.AcWindowState.acNorm
'//EXCEL7窗口恢复
xlhwnd = xlapp.Hwnd
xldeskHwnd = FindWindowEx(xlhwnd, 0&, "XLDESK", vbNullString)
GetWindowRect(xldeskHwnd, r) '保存窗体原来的位置及大小到变量r
EXCEL7Hwnd = FindWindowEx(xldeskHwnd, 0&, "EXCEL7", vbNullString)
MoveWindow(EXCEL7Hwnd, r.Left, 0, r.Width, r.Height, True)
End Sub
④Excel主窗体尺寸变化时,各部分自适应尺寸变化。
这部分完美的做法应该是利用HOOK技术,监视Excel主程序窗口的变化。目前能力受限,由于目前是自用,也能满足自身需求,优化暂时搁置。
Private Sub Application_WindowResize(Wb As Workbook, Wn As Window) Handles Application.WindowResize
xlhwnd = xlapp.Hwnd
xldeskHwnd = FindWindowEx(xlhwnd, 0&, "XLDESK", vbNullString) '获取xldesk窗口句柄
EXCEL7Hwnd = FindWindowEx(xldeskHwnd, 0&, "EXCEL7", vbNullString) '获取EXCEL7窗口句柄
formhandle = FindWindowEx(xldeskHwnd, 0&, vbNullString, "CAD窗口") '获取标题名为CAD窗口的窗体句柄
If formhandle = 0 Then Exit Sub '获取不到句柄,不做调整
GetWindowRect(xldeskHwnd, r) '保存xldesk的位置及大小到变量r
MoveWindow(formhandle, 0, 0, r.Width / 2, r.Height, True) '实时移动窗体位置
GetWindowRect(formhandle, cadrect) '保存修改过的cad窗体尺寸到=及位置到变量cadrect,为了下一步设置Excel7窗口的大小
MoveWindow(EXCEL7Hwnd, cadrect.Width, 0, r.Width - cadrect.Width, r.Height, True)
End Sub
|
评分
-
1
查看全部评分
-
|