欢迎来到我的博客
Civil 3D开发与应用,欢迎加入QQ群:484124761
AutoCAD开发,欢迎加入QQ群:193522571

设置autocad绘图窗口的大小

缘由:

为了截取(使用bmpout、pngout、wmfout类似的命令)固定大小的图片,

所以要设置窗体的大小,

没找到autocad内部功能,

因此找ai帮忙写了代码(控制台程序):

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    // 导入Windows API函数
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);

    // 常量定义
    private const uint SWP_NOMOVE = 0x0002;
    private const uint SWP_NOZORDER = 0x0004;
    private const uint GW_CHILD = 5;
    private const uint GW_HWNDNEXT = 2;

    static void Main(string[] args)
    {
        // 获取AutoCAD进程
        Process[] processes = Process.GetProcessesByName("acad");
        if (processes.Length == 0)
        {
            Console.WriteLine("AutoCAD is not running.");
            return;
        }

        // 获取AutoCAD主窗口句柄
        IntPtr hwnd = processes[0].MainWindowHandle;
        if (hwnd == IntPtr.Zero)
        {
            Console.WriteLine("Failed to get AutoCAD window handle.");
            return;
        }

        // 查找绘图区域子窗口
        IntPtr drawingAreaHandle = FindDrawingAreaWindow(hwnd);
        if (drawingAreaHandle == IntPtr.Zero)
        {
            Console.WriteLine("Failed to find AutoCAD drawing area window.");
            return;
        }

        // 设置绘图区域窗口大小为200x200
        // 目的是为了输出图片大小为200x200
        // 第一输出后图片的大小为184x161,因此将窗体的大小分别增加了16和39
        int width = 216;
        int height = 239;
        bool result = SetWindowPos(drawingAreaHandle, IntPtr.Zero, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);

        if (result)
        {
            Console.WriteLine("AutoCAD drawing area size set to 200x200.");
        }
        else
        {
            Console.WriteLine("Failed to set AutoCAD drawing area size.");
        }
    }

    // 递归查找绘图区域窗口
    private static IntPtr FindDrawingAreaWindow(IntPtr parentHandle)
    {
        IntPtr childHandle = GetWindow(parentHandle, GW_CHILD);
        while (childHandle != IntPtr.Zero)
        {
            // 获取窗口类名
            System.Text.StringBuilder className = new System.Text.StringBuilder(256);
            GetClassName(childHandle, className, className.Capacity);

            // 检查是否是绘图区域窗口
            if (className.ToString().StartsWith("Afx:"))
            {
                Console.WriteLine("Found drawing area window with class name: " + className.ToString());
                return childHandle;
            }

            // 递归查找子窗口
            IntPtr drawingAreaHandle = FindDrawingAreaWindow(childHandle);
            if (drawingAreaHandle != IntPtr.Zero)
            {
                return drawingAreaHandle;
            }

            // 查找下一个兄弟窗口
            childHandle = GetWindow(childHandle, GW_HWNDNEXT);
        }

        return IntPtr.Zero;
    }
}

运行后窗体是这个模样的:

 ***out的图片大小就是我所期望的了!

 

输出图片是简单重复的工作,

因此又找ai写了lisp程序,

简化一下操作:

(defun c:TT (/ ss pngfile)
  ;; 提示用户选择对象
  (setq ss (ssget))
  (if ss
    (progn
      ;; 缩放到选中对象
      (command "_.zoom" "_o" ss "")
      
      ;; 弹出对话框获取PNG文件名称,设置默认路径
      (setq pngfile (getfiled "保存PNG文件" "E:\\MYZW\\repos\\SlopeDrawingAssistant\\DrawingAssistant\\Resources" "png" 1))
      (if pngfile
        (progn
          ;; 运行 pngout 命令
          (command "_.pngout" pngfile "")
          
          ;; 完成命令
          (princ "\nPNG 导出完成。")
        )
        (princ "\n未指定PNG文件名称,操作取消。")
      )
    )
    (princ "\n未选择任何对象。")
  )
  (princ)
)

 

posted @ 2025-03-18 15:31  david96007  阅读(57)  评论(0)    收藏  举报