Windows ANE for native dialogs

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.

[Download ANE]
[Download source]

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.
ane1

Once it’s included you can use it as follows:

1
2
3
4
5
import com.nemenvisual.NativeDialog;

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 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
}

// or

switch (result)
{
    case NativeDialog.IDCANCEL:
        // cancel was clicked
    break;
    case NativeDialog.IDRETRY:
        // retry was clicked
    break;
    case NativeDialog.IDCONTINUE:
        // continue was clicked
    break;
}

Methods

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

Constants

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.

Icon constants:

  • 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 constants

  • NativeDialog.OK
  • NativeDialog.OKCANCEL
  • NativeDialog.ABORTRETRYIGNORE
  • NativeDialog.YESNOCANCEL
  • NativeDialog.YESNO
  • NativeDialog.RETRYCANCEL
  • NativeDialog.CANCELTRYCONTINUE

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

[Download ANE]
[Download source]

Posted toActionScript AIR C++ Flash Native Extension