Toolbar templates

28 Feb, 2002 By: Barry Bowen

Add custom toolbars to your AutoLISP program interfaces.

Toolbars are an essential part of any custom program or application you write. They provide an efficient and friendly interface for users to access your program. They also keep your program separate from AutoCAD's menus.


Download the Lessons in Programming code. The file name is MAR02.exe.

Creating a toolbar template is simple. Once you have a template, you can quickly add toolbars later for your program's interface. However, don't use AutoCAD's Customize button in the Toolbars dialog box to make your toolbars. You must keep your menu system separate from AutoCAD's by creating your own partial toolbar menu files.

Here's how
To get started, use a text editor such as Notepad to create a new file named TOOLBAR.MNS in your programming folder. This serves as the toolbar template for all the partial toolbar menus you create. Note that we're using the MNS extension, not the MNU extension.

In the text editor, type the lines in the box below into the open file, then save it for use later. Replace the items in italic with your own. See the next section for details on how to create your own custom toolbar icons.

Next, use the text editor to create a file that loads an AutoLISP application menu. Save this file as TOOLBAR.MNL. It contains the following lines:
(setq MYPATH c:/myprogram/)
(load (strcat MYPATH MYPROGRAM ))

When AutoCAD loads the partial menu toolbar, the MNL file of the same name loads and executes, setting the path (MYPATH) and loading the AutoLISP program (MYPROGRAM).

Figure 1. Smiley faces indicate missing bitmaps.

Create your icons
You can create image bitmaps for the toolbar icons in the AutoCAD toolbar editor or in other programs such as Microsoft Paint. Small bitmap images measure 1615 pixels in size, which AutoCAD 2000 stretches when Large buttons are on. Be sure to store the image files in the same folder as the menu file and add the path in AutoCAD. Loading the menu file without letting AutoCAD know where the bitmap images are results in a toolbar with smiley faces, as shown in figure 1.

I see smiley faces
Smiling faces in your toolbar indicate that AutoCAD could not find the icon bitmaps. To correct this, make sure you added the path to the bitmaps. In AutoCAD, select Tools | Options from the pull-down menu. On the File tab click the plus sign (+) before the Support File Search Path. Click the Add button, then Browse to locate the folder that holds the program bitmaps.

Select OK, then close the dialog box and AutoCAD. Open Windows Explorer and locate the folder where you stored the menu and image files. Delete the MNC and the MNR menu files. When you start AutoCAD again, the toolbar icons should appear correctly.

Because of the limited space for this article, I have included a sample program and toolbar to further illustrate how to apply the toolbar template to a sample custom program. You can download all the related code.

Code for Toolbar.MNS


;;;note:replace items in italic with your own names
[_Toolbar(My Program ,_Floating,_Show,742,121,1)]
[_Button(Name1 ,image1.bmp ,image1.bmp )]^C ^C
[_Button(Name2 ,image2.bmp ,image2.bmp )]^C ^C
[_Button(Name3 ,image3.bmp ,image3.bmp )]^C ^C
[_Button(Name4 ,image4.bmp ,image4.bmp )]^C ^C
[_Flyout(Flyout1 ,image5.bmp ,image5.bmp , _OtherIcon,FLYOUT1.MYPROGRAM)]
[_Toolbar(Flyout1 ,_Floating,_Show,742,121,1)]
[_Button(Name5 ,image5.bmp ,image5.bmp )]^C ^C
[_Button(Name6 ,image6.bmp ,image6.bmp )]^C ^C


More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!

For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!