Title: HTML Dialog Boxes
Question: How can you show System HTML Dialog boxes using the ShowHTMLDialog function in the MSHTML.dll library? Is Internet Explorer ver 4 and higher needed for a ShowHTMLDialog( ) function?
Answer:
Where is the ShowHTMLDialog function, in a .pas unit?
=====================================================
No. The ShowHTMLDialog( ) function in the MSHTML.dll library. and you will need to use
hLib := LoadLibrary('MSHTML.DLL');
to access this DLL, , , and use the
ShowHTMLDialog := GetProcAddress(hLib, 'ShowHTMLDialog');
to get the function address for this function.
Only if Internet Explorer version 4 and above is installed, can you use this function.
WHAT DOES A HTML DIALOG BOX LOOK LIKE?
=================================
When ShowHTMLDialog is used, Internet Explorer shows an IE Browser window with only a dialog type Title bar (no menu or tool bars, or status). This browser window will read the HTML in a file, and display it like a browser window. If you need user Input (buttons, Edit, radio buttons), you will use JScript (JavaScript or VBScript) as the code language for functions to read or change these Inputs.
WHAT CODE IS NEEDED TO USE THE HTML DIALOG?
=====================================
Since the HTML Dialog is an Internet Explorer browser window, COM is used to comunicate between your program and IE, with Variants, IMoniker and IHTMLDialog interfaces. And the ShowHTMLDialog function will implement these interfaces for you.
the ShowHTMLDialog fuction is defined below -
function ShowHTMLDialog(
hwndParent: Cardinal; // parent's Handle
UrlMnk: IMoniker; // IMoniker which has the HTML source file
PvarArgIn: PVariantArg; // Variant address that contains PWChar sent to dialog
PWCHOptions: PWChar; // pointer to Wide Char with Dialog Options
PvarArgOut: PVariantArg // Variant address receives the data placed in IHTMLDialog::returnValue
): HRESULT; // function Result is an OLE HRESULT (Integer)
Parameters
hwndParent - Handle to the parent of the dialog box, can be Zero.
UrlMnk - An IMoniker interface containing the URL from which the HTML file for the dialog box is loaded,
file can be in app's resources.
PvarArgIn - Pointer to a VariantArg record that contains the data sent to the dialog box.
The data passed in this VariantArg is placed in the dialog window object's IHTMLDialog::dialogArguments
property. This is mostly set as a VT_BSTR, with a PWChar for text the dialog will use for display.
This parameter can be set as an empty VariantArg (VT_EMPTY).
PWCHOptions - Window options for the dialog box. This parameter can be NIL or the address of a PWChar
that contains a combination of values, each separated by a semicolon (;).
POptions := 'dialogHeight:13;dialogWidth:21;resizable:no;help:no;center:yes'
PvarArgOut - Pointer to a VariantArg record that contains the data sent from the dialog box when
it closes. This VariantArg gets the data that was placed in the dialog window object's
IHTMLDialog::returnValue property. This parameter can be an empty VariantArg.
Result Value - Returns S_OK if successful, or an error value.
-----------------------------------------
The Second parameter in ShowHTMLDialog is an IMoniker type, in order to place data into an IMoniker, you would use the CreateURLMoniker Function in the UrlMon.pas, defined below
function CreateURLMoniker(
MkCtx: IMoniker; // moniker to use as the base context, can be NIL
szURL: PWChar; // contains the text of URL to be parsed
out mk: IMoniker // moniker with the new URL created from MkCtx and szURL.
): HResult; // function Result is an OLE HRESULT (Integer)
This Url string (szURL) needs to be prefixed with the "file://" to signify a disk file location,
or with the "res://" to signify a programs resource location, followed by the path and file name,
and a "/#" then the resource number, like this for file -
UrlStr := 'file://C:\Program Files\HtmlDlg1.htm';
or
UrlStr := 'file://'+ExtractFilePath(ParamStr(0))+'HtmlDlg1.htm';
from resource
UrlStr := 'res://MyProgram.exe/#101';
or
UrlStr := 'res://'+ExtractFileName(ParamStr(0))+'/#101';
==== the #101 is defined in your resource creation .rc file
-----------------------------------------
The Third and Fifth parameters in ShowHTMLDialog is a PVariantArg type, this is a Variant that delphi does not automatically "Decode" for you, so you will need to read or set the variant data type .vt (VarArgs.vt) yourself and then use that data "Type" when using the data in the Variant.
SAMPLE BUTTON CLICK CODE FOR HTML DIALOG
===================================================================
Here is a Button Click that will show an Html Dialog Box. I do not check for Internet Explorer version, since this does not use DHTML or COM objects that are only in higher versions (5, 6, 7 , 8) of IE. This uses a htm file on disk (HtmlDlg1.htm) in the applications folder. A text string (VarArgs.bstrVal) is sent to the dialog with the dialog Title, an Information text string, and text items to put in the Combo Box.
The code for the HtmlDlg1.htm web page is below this button click code.
procedure TForm1.button_FileHtmlDlgClick(Sender: TObject);
type
TShowHTMLDialog = function(hwndParent: Cardinal;
UrlMnk: IMoniker; PvarArgIn: PVariantArg;
PWCHOptions: PWChar; PvarArgOut: PVariantArg): HRESULT; stdcall;
var
hLib2: Integer;
ShowHTMLDialog: TShowHTMLDialog;
URLMoniker: IMoniker;
VarArgs, VarReturn: TVariantArg;
ArugStr, UrlStr, Return: String;
POptions: PWChar;
begin
Return := 'ERROR, IE version is Below 4 or does not support HTML dialogs';
hLib2 := LoadLibrary('MSHTML.DLL');
if hLib2 0 then
try
ShowHTMLDialog := GetProcAddress(hLib2, 'ShowHTMLDialog');
if Assigned(ShowHTMLDialog) then
begin
{the PvarArgIn is used to Pass Data To the HTML Dialog.
The VarArgs Variant will be used here as a PWChar, with the ^ being
used as a Delimiter for the seven sub Strings, that are sent to the Dialog}
ArugStr := 'HTML Dialog Top Title^This is Information text passed to the '+
'DialogType your Name Below^Small^Medium^Large^'+
'My Size^One size fits all';
{the first sub string is the dialog Title, the next substring is the text (and HTML)
that will be written to the html with JScript document.write(ArgArray[1]) ,
notice that I have HTML tags in this Text, this allows you to customize the dialog
from your programs data, all of the sub strings that follow are placed in the Combo Box}
VarArgs.vt := VT_BSTR;
VarArgs.bstrVal := StringToOleStr(ArugStr);
{the UrlStr is prefixed with the file:// to signify a disk file,
use res:// to sigify a resource}
UrlStr := 'file://'+ExtractFilePath(ParamStr(0))+'HtmlDlg1.htm';
{the URLMoniker is set to the URL of the source of the htm file used for
the dialog box, it can be a disk file, a program resource, or an Internet
file address, if the html file is not found, a blank dialog box is displayed}
OLECheck(CreateURLMoniker(nil, StringToOleStr(UrlStr), URLMoniker));
POptions := 'dialogHeight:17;dialogWidth:23;resizable:no;help:no;center:yes';
{POptions can be omitted (set to nil), but it allows you to set some options
of the dialog, like width and height}
VariantInit(OleVariant(VarReturn));
{VariantInit sets the VarReturn.vt to VT_EMPTY}
if ShowHTMLDialog(Handle, URLMoniker, @VarArgs, POptions, @VarReturn) = S_OK then
begin
{the JScript window.returnValue will automatically set the VarReturn.vt to the
data Type that is assigned to it, I only use 2 data types for the
window.returnValue , a JScript text string (VT_BSTR - OLE wide string) and an
integer (VT_I4) , so any other variant data type will indicate an Error}
if VarReturn.vt = VT_BSTR then
begin
Return := VarReturn.bstrVal;
{I use a ^ to delimit the VarReturn.bstrVal, but the first
Char is the Radio Box number, which is below 9, so I do not
delimit it, since it will always be a single Char Return[1]}
Return := 'OK was Clicked'#10'Radio was '+Return[1]+#10+
'Combo1 was '+Copy(Return,2,Pos('^',Return)-2)+
#10'Edit Text was '+Copy(Return,Pos('^',Return)+1, 512);
end else
if VarReturn.vt = VT_I4 then
Return := 'OK was NOT clicked'#10'the VarReturn is '+IntToStr(VarReturn.lVal)
else
Return := 'Variant Data type is not integer or OLE string, ERROR';
end else
Return := 'The ShowHTMLDialog FAILED';
end;
finally
FreeLibrary(hLib2);
end;
ShowMessage(Return);
end;
HTML CODE FOR DIALOG ABOVE
===========================================================================
Below is the HTML code for the web page HtmlDlg1.htm , to be displayed in the HTML Dialog box created by the Delphi code above. To get funtionality from your dialog box (button press, enter text, combo boxes) you will need to use a HTML Script language. If you do not know HTML or any HTML Scripting language (JScript, JavaScript, VBscript), then HTML dialog boxes are not for you.
The window.dialogArguments is your access to Data passed to the Dialog Box. You will usually pass a Wide String or Integer in your VarArgs parameter to the dialog box. If you want more than One Data Item in your VarArgs variant, you can use a Delimited String and the split method in
ArgArray = window.dialogArguments.split("^");
split will use it's single parameter as the delimiter charater, to divide up the string into an Array.
The only data this usage gets from the dialog box comes in the VarReturn variant. If you need more than one data Item returned, you can again use a delimited string, as I have done here.
put dialog window's width and height above --
HTML Dialog Test
var RBnum = 0;
var ArgArray = new Array();
//get the dialog arguments into an array with .split and the delimiter
ArgArray = window.dialogArguments.split("^");
//the ArgArray check below is not nessary, but allows you to have a Default
//dialog box with an empty PvarArgIn from ShowHTMLDialog
if (ArgArray.length == 0) {
ArgArray[0] = "Dialog Title";}
if (ArgArray.length == 1) {
ArgArray[1] = "No Info Text";}
if (ArgArray.length == 2) {
ArgArray[2] = "None";}
//the first Array string is the Title
document.title = ArgArray[0];
//set the default return value
window.returnValue = 0;
function StartUp()
{
//clear selections in Combo1
Combo1.options.length = 0;
//add the ArgArray strings to the Combo1 selections
var index;
index = 2;
// start with index of 2 because the first 2 strings are title and info
while(index {
var tempOption = new Option(ArgArray[index]);
Combo1.options[Combo1.options.length] = tempOption;
index++;
}
//set the first Combo1 selection position
Combo1.options[0].selected = true;
}
function OkClick()
{
//this return value means that the OK button was clicked
window.returnValue = RBnum+Combo1.options[Combo1.selectedIndex].text+"^"+Edit1.value;
//since the window.returnValue is a variant, it cant get text or numbers or objects
//The Integer RBnum is automatically converted to text
//close the dialog window
window.close();
}
function CancelClick()
{
//this -1 return value means that the Cancel button was clicked
window.returnValue = -1;
window.close();
}
function RadioClick(num)
{
RBnum = num;
// check and UnCheck the Radios
RB1.checked = (num == 0);
RB2.checked = (num == 1);
RB3.checked = (num == 2);
}
A HTML Dialog Box
throwing off your sizing.
but you could use style sheet settings to define fonts, colors, borders--
//a very usefull operation - document.write( )
//the second array string is the INFO text written here
document.write(ArgArray[1])
Howdy
Late
Goomba
Pick your Size -