In this post, I focus on showing message bubble window in various ways. There are some information/blog posts we can find online, especially from Kean Walmsley's excellant blog.
It would be nice when AutoCAD runs our custom-developed tools and something happens, user gets prompted in a way not too intrusive. Of course, the "something" is not too critical to continue the tool running, in most cases. Things like update availability checking, background work status, and so on.
I list 3 ways of showing message in bubble window from AutoCAD: using AutoCAD status bar, using AutoCAD InfoCenter and using Window's system tray.
Firstly, since there are mutiple ways to show bubble window from AutoCAD, in order to simplify the calling method from AutoCAD, I created an interface and have each bubble window showing class implement the interface. This way, the custom tool that wants to show bubble window would be effectively separated from the concrete implementation of how bubble window is shown in easy approach, as long as the approach implements the interface. Here is the code of the interface:
You may noticed, this is C# code and I uses optional parameters, which is newly adopted in C# 4.0. Obviously I run my code with AutoCAD2012. If you run earlier AutoCAD version and develop with .NET2.0/3.x, then you can only use optional parameter with VB.NET. If you use C#, you'd have to write a few overloaded ShowBubbleWindow() methods that take different parameters.
As I mentioned here I show three 3 ways of showing bubble window, which is done with 3 classes all implementing the IMyMessageBubble interface: MyAcadStatusBarBubble, MyAcadInfoCenterBubble and MyWindowSystemTrayBubble. Thanks to the interface, I can go ahead to write my custom tool without knowing how the 3 classes actually show their bubble window. Here is my simple custom tool that just shows the bubble window with different command. Optionally, upon the bubble window being clicked/closed, an Acad command can be called and executed. Here is the code:
You must notice there is command "Hello". This command is there to demonstrate that when the something happens to the bubble window (clicked, closed), we can make a call to an AutoCAD command, if needed. Read on.
Here are 3 classes that implements IMyMessageBubble interface.
1. Using AutoCAD Status Bar
The code of the class MyAcadStatusBarBubble is here:
Pay attention to the line of code in the Constructor:
_item.Icon = System.Drawing.SystemIcons.Exclamation;
TrayItem's icon must be set to a valid System.Drawing.Icon object, or the TrayItemBubbleWindow will not be shown. That is, when TrayItem.ShowBubbleWindow() is called, the TrayItemBubbleWindow will close immediately with TrayItemBubbleWindowCloseReason.FailedToCreate.
From the code we can see, although TrayItemBubbleWindow is an object separated from TrayItem, but it does not have a method to show itself. It relies on an TrayItem to be shown. Thus, the need to create a TrayItem and added it to AutoCAD status bar. In my case, the TrayItem is only there for showing the bubble window, so, I added it to AutoCAD status bar right before the bubble window is shown and remove it from AutoCAD status bar after the bubble window is closed. If you have other use of the TrayItem, you could add it to AutoCAD status bar in the class' Constructor and not remove it at all.
It is also possible to just loop through AutoCAD.StatusBar.TrayItems collection and find an existing TrayItem and "borrow" it to show your own bubble window.
2. Using AutoCAD InfoCenter
Kean Walmsley posted example of this here. But I'll go ahead with my similar code in class MyAcadInfoCenterBubble that implements IMyMessageBubble anyway:
You noticed I used System.Diagnostics.Process to start browser with given link. For some reason, although the ResultItem has been supplied a valid System.Uri object, and a link shows correctly in the bubble window, however, nothing happens as expected when I clicked it. In the case of AutoCAD status bar bubble window, the the link is set, there is no need to do anything. I am not sure I am doing thing correctly here by having to use System.Diagnostics.Process to make the link in the InfoCenter bubble window to work. I have not tried this code in other version of AutoCAD other than 2012.
As you can see, with InfoCenter bubble window, there is fewer options for us to show/format the message than TrayItemBubbleWindow.
3. Using Windows System Tray Nitification Icon
In one of my previous posts, I used System.Windows.Forms.NotifyIcon to show status of async process status. Here is the version of implementing IMyMessageBubble:
It is the same as the case of AutoCAD status bar's TrayItem that NotifyIcon's Icon property must be set to a valid System.Drawing.Icon object (no wonder the class is called NotifyIcon). Or it will not be able to show a bubble window (BalloonTip).
Among these 3 ways of showing message bubble window, I recommend to use either AutoCAD status bar's TrayItem, or use Windows system tray NotifyIcon. Both of them make it easy to handle user interaction to the bubble window, such as how the bubble window is closed (TrayItemBubbleWindow), or single/double clicks on the bubble window (Window system NotifyIcon), or mouse click (left/right button) on the icon itself of the both.
This video clip shows how the bubble window pops up in each of the 3 ways discussed in this post.