Advertisement
Guest User

Untitled

a guest
Jan 24th, 2011
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.Runtime;
  7. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  8. using System.Windows.Forms;
  9.  
  10. /// PaletteUtils.cs  Copyright(c) 2008   Tony Tanzillo  / www.caddzone.com
  11.  
  12. /// UnComment this if you are using ExecuteInDocumentContext, and
  13. /// also using the CommandClass attribute elswhere in the same assembly.
  14. ///
  15. /// [assembly: CommandClass(typeof(CaddZone.Windows.PaletteUtils))]
  16.  
  17. namespace CaddZone.AutoCAD.Windows
  18. {
  19.    /// <summary>
  20.    /// Utility functions for use from Palettes and Modeless dialogs.
  21.    /// </summary>
  22.  
  23.    public static class PaletteUtils
  24.    {
  25.       /// <summary>
  26.       ///
  27.       /// Activates the AutoCAD Editor Window when a palette has focus.
  28.       /// This should be called immediately before calling a GetXxxxx()
  29.       /// method of the Editor to acquire graphical/command line input.
  30.       ///
  31.       /// </summary>
  32.  
  33.       public static void ActivateEditor()
  34.       {
  35.          PostMessage( ActiveDocument.Window.Handle, WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero );
  36.          System.Windows.Forms.Application.DoEvents();
  37.       }
  38.  
  39.         class Thunk : IDisposable
  40.         {
  41.             private EventHandler handler;
  42.             public Thunk( EventHandler handler )
  43.             {
  44.                 if( handler == null )
  45.                     throw new ArgumentNullException( "handler" );
  46.                 this.handler = handler;
  47.                 System.Windows.Forms.Application.Idle += Application_Idle;
  48.             }
  49.  
  50.             void Application_Idle( object sender, EventArgs e )
  51.             {
  52.                 System.Windows.Forms.Application.Idle -= Application_Idle;
  53.                 ActivateEditor();
  54.                 try
  55.                 {
  56.                     handler( null, e );
  57.                 }
  58.                 finally
  59.                 {
  60.                     Dispose();
  61.                 }
  62.             }
  63.  
  64.             ~Thunk()
  65.             {
  66.                 Dispose();
  67.             }
  68.  
  69.             public void Dispose()
  70.             {
  71.                 GC.SuppressFinalize( this );
  72.                 if( handler != null )
  73.                     System.Windows.Forms.Application.Idle -= Application_Idle;
  74.                 handler = null;
  75.             }
  76.  
  77.         }
  78.  
  79.         public static void OnEditorActivated( object sender, EventHandler handler )
  80.         {
  81.             if( AcadApp.DocumentManager.IsApplicationContext )
  82.             {
  83.                 new Thunk( handler );
  84.             }
  85.         }
  86.  
  87.       /// <summary>
  88.       /// The currently active document
  89.       /// </summary>
  90.  
  91.       public static Document ActiveDocument
  92.       {
  93.          get
  94.          {
  95.             return AcadApp.DocumentManager.MdiActiveDocument;
  96.          }
  97.       }
  98.  
  99.       /// <summary>
  100.       ///
  101.       /// Executes code in the document context (the inverse of the
  102.       /// DocumentCollection's ExecuteInApplicationContext() method).
  103.       ///
  104.       /// Call this method from the Application context when there is
  105.       /// no command active, and pass a delegate that returns void and
  106.       /// takes no parameters, and the code in the delegate will be
  107.       /// executed in the document context, as if it were being called
  108.       /// from the handler of a registered command (well, that's because
  109.       /// it actually is called from the handler of a registered command :)
  110.       ///
  111.       /// If you execute AutoCAD commands from the handler, you must
  112.       /// be extremely careful, and must ensure AutoCAD has returned
  113.         /// to a quiescent state (e.g., at the Command: prompt, waiting
  114.         /// for input), at the point when the delegate returns.
  115.       ///
  116.       /// If you call this API from the handler of a UI element, like
  117.       /// a button on a palette or modeless dialog, you should always
  118.       /// disable that UI element while the handler executes. You can
  119.       /// disable the UI before calling this, and enable it from the
  120.       /// handler passed as the argument, just before it returns.
  121.       ///
  122.       /// </summary>
  123.  
  124.       public static void ExecuteInDocumentContext( ExecuteInDocumentContextDelegate handler )
  125.       {
  126.          if( handler == null )
  127.             return;
  128.          ActivateEditor();
  129.          if( !AcadApp.DocumentManager.IsApplicationContext )
  130.             handler();
  131.          else
  132.          {
  133.             if( !ActiveDocument.Editor.IsQuiescent )
  134.                throw new InvalidOperationException( "AutoCAD is busy" );
  135.             documentContextHandler = handler;
  136.             ActiveDocument.SendStringToExecute( commandName + "\n", true, false, false );
  137.             acedPostCommand( "" );
  138.          }
  139.       }
  140.  
  141.  
  142.       [System.Security.SuppressUnmanagedCodeSecurity]
  143.       [DllImport( "user32.dll", CharSet = CharSet.Auto )]
  144.       public static extern int PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam );
  145.  
  146.         [DllImport( "user32.dll", CharSet = CharSet.Auto )]
  147.         public static extern IntPtr SendMessage( IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam );
  148.  
  149.         private const int WM_SETFOCUS = 7;
  150.       private const string commandName = "DOCUMENT_COMMAND";
  151.  
  152.       private const CommandFlags CommandFlagsSilent = CommandFlags.NoHistory | CommandFlags.NoMultiple;
  153.       private const CommandFlags CommandFlagsSilentNoUndo = CommandFlagsSilent | CommandFlags.NoUndoMarker;
  154.  
  155.       public delegate void ExecuteInDocumentContextDelegate();
  156.  
  157.       private static ExecuteInDocumentContextDelegate documentContextHandler = null;
  158.  
  159.       [CommandMethod( commandName, CommandFlagsSilent )]
  160.       public static void OnCommandExecute()
  161.       {
  162.          if( documentContextHandler != null )
  163.          {
  164.             bool flag = AcadApp.DocumentManager.DocumentActivationEnabled;
  165.                 AcadApp.DocumentManager.DocumentActivationEnabled = false;
  166.                 try
  167.             {
  168.                documentContextHandler();
  169.             }
  170.             finally
  171.             {
  172.                documentContextHandler = null;
  173.                AcadApp.DocumentManager.DocumentActivationEnabled = flag;
  174.             }
  175.          }
  176.       }
  177.  
  178.       [System.Security.SuppressUnmanagedCodeSecurity]
  179.       [DllImport( "acad.exe", CallingConvention = CallingConvention.Cdecl,
  180.          EntryPoint = "?acedPostCommand@@YAHPB_W@Z" )]
  181.       extern static public int acedPostCommand( string cmd );
  182.    }
  183.  
  184.     /// <summary>
  185.     /// The ActivateEditor() method works for simple Button controls, but
  186.     /// does not always work for ToolStrip buttons or menu items.
  187.     ///
  188.     /// To solve that you can use this class instead of the ToolStrip class,
  189.     /// and it will ensure that the AutoCAD editor gets the focus when you
  190.     /// call a getxxxxx() method.
  191.     ///
  192.     /// </summary>
  193.  
  194.     public class PaletteToolStrip : ToolStrip
  195.     {
  196.         protected override void OnMouseDown( MouseEventArgs mea )
  197.         {
  198.             if( !base.DesignMode )
  199.             {
  200.                 ToolStripItem item = this.GetItemAt( mea.X, mea.Y );
  201.                 if( item != null && ( item is ToolStripButton || item is ToolStripMenuItem ) )
  202.                 {
  203.                     PaletteUtils.OnEditorActivated( item,
  204.                         delegate( object sender, EventArgs e )
  205.                         {
  206.                             item.PerformClick();
  207.                         }
  208.                     );
  209.                     return;
  210.                 }
  211.             }
  212.             base.OnMouseDown( mea );
  213.         }
  214.  
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement