Windows ANE for native dialogs
9 years ago
My post on creating native alerts with Adobe AIR has proven to be quite popuplar so I decided to follow it up with something even better (at least for Windows apps).
Recently I have been dabbling in a bit of C# for creating Windows Store apps as well as some C/C++ to try and figure out how to create Native Extensions (ANE) for Adobe AIR. As a first ANE I decided to make a native dialog extension. Extensions for this already exist for iOS and Android but I was unable to find any for Windows.
For anyone wanting to make an ANE for windows I recommend this tutorial which runs through everything from setting up a Visual Studio project to compiling the .ANE file. I found it very useful. So here is my first ANE. You can download the compiled the ANE below or the source files with the Visual Studio project and AS files. Read on for more details.
It should be pretty straight forward to use. First you need to include the ANE in your project. In Flash Professional you can go to ActionScript settings, click on the Library Path tab then click the Browse to Native Extension button.
Once it’s included you can use it as follows:
var ane:NativeDialog = new NativeDialog();
// displays a simple alert with an OK button
ane.alert("Alert title","This is an alert dialog.");
As you can see an advantage of using this ANE over my other solution using the HTMLLoader is that you can specify a title for the message box.
You can also use the customDialog method to specify the buttons you want to use and the icon to be displayed in the box.
// displays a dialog with Cancel, Try Again and Continue buttons and an exclamation icon.
var result = ane.customDialog("Custom alert","The message in the custom alert.",NativeDialog.CANCELTRYCONTINUE,NativeDialog.ICONEXCLAMATION);
if (result == NativeDialog.IDRETRY)
// the retry button was clicked
// cancel was clicked
// retry was clicked
// continue was clicked
There a five types of dialogs you can show. Currently there is no ‘prompt’ equivalent for user input.
- alert(title:String, message:String) – displays simple alert with an OK button and an exclamation icon
- confirm(title:String, message:String) – displays a dialog with Yes/No buttons and an exclamation icon
- returns true/false for yes/no respectively
- error(title:String, message:String) – displays a dialog with an OK button and a red cross icon
- info(title:String, message:String) – displays a dialog with an OK button and an info (i) icon
- custom(title:String, message:String, icon:int, buttons:int) – displays a dialog with specified icon and buttons – see icon and button constants below
- returns an int corresponding to the button id clicked – see button constants below
The NativeDialog ANE implements most of the constants specified on the MSDN MessageBox page except where there are duplicates (for example MB_ICONEXCLAMATION and MB_ICONWARNING will show the same icon). I will add a modality option in the future to enable non-blocking dialogs.
- NativeDialog.ICONEXCLAMATION – yellow triangle with exclamation mark
- NativeDialog.ICONINFORMATION – blue circle with an i
- NativeDialog.ICONQUESTION – blue circle with a question mark (not recommended as this is now deprecated on Windows)
- NativeDialog.ICONERROR – red circle with a cross
- NativeDialog.ICONNONE – no icon
Button IDs (returned by customDialog)
- NativeDialog.IDABORT – 3
- NativeDialog.IDCANCEL – 2
- NativeDialog.IDCONTINUE – 11
- NativeDialog.IDIGNORE – 5
- NativeDialog.IDNO – 7
- NativeDialog.DOK – 1
- NativeDialog.IDRETRY – 4
- NativeDialog.IDTRYAGAIN – 10
- NativeDialog.IDYES – 6