SRL Archive - Research & Development Lounge

MessageBox


The_Scripts

2009-08-24 06:30:19

Hi Everyone,

I have been searching for a way to call an "editable" MessageBox in SCAR for quite sometime now.

I don't recall every seeing a function on SRL that calls a MessageBox, so I decided to try and find a way to do so.

I noticed that if you use SCAR's GetApplication function it returns the TScarApp Object. This Object then has a MessageBox function. The MessageBox function itself is not thread-safe, so I made a function that can call it in a thread-safe manor.

Here are the functions that I have come up with.
MessageBoxUnstable: Just uses the MessageBox function. This causes SCAR to crash sometimes.
MessageBox: Calls the MessageBoxUnstable function in a thread-safe way.
MessageBoxEx: Same as MessageBox, but I tried to make it a bit more user friendly, in terms of changing the MessageBox Icon.


{*******************************************************************************
function MessageBoxUnstable(Text, Caption : String; Flags : Integer) : Integer;
By: The_Scripts
Description: Displays a MessageBox to the user. IS NOT THREAD-SAFE.
*******************************************************************************}

function MessageBoxUnstable(Text, Caption : String; Flags : Integer) : Integer;
begin
Result := GetApplication.MessageBox(Text, Caption, Flags);
end;



{*******************************************************************************
function MessageBox(Text, Caption : String; Flags : Integer) : Integer;
By: The_Scripts
Description: Displays a MessageBox to the user. IS THREAD-SAFE.

Parameters.
Text: Text to be displayed within the MessageBox.
Caption: Text to be displayed as the MessageBox title.
Flags: Specifies the type of MessageBox to display.
Buttons displayed are based on flag values.
0: OK
1: OK, Cancel
2: Abort, Retry, Ignore
3: Yes, No, Cancel
4: Yes, No
5: Retry, Cancel
6: Cancel, Try Again, Continue
Images displayed are based on flag values.
0-6: None
16-22: Error
32-38: Question
48-54: Warning
64-70: Information

Return values are based on which button is pressed.
1: OK
2: Cancel
3: Abort
4: Retry
5: Ignore
6: Yes
7: No
10: Try Again
11: Continue
*******************************************************************************}

function MessageBox(Text, Caption : String; Flags : Integer) : Integer;
var
Parameters : TVariantArray;
begin
SetArrayLength(Parameters, 3)
Parameters[0] := Text;
Parameters[1] := Caption;
Parameters[2] := Flags;
Result := ThreadSafeCall('MessageBoxUnstable', Parameters);
end;



{*******************************************************************************
function MessageBoxEx(Text, Caption : String; Kind, Icon : Integer) : Integer;
By: The_Scripts
Description: Displays a MessageBox to the user. IS THREAD-SAFE.

Parameters.
Text: Text to be displayed within the MessageBox.
Caption: Text to be displayed as the MessageBox title.
Kind: Specifies the type of MessageBox to display.
0: OK
1: OK, Cancel
2: Abort, Retry, Ignore
3: Yes, No, Cancel
4: Yes, No
5: Retry, Cancel
6: Cancel, Try Again, Continue
Icon:
0: None
1: Error
2: Question
3: Warning
4: Information

Return Values. (Each button has a different return value)
1: OK
2: Cancel
3: Abort
4: Retry
5: Ignore
6: Yes
7: No
10: Try Again
11: Continue
*******************************************************************************}

function MessageBoxEx(Text, Caption : String; Kind, Icon : Integer) : Integer;
var
Parameters : TVariantArray;
begin
if (Kind < 0) or (Kind > 6) then
begin
WriteLn('Kind is invalid.');
Result := -1;
Exit;
end;
if (Icon < 0) or (Icon > 4) then
begin
WriteLn('Icon is invalid.');
Result := -1;
Exit;
end;
SetArrayLength(Parameters, 3)
Parameters[0] := Text;
Parameters[1] := Caption;
Parameters[2] := Kind + (Icon * 16);
Result := ThreadSafeCall('MessageBoxUnstable', Parameters);
end;

Please feel free to comment or ask questions.


EDIT: I have Simplified MessageBoxEx to make it even more user friendly.


function MessageBoxEx2(Text,Caption:String;Buttons:(OK,OKCancel,AbortRetryIgnore,YesNoCancel,YesNo,RetryCancel,CancelTryAgainContinue);Icon:(None,Error,Question,Warning,Information)):Integer;
var
Parameters:TVariantArray;
begin
SetLength(Parameters,3);
Parameters[0]:=Text;
Parameters[1]:=Caption;
Parameters[2]:=Ord(Buttons)+(Ord(Icon)*16);
Result:=ThreadSafeCall('MessageBoxUnstable',Parameters);
end;

EvilChicken!

2009-08-24 09:27:29

Looking good, though I don't see the point of MsgBoxUnstable.

Besides, why not just use ShowMessage(S);?

The_Scripts

2009-08-28 06:03:17

The procedure "MsgBoxUnstable" calls the MessageBox method from the TScarApp Object.
The MessageBox function must be called in a thread-safe way.
In order to do so, I needed to have it contained in a function that I could call using the function "ThreadSafeCall(ProcName: string; var V: TVariantArray): Variant".

From my understanding "ShowMessage" does not allow for the Caption and the Icon in the MessageBox to be changed.

Also, sorry for not replying sooner. My DSL modem died, and it took awhile for my ISP to get off there asses, and give me another…