Delphi7 – MessageBox

program MsgBoxExample; //program name

uses
  SysUtils, //SysUtils module
  Windows,  //Windows module
  Messages; //Messages module

const
  sCaption = 'MsgBox'; //our const sCaption
var
  iReturnedValue : Integer; //our variable iReturnedValue (Integer)
begin
{
  MessageBox function creates, shows, operates (etc. :P) Message Box.
  You can use it to informate user about any errors, informations etc.
  It's very useful function in Windows Programming.

  MessageBox function returns following values:
  0 - out of memory
  1 - OK button clicked (IDOK)
  2 - Cancel button clicked (IDCANCEL)
  3 - Abort button clicked (IDABORT)
  4 - Retry button clicked (IDRETRY)
  5 - Ignore button clicked (IDIGNORE)
  6 - Yes button clicked (IDYES)
  7 - No button clicked  (IDNO)

  For example:
}

  iReturnedValue := MessageBoxA(0,'Choose "Yes" or "No" :)',sCaption,MB_YESNO);
  case iReturnedValue of
  6  :
      begin
        MessageBox(0,'You clicked "Yes"!!!',sCaption,0);
      end;

  7  :
      begin
        MessageBox(0,'You clicked "No"!!!',sCaption,0);
      end;
  end;

{
  Now I must tell you something about buttons and icons in our MsgBoxes.
  Buttons:
  MB_OK = $0
  MB_OKCANCEL = $1
  MB_ABORTRETRYIGNORE = $2
  MB_YESNOCANCEL = $3
  MB_YESNO = $4
  MB_RETRYCANCEL = $5
  So, you can use 0 or MB_OK, 4 or MB_YESNO etc.

  Above it's only general consts.
  There is more:
  MB_OK = $0
  MB_OKCANCEL = $1
  MB_ABORTRETRYIGNORE = $2
  MB_YESNOCANCEL = $3
  MB_YESNO = $4
  MB_RETRYCANCEL = $5
  MB_ICONHAND = $10
  MB_ICONQUESTION = $20
  MB_ICONEXCLAMATION = $30
  MB_ICONASTERISK = $40
  MB_USERICON	= $80
  MB_ICONERROR = MB_ICONHAND
  MB_ICONINFORMATION = MB_ICONASTERISK
  MB_ICONSTOP = MB_ICONHAND
  MB_ICONWARNING = MB_ICONEXCLAMATION
  MB_DEFBUTTON1 = $0
  MB_DEFBUTTON2 = $100
  MB_DEFBUTTON3 = $200
  MB_DEFBUTTON4 = $300
  MB_APPLMODAL = $0
  MB_SYSTEMMODAL = $1000
  MB_TASKMODAL = $2000
  MB_HELP	= $4000
  MB_NOFOCUS = $8000
  MB_SETFOREGROUND = $10000
  MB_DEFAULT_DESKTOP_ONLY = $20000
  MB_TYPEMASK = $0F
  MB_ICONMASK = $0F0
  MB_DEFMASK = $0F00
  MB_MODEMASK = $3000
  MB_MISCMASK = $0C000
  MB_TOPMOST = $40000
  MB_RIGHT = $80000
  MB_RTLREADING = $100000
  MB_SERVICE_NOTIFICATION = $200000
  Huh :)

  Little example:
}

  MessageBox(0,'Delphi lernen macht Spaß :)',sCaption,$3+$30+$100);

{
  In the example below we use:
  $3 = MB_YESNOCANCEL
  $30 = MB_ICONEXCLAMATION
  $100 = MB_DEFBUTTON2

  1. MB_YESNOCANCEL - our buttons is "Yes", "No", "Cancel".
  2. MB_ICONEXCLAMATION - our icon is warning icon
  3. MB_DEFBUTTON2 - default button, second btn will be "highlighted".

  But code with consts is more readable:
}
  MessageBox(0,'Simple Text ;)',sCaption,MB_YESNOCANCEL+MB_ICONEXCLAMATION+MB_DEFBUTTON2);

end. //end of the code
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: