cottage 发表于 2005-10-21 22:18:00

求助!cad嵌入vb问题!

<P>求助!我看了伊凡版主以前的一个帖子,是关于怎样把cad窗体嵌入vb程序中,我也照着做了一个,实现了嵌入,并且cad窗体能随着vb窗体的拖动改变大小。但是问题是嵌入后无法用vb控制cad画图,不知道问题在哪,希望高手帮帮忙,十分感谢!</P>
<P>这是关于上面问题的一个程序段,是想在嵌入后画条直线,但是不行:</P>
<P>Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long<BR>Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long<BR>Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long<BR>Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long</P>
<P>Private Type RECT<BR>&nbsp;&nbsp;&nbsp; Left As Long<BR>&nbsp;&nbsp;&nbsp; Top As Long<BR>&nbsp;&nbsp;&nbsp; Right As Long<BR>&nbsp;&nbsp;&nbsp; Bottom As Long<BR>End Type</P>
<P>Public acadApp As Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'The AutoCAD application object<BR>Public acadDoc As Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'The AutoCAD document (drawing) object<BR>Public moSpace As Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'The model space object collection<BR>Public paSpace As Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'The paper space object collection<BR>Public AppLayer As Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '应用程序使用的层<BR>Public acadDocname As String<BR>Private acadApp As Object<BR>Private lHwnd As Long '保存ACAD应用程序的窗口句柄<BR>Private lState As Long '保存ACAD的初始窗口状态<BR>Private r As RECT '保存ACAD的初始窗口位置</P>
<P>Private Sub Form_Load()<BR>&nbsp;&nbsp;&nbsp; On Error GoTo ErrTrap<BR>&nbsp;&nbsp;&nbsp; Set acadApp = GetObject(, "AutoCAD.Application")<BR>&nbsp;&nbsp;&nbsp; acadApp.Visible = True<BR>&nbsp;&nbsp;&nbsp; lHwnd = GetParent(GetParent(acadApp.Activedocument.hwnd))<BR>&nbsp;&nbsp;&nbsp; If lHwnd = 0 Then Exit Sub<BR>&nbsp;&nbsp;&nbsp; lState = acadApp.WindowState<BR>&nbsp;&nbsp;&nbsp; acadApp.WindowState = 1 '设置ACAD的窗口状态为默认,用于保存窗口位置。<BR>&nbsp;&nbsp;&nbsp; GetWindowRect lHwnd, r<BR>&nbsp;&nbsp;&nbsp; SetParent lHwnd, Form1.hwnd<BR>&nbsp;&nbsp;&nbsp; Form1.ScaleMode = vbPixels '将VB窗体默认的缇单位改为以像素为单位。<BR>&nbsp;&nbsp;&nbsp; SetWindowPos lHwnd, 0, Form1.ScaleLeft, Form1.ScaleTop, Form1.ScaleWidth, Form1.ScaleHeight, 0<BR>&nbsp;&nbsp;&nbsp; Exit Sub<BR>ErrTrap:<BR>&nbsp;&nbsp;&nbsp; On Error GoTo 0<BR>End Sub</P>
<P>Private Sub Form_Resize()<BR>&nbsp;&nbsp;&nbsp; SetWindowPos lHwnd, 0, Form1.ScaleLeft + 100, Form1.ScaleTop, Form1.ScaleWidth - 100, Form1.ScaleHeight, 0<BR>End Sub</P>
<P>Private Sub Form_Unload(Cancel As Integer)<BR>&nbsp;&nbsp;&nbsp; If lHwnd = 0 Then Exit Sub<BR>&nbsp;&nbsp;&nbsp; SetParent lHwnd, 0<BR>&nbsp;&nbsp;&nbsp; SetWindowPos lHwnd, 0, r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top, 0<BR>&nbsp;&nbsp;&nbsp; acadApp.WindowState = lState<BR>&nbsp;&nbsp;&nbsp; Set acadApp = Nothing<BR>End Sub</P>
<P>Private Sub Command1_Click()<BR>Call lin<BR>End Sub</P>
<P>Private Sub lin()<BR>Dim lineObj As Object<BR>&nbsp;&nbsp;&nbsp; Dim startPoint(0 To 2) As Double<BR>&nbsp;&nbsp;&nbsp; Dim endPoint(0 To 2) As Double<BR>&nbsp;&nbsp;&nbsp; ' Define the start and end points for the line<BR>&nbsp;&nbsp;&nbsp; startPoint(0) = 1#: startPoint(1) = 1#: startPoint(2) = 0#<BR>&nbsp;&nbsp;&nbsp; endPoint(0) = 500#: endPoint(1) = 500#: endPoint(2) = 0#<BR>&nbsp;&nbsp;&nbsp; ' Create the line in model space<BR>&nbsp;&nbsp;&nbsp; Set lineObj = moSpace.AddLine(startPoint, endPoint)<BR>acadDoc.Application.ZoomAll<BR>End Sub</P>
<P>谢谢!</P>
页: [1]
查看完整版本: 求助!cad嵌入vb问题!