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:
Code Snippet
- namespace MessageBubbles
- {
- public interface IMyMessageBubble
- {
- void ShowBubbleWndow(
- string title,
- string message1,
- string message2="",
- string linkText="",
- string linkUrl="",
- string acadCommand="");
- }
- }
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:
Code Snippet
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(MessageBubbles.MyCommands))]
- namespace MessageBubbles
- {
- public class MyCommands
- {
- private static IMyMessageBubble _bubble = null;
- [CommandMethod("Hello")]
- public static void HelloWorld()
- {
- Document dwg = Application.DocumentManager.MdiActiveDocument;
- Editor ed = dwg.Editor;
- ed.WriteMessage("\nHELLO WORLD!\n");
- }
- [CommandMethod("InfoCenterBubble")]
- public static void ShowInfoCenterBubble()
- {
- _bubble = new MyAadInfoCenterBubble();
- _bubble.ShowBubbleWndow(
- title: "My Message from InfoCenter",
- message1: "",
- message2: "",
- linkText: "Go to Google",
- linkUrl: "http://www.google.com",
- acadCommand: "Hello ");
- }
- [CommandMethod("StatusBarBubble")]
- public static void ShowTrayItemBubble()
- {
- _bubble = new MyAcadStatusBarBubble();
- _bubble.ShowBubbleWndow(
- title: "My Message from Status Bar",
- message1: "This message is important",
- message2: "Your AutoCAD has been instructed to show " +
- "bubble window at its status bar. " +
- "Go to Google for more details",
- linkText: "Go to Google",
- linkUrl: "http://www.google.com",
- acadCommand:"Hello ");
- }
- [CommandMethod("SystemTrayBubble")]
- public static void ShowSystemTrayBubble()
- {
- _bubble = new MyWindowSystemTrayBubble();
- _bubble.ShowBubbleWndow(
- title: "My Message from Window System Tray",
- message1: "This is a message from Google website.",
- message2: "",
- linkText: "Google",
- linkUrl: "http://www.google.com",
- acadCommand: "Hello ");
- }
- }
- }
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:
Code Snippet
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.Windows;
- namespace MessageBubbles
- {
- public class MyAcadStatusBarBubble : IMyMessageBubble
- {
- TrayItem _item;
- TrayItemBubbleWindow _bubble;
- string _commandOnLinkClick = "";
- public MyAcadStatusBarBubble()
- {
- _item = new TrayItem();
- _item.Icon = System.Drawing.SystemIcons.Exclamation;
- _item.ToolTipText = "My bubble window item";
- }
- public void ShowBubbleWndow(
- string title,
- string message1,
- string message2="",
- string linkText="",
- string linkUrl="",
- string acadCommand="")
- {
- _commandOnLinkClick = acadCommand;
- //Add tray item to status bar
- Application.StatusBar.TrayItems.Add(_item);
- //Create TrayItemBubbleWindow object
- _bubble = new TrayItemBubbleWindow();
- _bubble.IconType = IconType.Information;
- _bubble.Title = title;
- _bubble.Text = message1;
- if (!String.IsNullOrEmpty(message2)) _bubble.Text2 = message2;
- if (!String.IsNullOrEmpty(linkText) &&
- !String.IsNullOrEmpty(linkUrl))
- {
- _bubble.HyperText = linkText;
- _bubble.HyperLink = linkUrl;
- }
- _bubble.Closed +=
- new TrayItemBubbleWindowClosedEventHandler(_bubble_Closed);
- //Show bubble window
- _item.ShowBubbleWindow(_bubble);
- Application.StatusBar.Update();
- }
- private void _bubble_Closed(object sender,
- TrayItemBubbleWindowClosedEventArgs e)
- {
- Document dwg = Application.DocumentManager.MdiActiveDocument;
- if (e.CloseReason ==
- TrayItemBubbleWindowCloseReason.HyperlinkClicked)
- {
- if (!String.IsNullOrEmpty(_commandOnLinkClick))
- {
- dwg.SendStringToExecute(
- _commandOnLinkClick, true, false, true);
- }
- }
- else
- {
- dwg.Editor.WriteMessage(
- "\nStatus Bar bubble window closed due to {0}\n",
- e.CloseReason.ToString());
- }
- Application.StatusBar.TrayItems.Remove(_item);
- _bubble.Dispose();
- _bubble = null;
- }
- }
- }
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:
Code Snippet
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.Internal.InfoCenter;
- using Autodesk.AutoCAD.AcInfoCenterConn;
- namespace MessageBubbles
- {
- public class MyAadInfoCenterBubble: IMyMessageBubble
- {
- private string _url="";
- private string _command = "";
- public void ShowBubbleWndow(
- string title,
- string message1,
- string message2 = "",
- string linkText = "",
- string linkUrl = "",
- string acadCommand = "")
- {
- if (!String.IsNullOrEmpty(linkUrl))
- {
- _url = linkUrl;
- }
- _command = acadCommand;
- InfoCenterManager icm = new InfoCenterManager();
- ResultItem ri = new ResultItem();
- ri.Category = title;
- ri.Title = linkText;
- ri.Uri =new System.Uri(linkUrl);
- ri.IsFavorite = true;
- ri.IsNew = true;
- ri.ResultClicked +=
- new EventHandler<ResultClickEventArgs>(ri_ResultClicked);
- icm.PaletteManager.ShowBalloon(ri);
- }
- void ri_ResultClicked(object sender, ResultClickEventArgs e)
- {
- Document dwg = Application.DocumentManager.MdiActiveDocument;
- dwg.Editor.WriteMessage("\nInfo center is clicked\n");
- if (_url.Length>0)
- {
- System.Diagnostics.ProcessStartInfo proc =
- new System.Diagnostics.ProcessStartInfo();
- proc.FileName = _url;
- System.Diagnostics.Process.Start(proc);
- }
- if (!String.IsNullOrEmpty(_command))
- {
- dwg.SendStringToExecute(_command, true, false, true);
- }
- }
- }
- }
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:
Code Snippet
- using System;
- using System.Windows.Forms;
- using Autodesk.AutoCAD.ApplicationServices;
- namespace MessageBubbles
- {
- public class MyWindowSystemTrayBubble : IMyMessageBubble
- {
- private NotifyIcon _bubble = null;
- private string _url = "";
- private string _command;
- public void ShowBubbleWndow(
- string title,
- string message1,
- string message2 = "",
- string linkText = "",
- string linkUrl = "",
- string acadCommand = "")
- {
- _url = linkUrl;
- _command = acadCommand;
- _bubble = new NotifyIcon();
- _bubble.Icon = System.Drawing.SystemIcons.Exclamation;
- _bubble.BalloonTipIcon = ToolTipIcon.Info;
- _bubble.BalloonTipTitle = title;
- _bubble.BalloonTipText =
- message1 + "\n\nClick to go to " + linkText;
- _bubble.BalloonTipClicked +=
- new EventHandler(_bubble_BalloonTipClicked);
- _bubble.BalloonTipClosed +=
- new EventHandler(_bubble_BalloonTipClosed);
- _bubble.Visible = true;
- _bubble.ShowBalloonTip(5000);
- }
- private void _bubble_BalloonTipClosed(object sender, EventArgs e)
- {
- if (_bubble != null)
- {
- _bubble.Dispose();
- _bubble = null;
- }
- }
- private void _bubble_BalloonTipClicked(object sender, EventArgs e)
- {
- if (_url != null)
- {
- System.Diagnostics.ProcessStartInfo proc =
- new System.Diagnostics.ProcessStartInfo();
- proc.FileName = _url;
- System.Diagnostics.Process.Start(proc);
- }
- if (!String.IsNullOrEmpty(_command))
- {
- Document dwg = Autodesk.AutoCAD.ApplicationServices.
- Application.DocumentManager.MdiActiveDocument;
- dwg.SendStringToExecute(
- _command, true, false, true);
- }
- _bubble.Dispose();
- _bubble = null;
- }
- }
- }
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.