我正在开发一个应在启动时执行以下操作的应用程序:
我已经成功完成了前两个步骤,但是第三步给了我一些问题。
我不知道在父窗口不可见的情况下是否可以使子窗口可见。每当我使孩子可见或使其成为最顶部的窗口时,AutoCAD也会同时可见。
我的目标是运行DLL代码,但使AutoCAD在后台运行,而用户完全看不见。必须通过AutoCAD加载DLL,因为它允许我使用AutoCAD的.NET界面而不是COM。
无论如何,我很好奇我尝试做的事情是否可能实现,可能是通过某些Windows API调用,或者是.NET中的某种方式。
PS:我不确定这种窗口关系是否真的是亲子关系。我假设是这样,因为由于DLL加载,我的窗口属于AutoCAD应用程序实例。
任何帮助是极大的赞赏。谢谢!:)
编辑: DLL代码创建一个窗口。
//CommandMethod is an AutoCAD attribute for entering into the DLL. This code is called
//when the user attempts the command "AUTOCADCOMMANDNAME" or can be done by simulating
//the command programmatically.
[CommandMethod("AUTOCADCOMMANDNAME", CommandFlags.Session)]
public void CommandEntry()
{
MainWindow mainWin = new MainWindow();
mainWin.ShowDialog();
}
主要应用代码
public static void Main()
{ //Use a utility class to create a connection to AutoCAD via COM
AcadApplication acadApp = ACUtil.GetAcadInstance(out createdNewInstance);
acadApp.Visible = false;
//Irrelevant code omitted...
acadApp.ActiveDocument.SendCommand("AUTOCADCOMMANDNAME");
acadApp.Quit(); //Exit AutoCAD application
//Note: doesn't quit until mainWin closes because of ShowDialog()
}
哈哈!我找到了!
因此,我最终SetWindowPos
在Windows API中调用了该函数,并提供了AutoCAD窗口的句柄。我在主应用程序中执行了此操作:
[DllImport("User32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int w, int h, uint flags);
public const int SWP_HIDEWINDOW = 0x0080;
public static void Main()
{
//...Setup AutoCAD...
//Change window size and hide it before calling to open mainWin inside the DLL.
SetWindowPos(new IntPtr(acadApp.HWND), new IntPtr(1), 0, 0, 0, 0, SWP_HIDEWINDOW);
//Display mainWin by entering the DLL.
acadApp.ActiveDocument.SendCommand("AUTOCADCOMMANDNAME");
//Terminate application as before...
}
基本上,我是通过直接修改HWND来告诉AutoCAD窗口隐藏。我还将尺寸设置为width=0
,height=0
这将导致窗口占用尽可能小的尺寸。不幸的是,该窗口将闪烁一次,但就我的目的而言,这可以忽略不计。如果有人能找到消除闪烁的方法,那就太好了!:)
SetWindowPos
,Windows会记住下次显示该应用程序窗口时输入的值。这意味着,如果恢复不正确,则用户下次手动打开AutoCAD时,其坐标为0,0,最小宽度/高度。
若要更改该行为,必须获取窗口信息。对于我的程序,我使用GetWindowRect
了原始设置。关闭程序之前,我使用还原了这些设置SetWindowPos
。有关详细信息,请参见下面的代码:
首先,导入必要的WINAPI函数和结构:
[DllImport("User32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out RECT rect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
在修改窗口之前获取原始设置:
RECT originalRect;
GetWindowRect(new IntPtr(acadApp.HWND), out originalRect);
修改窗口以隐藏(并调整大小):
SetWindowPos(new IntPtr(acadApp.HWND), new IntPtr(1), 0, 0, 0, 0, SWP_HIDEWINDOW);
退出前恢复原始设置:
SetWindowPos(new IntPtr(acadApp.HWND), new IntPtr(1),
originalRect.Left,
originalRect.Top,
originalRect.Right - originalRect.Left,
originalRect.Bottom - originalRect.Top, 0);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句