Windows MessageBoxA function with flags. (win32)

Previous topic - Next topic

Hemlos

This is for win32 only.
You can control most of the aspects of a windows message box with these defined flags.
I included the return code definitions too.  :rtfm:

Notes:
A) The help button doesn't return anything in GLBasic.
B) You can use your own icon, however, i'm not sure how to define it.

Code (glbasic) Select
// --------------------------------- //
// Project: MessageBoxA function (winuser.h) - win32
// Start: Wednesday, June 14, 2023
// IDE Version: 15.238
// Coded by: Neil Silver AKA Hemlos
//
// ----------------------
//
// Resource:
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxa
//
//  Displays a modal dialog box that contains a system icon,
//  a set of buttons, and a brief application-specific message,
//  such as status or error information.
//  The message box returns an integer value that indicates which button the user clicked.
//
// ----------------------
//
// Note:
// There is an L (long) appended at the end of each code constant on this webpage.
// Omitting L gets it working in GLBasic. :D
//
// ----------------------
//
// Note:
// On the webpage example, you will notice the '|' indicator between the flag codes.
// This is a concatenation in C++
// In GLBasic use '+' to concatenate the flags, see example below.
//
// ----------------------
//
// Note:
// The dialog can disappear behind the main program window if you do not specify modality.
// All modalities require the dialog be responded to in order for the program to continue.
// Append MB_TASKMODAL or MB_SYSTEMMODAL or MB_TOPMOST to the flags to prevent this dialog from losing focus.
//
// ----------------------
//
// Example:
//
//    LOCAL Title$ = "Test Title"
//    LOCAL Body$  = "Test Body"
//    LOCAL Flags% = MB_ICONEXCLAMATION + MB_CANCELTRYCONTINUE + MB_DEFBUTTON2 + MB_TASKMODAL
//    LOCAL MessageOutput% = MessageBoxA(0, Body$, Title$, Flags )
//
//    SELECT MessageOutput%
//        CASE IDABORT; // Add code
//        CASE IDCANCEL;; // Add code
//        CASE IDCONTINUE;; // Add code
//        CASE IDIGNORE;; // Add code
//        CASE IDNO;; // Add code
//        CASE IDOK;; // Add code
//        CASE IDRETRY;; // Add code
//        CASE IDTRYAGAIN;; // Add code
//        CASE IDABORT;; // Add code
//        DEFAULT; // 0 = IDFAILED
//    ENDSELECT

    // ====================================================== //
    // MessageBoxA function (winuser.h) - win32
    IMPORT "C" int __stdcall MessageBoxA(int hwnd, const char* text, char* caption, int flags );
   
    // To indicate the buttons displayed in the message box, specify one of the following values.
    CONSTANT MB_ABORTRETRYIGNORE     = 0x00000002
    CONSTANT MB_CANCELTRYCONTINUE    = 0x00000006
    CONSTANT MB_HELP                 = 0x00004000 // This button doesn't seem to work in GLBasic, it shows, yet no return.
    CONSTANT MB_OK                   = 0x00000000
    CONSTANT MB_OKCANCEL             = 0x00000001
    CONSTANT MB_RETRYCANCEL          = 0x00000005
    CONSTANT MB_YESNO                = 0x00000004
    CONSTANT MB_YESNOCANCEL          = 0x00000003

    // To display an icon in the message box, specify one of the following values.
    CONSTANT MB_ICONEXCLAMATION      = 0x00000030 //  /!\
    CONSTANT MB_ICONINFORMATION      = 0x00000040 //  (i)
    CONSTANT MB_ICONQUESTION         = 0x00000020 //  (?)
    CONSTANT MB_ICONSTOP             = 0x00000010 //  (X)
    CONSTANT MB_USERICON             = 0x00000080 // user icon, unused
   
    // To indicate the default button, specify one of the following values.
    CONSTANT MB_DEFBUTTON1           = 0x00000000
    CONSTANT MB_DEFBUTTON2           = 0x00000100
    CONSTANT MB_DEFBUTTON3           = 0x00000200
    CONSTANT MB_DEFBUTTON4           = 0x00000300
   
    // To indicate the modality of the dialog box, specify one of the following values.
    CONSTANT MB_APPLMODAL            = 0x00000000
    CONSTANT MB_SYSTEMMODAL          = 0x00001000
    CONSTANT MB_TASKMODAL            = 0x00002000
   
    // To specify other options, use one or more of the following values.
    CONSTANT MB_DEFAULT_DESKTOP_ONLY = 0x00020000
    CONSTANT MB_RIGHT                = 0x00080000
    CONSTANT MB_RTLREADING           = 0x00100000
    CONSTANT MB_SETFOREGROUND        = 0x00010000
    CONSTANT MB_TOPMOST              = 0x00040000

    //Return:
    CONSTANT IDFAILED   = 0    // Function Failed
    CONSTANT IDABORT    = 3    // The Abort button was selected.
    CONSTANT IDCANCEL   = 2    // The Cancel button was selected.
    CONSTANT IDCONTINUE = 11   // The CONTINUE button was selected.
    CONSTANT IDIGNORE   = 5    // The Ignore button was selected.
    CONSTANT IDNO       = 7    // The No button was selected.
    CONSTANT IDOK       = 1    // The OK button was selected.
    CONSTANT IDRETRY    = 4    // The Retry button was selected.
    CONSTANT IDTRYAGAIN = 10   // The TRY Again button was selected.
    CONSTANT IDYES      = 6    // The YES button was selected..
    CONSTANT IDHELP     = 9    // Help Button pushed. Not GLBasic. \o/

Bing ChatGpt is pretty smart :O

bigsofty

Nice GLBasic import win32 api call example!  :good:
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)