2016年2月17日

Disabling AutoCAD’s complete UI using .NET

Disabling AutoCAD's complete UI using .NET

Disabling AutoCAD’s complete UI using .NET

In the first post in this series, we saw how to disable AutoCAD's ribbon. In the second post, we saw how to make (with some caveats) AutoCAD's toolbars disappear. In this post we're going to throw all that away and show how to get better results with a single line of code. <sigh>

But before all that, a big "thanks" to both James Meading and Alexander Rivilis, who have helped us get to this point.

James pointed out a fairly significant flaw in yesterday's toolbar-hiding code (hence the mention of caveats, above), in that it didn't place toolbars on multiple rows at exactly the same position when they're made visible, once again. I started to work around this by making toolbars visible in the order of their "top" index: this seemed to work well enough in my cursory tests, but I didn't see a little strangeness that I was planning on dealing with today.

But, overnight, Alexander provided some MFC code that disables AutoCAD's UI at a more basic level: disabling the complete AutoCAD frame with all of its user interface elements, avoiding us having to hide anything.

I took Alexander's code and found a simple way to make it work from C# using P/Invoke. And that's the code we're going to see today:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.Runtime;

using System;

using System.Runtime.InteropServices;

 

namespace UserInterfaceManipulation

{

  public class Commands

  {

    [DllImport("user32.dll")]

    static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

 

    [CommandMethod("DU")]

    public static void DisableUICommand()

    {

      EnableUI(false);

    }

 

    [CommandMethod("EU")]

    public static void EnableUICommand()

    {

      EnableUI(true);

    }

 

    public static void EnableUI(bool enable)

    {

      EnableWindow(Application.MainWindow.Handle, enable);

    }

  }

}

 

Here are the updated DU and EU commands in action, disabling and enabling the AutoCAD UI, respectively.

Disabling the full UI

You'll notice that disabling leaves the toolbars in their place and doesn't grey them out, but that hovering over them shows that they're properly disabled. While disabled, the ribbon is greyed out in much the way we saw at the beginning of the week.

留下您的评论