Sunday, November 21, 2010

Tạo Windows Form trong AutoCAD .Net

Showing a splash-screen from your AutoCAD .NET application

(http://through-the-interface.typepad.com/through_the_interface/2007/06/showing_a_splas.html)

Thanks once again to Viru Aithal for the inspiration behind this post, although I did write most of the code, this time. :-)
Adding a splash screen can give a touch of class to your application, assuming it's done non-intrusively. This post focuses on how best to do so within AutoCAD, and use the time it's displayed to perform initialization for your application.
The first thing you need to do is add a Windows Form to your project:
Splash_screen_1
You should select the standard "Windows Form" type, giving an appropriate name (in this case I've used "SplashScreen", imaginatively enough).
Splash_screen_2
Once this is done, you should set the background for the form to be your preferred bitmap image, by browsing to it from the form's BackgroundImage property:
Splash_screen_3
Now we're ready to add some code. Here's some C# code that shows how to show the splash-screen from the Initialize() method:

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Prompts; // This is the name of the module

namespace SplashScreenTest
{
  public class Startup : IExtensionApplication
  {
    public void Initialize()
    {
      SplashScreen ss = new SplashScreen();

      // Rather than trusting these properties to be set
      // at design-time, let's set them here
      ss.StartPosition =
        System.Windows.Forms.FormStartPosition.CenterScreen;
      ss.FormBorderStyle =
        System.Windows.Forms.FormBorderStyle.None;
      ss.Opacity = 0.8;
      ss.TopMost = true;
      ss.ShowInTaskbar = false;

      // Now let's disply the splash-screen
      Application.ShowModelessDialog(
        Application.MainWindow,
        ss,
        false
      );
      ss.Update();

      // This is where your application should initialise,
      // but in our case let's take a 3-second nap
      System.Threading.Thread.Sleep(3000);

      ss.Close();
    }
    public void Terminate()
    {
    }
  }
}
Some notes on the code:
  • I used a sample application called "Prompts" - you should change the using directive to refer to your own module name.
  • We're setting a number of properties dynamically (at runtime), rather than stepping through how to set them at design-time.
  • We've set the splash screen to be 80% opaque (or 20% transparent). This is easy to adjust.
  • Some of the additional properties may be redundant, but they seemed sensible to set (at least to me).
Here's the result... I've set up my application to demand-load when I invoke a command, which allowed me to load a DWG first to show off the transparency of the splash-screen (even though the above code doesn't actually define a command - so do expect an "Unknown command" message, if you do exactly the same thing as I have). You may prefer to set the module to load on AutoCAD startup, otherwise.
Splash_screen_4

Update:
Roland Feletic brought it to my attention that this post needed updating for AutoCAD 2010. Thanks, Roland!
I looked into the code, and found that the call to ShowModelessDialog needed changing to this:
      Application.ShowModelessDialog(
        Application.MainWindow.Handle,
        ss,
        false
      );
I also found I had to add an additional assembly reference to PresentationCore (a .NET Framework 3.0 assembly).

No comments: