ShowHtmlDialog

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

ShowHtmlDialog

16 Aug 2017, 19:01

I found this neat script on the archived forum, credit to Lexikos, and I tried removing the COM.ahk stuff to work with 1.1+.

It functions as long as you don't pass any Flags. Haven't figured that out yet. Also, it seems the documentMode is always stuck at 5 regardless of the X-UA-Compatible meta tag.

For what it's worth, thought I'd share here.

Code: Select all

/* Flags: Search MSDN for ShowHTMLDialogEx.
#define HTMLDLG_NOUI                     0x0010 // invisible
#define HTMLDLG_MODAL                    0x0020 // normal behaviour
#define HTMLDLG_MODELESS                 0x0040 // returns immediately
#define HTMLDLG_PRINT_TEMPLATE           0x0080
#define HTMLDLG_VERIFY                   0x0100 // force into viewable portion of desktop
#define HTMLDLG_ALLOW_UNKNOWN_THREAD     0x0200 // IE7+
*/

/*  Options: one or more of the following semicolon-delimited values:
dialogHeight:sHeight
    Sets the height of the dialog window.
    Valid unit-of-measure prefixes: cm, mm, in, pt, pc, em, ex, px
dialogLeft:sXPos
    Sets the left position of the dialog window relative to the upper-left
    corner of the desktop.
dialogTop:sYPos
    Sets the top position of the dialog window relative to the upper-left
    corner of the desktop.
dialogWidth:sWidth
    Sets the width of the dialog window.
    Valid unit-of-measure prefixes: cm, mm, in, pt, pc, em, ex, px
center:{ yes | no | 1 | 0 | on | off }
    Specifies whether to center the dialog window within the desktop. Default: yes
dialogHide:{ yes | no | 1 | 0 | on | off }
    Specifies whether the dialog window is hidden when printing or using print
    preview. Default: no
edge:{ sunken | raised }
    Specifies the edge style of the dialog window. Default: raised
resizable:{ yes | no | 1 | 0 | on | off }
    Specifies whether the dialog window has fixed dimensions. Default: no
scroll:{ yes | no | 1 | 0 | on | off }
    Specifies whether the dialog window displays scrollbars. Default: yes
status:{ yes | no | 1 | 0 | on | off }
    Specifies whether the dialog window displays a status bar. Default: no
unadorned:{ yes | no | 1 | 0 | on | off }
    Specifies whether the dialog window displays the window border.
*/

html =
(
<!DOCTYPE html>
<HEAD>
    <meta http-equiv='X-UA-Compatible' content='IE=9'>
    <TITLE>Example Dialog</TITLE>
</HEAD>
<BODY>
  <INPUT TYPE='text' ID="textbox"><BR>
  <BUTTON ID="okButton">Okay</BUTTON><BR>
  <SCRIPT FOR="window" EVENT="onload">
    textbox.value = dialogArguments.DefaultText;
    textbox.select();
  </SCRIPT>
  <SCRIPT FOR="okButton" EVENT="onclick">
    window.returnValue = textbox.value;
    window.close();
  </SCRIPT>
  <SCRIPT>
    document.getElementById('textbox').oninput = function(e) {
        dialogArguments.my_func(e.target.value);
    }
  </SCRIPT>
</BODY>
)

args := {}
args.HTML := html
args.my_func := Func("my_func")
args.DefaultText := "default text!"

text := ShowHTMLDialog("javascript:dialogArguments.HTML", args, "dialogWidth:150px;dialogHeight:50px")
if (text)
    MsgBox % text

my_func(e) {
    MsgBox, % e
}

ShowHTMLDialog(URL, dialogArguments="", Options="", hwndParent=0, Flags=0)
{    
    hinstMSHTML := DllCall("LoadLibrary","str","MSHTML.DLL")
    hinstUrlMon := DllCall("LoadLibrary","str","urlmon.dll") ; necessary to keep the URL moniker in memory.
    
    if !hinstMSHTML or !hinstUrlMon
        goto ShowHTMLDialog_Exit
    
    hr := DllCall("urlmon\CreateURLMonikerEx","uint",0,"uint",&Url,"uint*",pUrlMoniker,"uint",1)
    if (ErrorLevel) {
        Error = DllCall(CreateURLMoniker)--%ErrorLevel%
        goto ShowHTMLDialog_Exit
    }
    if (hr or !pUrlMoniker) {
        Error = CreateURLMoniker--%hr%
        goto ShowHTMLDialog_Exit
    }

    pOptions := Options ? &Options : 0
    
    VarSetCapacity(varArgIn,16,0), VarSetCapacity(varResult,16,0)

    if (dialogArguments is Integer || IsObject(dialogArguments))
    {
        NumPut(IsObject(dialogArguments) ? 9 : 20,varArgIn,0)
        NumPut(&dialogArguments,varArgIn,8)
    } else {    ; string
        NumPut(8,varArgIn,0)
        NumPut(&dialogArguments,varArgIn,8)
    }

    if Flags
        hr := DllCall("mshtml\ShowHTMLDialogEx","uint",hwndParent,"uint",pUrlMoniker,"uint",Flags,"uint",&varArgIn,"uint",pOptions,"uint",&varResult)
    else
        hr := DllCall("mshtml\ShowHTMLDialog","uint",hwndParent,"uint",pUrlMoniker,"uint",&varArgIn,"uint",pOptions,"uint",&varResult)
    if (ErrorLevel) {
        Error = DllCall(ShowHTMLDialog)--%ErrorLevel%
        goto ShowHTMLDialog_Exit
    }
    if (hr) {
        Error = ShowHTMLDialog--%hr%
        goto ShowHTMLDialog_Exit
    }
    ; based on a line from COM_Invoke(). returnValue = varResult as string;
    InStr(" 0 4 5 6 7 14 "," " . NumGet(varResult,0,"Ushort") . " ") ? DllCall("oleaut32\VariantChangeTypeEx","Uint",&varResult,"Uint",&varResult,"Uint",0,"Ushort",0,"Ushort",8) : "", NumGet(varResult,0,"Ushort")=8 ? returnValue := StrGet(NumGet(varResult,8)) : returnValue := NumGet(varResult,8)
    
ShowHTMLDialog_Exit:    
    ; "Each process maintains a reference count for each loaded library module.
    ;  This reference count is incremented each time LoadLibrary is called and
    ;  is decremented each time FreeLibrary is called."
    ; -- So FreeLibrary() will not unload these DLLs if they were loaded before
    ;    the function was called. :)
    if hinstMSHTML
        DllCall("FreeLibrary","uint",hinstMSHTML)
    if hinstUrlMon
        DllCall("FreeLibrary","uint",hinstUrlMon)
    
    ErrorLevel := Error
    return returnValue
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 144 guests