cad.net cad启动慢? cad2008启动慢? cad启动延迟? cad卡住? cad98%卡? 默认打印机!!

默认打印机是不是联网打印机,如果cad找不到这个打印机将会很慢才打开cad的界面(它真的不是卡死了,而是找不到...)
奇妙的是桌子至今都没有利用新建线程的方式来控制这个打印机等待,而是直接在主线程上面实现(cad公司是知道这回事的,因为我在官网有解答)...

一种想法就是双击cad.exe的时候插入某些代码到exe前面,这个对于敲c++的人恐怕不难...

那cad.net只有在界面加载之后才能运行,那么第一次启动的时候就等一下,以至于我下面的代码能执行,并提示...
为什么需要提示就不用我多说了吧....(我本来也是知道的,但是这次我重新安装了系统之后,我又忘了打印机等待这件事....
所以把此代码添加到加载后立即运行的class里面~(继承IExtensionApplication的class啦)

或者新建一个线程...(在线程内死循环以下代码~)

try
{
                var dayinjis = LocalPrinter.GetLocalPrinters();
                if (dayinjis != null && dayinjis.Length > 0)
                {
                    if (dayinjis[0][0] == '\\')//第一个是默认打印机,如果是网络打印机
                    {
                        bool fa = false;
                        const string pdf = "Microsoft Print to PDF";
                        if (dayinjis.Contains(pdf))
                        { 
                            fa = Win32api.SetDefaultPrinter(pdf);
                        }
                        else
                        {
                            foreach (var item in dayinjis)
                            {
                                if (item[0] != '\\')
                                { 
                                    fa = Win32api.SetDefaultPrinter(item);
                                    break;
                                }
                            }
                        }
                        if (!fa)
                        {
                            MessageBox.Show("不允许默认打印机为网络打印机,找不到的时候打开cad将会很慢\n当前电脑所有都是网络打印机", "惊惊盒子");
                        }
                    }
                }
            }
catch (System.Exception e)
{
      MessageBox.Show("修改默认打印机出错\n" + e.Message, "惊惊盒子");
}
public class LocalPrinter
{
        /// <summary>
        /// 系统所有打印机名称(默认将在第一)
        /// </summary> 
        public static string[] GetLocalPrinters()
        {
            var fPrinters = new List<string>(); 
            try
            {
                PrintDocument fPrintDocument = new PrintDocument();
                string s = fPrintDocument.PrinterSettings.PrinterName;//默认打印机
                if (s != null)
                {
                    //默认打印机始终出现在列表的第一项
                    fPrinters.Add(s);
                } 
                foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
                {
                    if (!fPrinters.Contains(fPrinterName))
                    {
                        fPrinters.Add(fPrinterName);
                    }
                }
            }
            catch  
            {  }
            return fPrinters.ToArray();
        }
}

public partial class Win32api
{
        /// <summary>
        /// 设置默认打印机    
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        [DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(string Name);
}
posted @ 2019-02-21 17:04  惊惊  阅读(890)  评论(0编辑  收藏  举报