I tried to make DHTMLEDiT control using your EASY COM.
I managed to make poor one.(expecting a lot of criticism)
It's absolutely crude but at least I can edit html in WYSIWYG way.
But the problem is that some method is working only in my main script(Test_DHTmlEdit.ahk) not from the DE_control.ahk.
I put comments in the script where there are problems.
And when I made GUI I used Sean's AtlAX kind Functions.
They are great functions so can you consider adding these functions?
To add those functions I had to include "Cohelper.ahk", it's not a problem for me, but the problem is your function name and cohelper's function name conflict specifically "CreateObject".
So I had to Edit the original cohelper.ahk(just removed only createObject function).
DownLoad All Packed File
http://blogfile.paran.com/BLOG_387244/200707/1184002338_DhtmlEdit_Test.zip
including easycom.dll, easycom.ahk, cohelper_ed.ahk(only CreateObject removed), DE_control.ahk, and test demo script)
DE_control.ahk
Code:
; it can be used to display and edit html in a visual way.(WYSIWYG)
; it's only my try to understand COM, not intended for final use 'cause easycom is still under development.
; Of course, they are working but I really hope that somebody has these functions wrapped.
; ill-structured and still a lot left to do.
; Command IDs
DECMD_BOLD = 5000
DECMD_COPY = 5002
DECMD_CUT = 5003
DECMD_DELETE = 5004
DECMD_DELETECELLS = 5005
DECMD_DELETECOLS = 5006
DECMD_DELETEROWS = 5007
DECMD_FINDTEXT = 5008
DECMD_FONT = 5009
DECMD_GETBACKCOLOR = 5010
DECMD_GETBLOCKFMT = 5011
DECMD_GETBLOCKFMTNAMES = 5012
DECMD_GETFONTNAME = 5013
DECMD_GETFONTSIZE = 5014
DECMD_GETFORECOLOR = 5015
DECMD_HYPERLINK = 5016
DECMD_IMAGE = 5017
DECMD_INDENT = 5018
DECMD_INSERTCELL = 5019
DECMD_INSERTCOL = 5020
DECMD_INSERTROW = 5021
DECMD_INSERTTABLE = 5022
DECMD_ITALIC = 5023
DECMD_JUSTIFYCENTER = 5024
DECMD_JUSTIFYLEFT = 5025
DECMD_JUSTIFYRIGHT = 5026
DECMD_LOCK_ELEMENT = 5027
DECMD_MAKE_ABSOLUTE = 5028
DECMD_MERGECELLS = 5029
DECMD_ORDERLIST = 5030
DECMD_OUTDENT = 5031
DECMD_PASTE = 5032
DECMD_REDO = 5033
DECMD_REMOVEFORMAT = 5034
DECMD_SELECTALL = 5035
DECMD_SEND_BACKWARD = 5036
DECMD_BRING_FORWARD = 5037
DECMD_SEND_BELOW_TEXT = 5038
DECMD_BRING_ABOVE_TEXT = 5039
DECMD_SEND_TO_BACK = 5040
DECMD_BRING_TO_FRONT = 5041
DECMD_SETBACKCOLOR = 5042
DECMD_SETBLOCKFMT = 5043
DECMD_SETFONTNAME = 5044
DECMD_SETFONTSIZE = 5045
DECMD_SETFORECOLOR = 5046
DECMD_SPLITCELL = 5047
DECMD_UNDERLINE = 5048
DECMD_UNDO = 5049
DECMD_UNLINK = 5050
DECMD_UNORDERLIST = 5051
DECMD_PROPERTIES = 5052
; Enums
;OLECMDEXECOPT
OLECMDEXECOPT_DODEFAULT = 0
OLECMDEXECOPT_PROMPTUSER = 1
OLECMDEXECOPT_DONTPROMPTUSER = 2
; DHTMLEDITCMDF
DECMDF_NOTSUPPORTED = 0
DECMDF_DISABLED = 1
DECMDF_ENABLED = 3
DECMDF_LATCHED = 7
DECMDF_NINCHED = 11
; DHTMLEDITAPPEARANCE
DEAPPEARANCE_FLAT = 0
DEAPPEARANCE_3D = 1
; OLE_TRISTATE
OLE_TRISTATE_UNCHECKED = 0
OLE_TRISTATE_CHECKED = 1
OLE_TRISTATE_GRAY = 2
; Error Return Values
;
DE_E_INVALIDARG = 0x5
DE_E_ACCESS_DENIED = 0x46
DE_E_PATH_NOT_FOUND = 0x80070003
DE_E_FILE_NOT_FOUND = 0x80070002
DE_E_UNEXPECTED = 0x8000ffff
DE_E_DISK_FULL = 0x80070027
DE_E_NOTSUPPORTED = 0x80040100
DE_E_FILTER_FRAMESET = 0x80100001
DE_E_FILTER_SERVERSCRIPT = 0x80100002
DE_E_FILTER_MULTIPLETAGS = 0x80100004
DE_E_FILTER_SCRIPTLISTING = 0x80100008
DE_E_FILTER_SCRIPTLABEL = 0x80100010
DE_E_FILTER_SCRIPTTEXTAREA = 0x80100020
DE_E_FILTER_SCRIPTSELECT = 0x80100040
DE_E_URL_SYNTAX = 0x800401E4
DE_E_INVALID_URL = 0x800C0002
DE_E_NO_SESSION = 0x800C0003
DE_E_CANNOT_CONNECT = 0x800C0004
DE_E_RESOURCE_NOT_FOUND = 0x800C0005
DE_E_OBJECT_NOT_FOUND = 0x800C0006
DE_E_DATA_NOT_AVAILABLE = 0x800C0007
DE_E_DOWNLOAD_FAILURE = 0x800C0008
DE_E_AUTHENTICATION_REQUIRED = 0x800C0009
DE_E_NO_VALID_MEDIA = 0x800C000A
DE_E_CONNECTION_TIMEOUT = 0x800C000B
DE_E_INVALID_REQUEST = 0x800C000C
DE_E_UNKNOWN_PROTOCOL = 0x800C000D
DE_E_SECURITY_PROBLEM = 0x800C000E
DE_E_CANNOT_LOAD_DATA = 0x800C000F
DE_E_CANNOT_INSTANTIATE_OBJECT = 0x800C0010
DE_E_REDIRECT_FAILED = 0x800C0014
DE_E_REDIRECT_TO_DIR = 0x800C0015
DE_E_CANNOT_LOCK_REQUEST = 0x800C0016
LoadEasyCOM()
; ------------------------- General functions ---------------------------------------------------------------
DE_Add(hWnd, x, y, w, h)
{
AtlAxWinInit()
Return AtlAxGetControl( AtlAxCreateContainer(hWnd, x, y, w, h, "DhtmlEdit.DhtmlEdit") )
}
DE_Move(pwb, x, y, w, h)
{
WinMove, % "ahk_id " . GetHostWindow(pwb), , x, y, w, h
}
DE_BrowseMode(pDHtmlEdit) ; toggle between Edit mode and View mode.
{
iret := InvokeS(pDhtmlEdit, "Browsemode")
if iret = 0
iret := InvokeS(pDhtmlEdit, "Browsemode=", 1)
Else
iret := InvokeS(pDhtmlEdit, "Browsemode=", 0)
}
DE_LoadUrl(pDhtmlEdit, url) ;Load url(e.g. "http://www.autohotkey.com") and ready to edit in a WYSIWIG way
{
iRet := InvokeS(pDhtmlEdit, "LoadUrl()", url)
}
DE_NewDocument(pDhtmlEdit) ;clear current document and open blank html document
{
iRet := InvokeS(pDhtmlEdit, "NewDocument()")
}
DE_LoadDocument(pDhtmlEdit, FileDir, prompt = "Load Html File") ;open file dialog and last parameter is prompt string.
{
iRet := InvokeS(pDhtmlEdit, "LoadDocument()", FileDir)
}
DE_SaveDocument(pDhtmlEdit, Filedir) ;save contents in html.
{
iRet := InvokeS(pDhtmlEdit, "SaveDocument()", FileDir)
}
DE_GetDocumentHtml(pDHtmlEdit) ;get and return DOCUMENT'S htmlcode
{
iret := InvokeS(pDhtmlEdit, "DocumentHtml")
return iret
}
DE_SetDocumentHtml(pDHtmlEdit, sHtml) ;set document's htmlcode
{
iret := InvokeS(pDhtmlEdit, "DocumentHtml=", sHtml)
}
DE_Refresh(pDhtmlEdit) ;open file dialog and last parameter is prompt string.
{
iRet := InvokeS(pDhtmlEdit, "Refresh()")
}
; --- WYSIWYG Edit functions ----------------------------------------------------------------------------------------------
; Set property --> use ExecCommand(), Command ID
; Get Propery --> use QueryStatus(), command ID
DE_SetBOLD(pDhtmlEdit) ; toggle selections bold/normal
{
iret := InvokeS(pDhtmlEdit, "ExecCommand()", DECMD_BOLD)
}
DE_SetUnderline(pDHtmlEdit) ; toggle selections underline
{
iret := InvokeS(pDhtmlEdit, "ExecCommand()", DECMD_UNDERLINE)
}
DE_SetItalic(pDHtmlEdit) ; toggle selections italic
{
iret := InvokeS(pDhtmlEdit, "ExecCommand()", DECMD_ITALIC)
}
DE_SetForeColor(pDHtmlEdit, sColor) ; set font color string, e.g. "#55A0FF", "55A0FF", "Blue", "Red"
{
iret := InvokeS(pDhtmlEdit, "ExecCommand()", DECMD_SETFORECOLOR, OLECMDEXECOPT_DODEFAULT, sColor)
}
DE_SetHyperLink(pDHtmlEdit) ; insert hyperlink property in selection
{
iret := InvokeS(pDhtmlEdit, "ExecCommand()", DECMD_HYPERLINK, OLECMDEXECOPT_DODEFAULT, "")
}
DE_SetImage(pDHtmlEdit) ; insert image in selection
{
iret := InvokeS(pDhtmlEdit, "ExecCommand()", DECMD_IMAGE, OLECMDEXECOPT_DODEFAULT, "")
}
; --------- USING DOM ------------------------------------------------------------------------------------------
; it's just from the Dhtml SDK, I don't know at all about DOM.
; maybe we need to load MSHTML.DLL to acess DOM, I don't know.
DE_DOM(pDHtmlEdit)
{
iret := InvokeS(pDhtmlEdit, "DOM")
return iret
}
;How can I invoke this? I can invoke DOM like above, but not selection below.
; sel = DHTMLEdit.DOM.selection (from SDK)
; As far as I know, if we wanna play with html source, we have to acess DOM.
GetHostWindow(pwb)
{
GUID4String(IID_IOleWindow, "{00000114-0000-0000-C000-000000000046}")
DllCall(VTable(pwb, 0), "Uint", pwb, "str", IID_IOleWindow, "UintP", pow)
DllCall(VTable(pow, 3), "Uint", pow, "UintP", hWnd)
DllCall(VTable(pow, 2), "Uint", pow)
Return DllCall("GetParent", "Uint", hWnd)
}
AtlAxWinInit()
{
If !DllCall("GetModuleHandle", "str", "atl")
DllCall("LoadLibrary" , "str", "atl")
Return DllCall("atl\AtlAxWinInit")
}
AtlAxWinTerm()
{
If hModule := DllCall("GetModuleHandle", "str", "atl")
DllCall("FreeLibrary", "Uint", hModule)
}
AtlAxGetControl(hWnd)
{
DllCall("atl\AtlAxGetControl", "Uint", hWnd, "UintP", punk)
pdsp := QueryInterface(punk, IID_IDispatch := "{00020400-0000-0000-C000-000000000046}")
Release(punk)
Return pdsp
}
AtlAxAttachControl(pdsp, hWnd)
{
punk := QueryInterface(pdsp, IID_IUnknown := "{00000000-0000-0000-C000-000000000046}")
DllCall("atl\AtlAxAttachControl", "Uint", punk, "Uint", hWnd, "Uint", 0)
Release(punk)
}
AtlAxCreateContainer(hWnd, x, y, w, h, sName = "")
{
pName := sName ? &sName : 0
AtlAxWin := "AtlAxWin"
Return DllCall("CreateWindowEx", "Uint", 0x200, "Uint", &AtlAxWin, "Uint", pName, "Uint",0x10000000|0x40000000|0x04000000, "int", x, "int", y, "int", w, "int", h, "Uint", hWnd, "Uint", 0, "Uint", 0, "Uint", 0)
}
Test_demo(same with the above screenshot)Code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Include easycom.ahk
#include cohelper_ed.ahk
; poor way of avoiding the conflicts of the same function name, but I need to use AltAX kind functions.
; it's Sean's cohleper.ahk except I removed only CreateObject functions
; until .. implements AltxWin related functions in his easycom.dll
#include DE_Control.ahk
LoadEasyCOM()
CoInitialize()
Gui, +Resize +LastFound
Gui, Add, Button, x0 y0 gSetBold, Bold
Gui, Add, Button, xp+40 gSetItalic,italic
Gui, Add, Button, xp+50 gSetUnderLine, underline
gui, Add, Button, xp+75 gSetBlue, Blue
Gui, Add, Button, xp+40 gSetImageLink, Image
Gui, Add, Button, xp+50 gSetHyperLink, Link
Gui, Add, Button, xp+50 gLoadUrl, LoadURL
Gui, Add, Button, xp+70 gGetDocument, GetHtml
gui, add, button, xp+70 gSetDocument, SetHtml
Gui, Add, button, xp+70 gSaveDocument, SaveHtml
Gui, Add, button, xp+80 gBrowseMode, BrowseMode Toggle
Gui, add, button, xp+140 gNewDocument, New
Gui, Show, w800 h600 Center, DhtmlEdit_Test
hWnd := WinExist()
;DE_ADD(hWnd, 0, 25, 800, 575)
;DEdit := GetActiveObject("DhtmlEdit.DhtmlEdit")
AtlAxWinInit()
DEdit := CreateObject("DhtmlEdit.DhtmlEdit")
;AtlAxAttachControl(DEdit, hWnd)
hContainerCtrl := AtlAxCreateContainer(hWnd, 0, 25, 800, 575)
AtlAxAttachControl(DEdit, hContainerCtrl)
gosub, SetDocument
Return
SetBold:
iret := InvokeS(DEdit, "ExecCommand()", DECMD_BOLD)
;DE_SetBold(DEdit) <---- I don't know why it's not working. It's the same function.
Return
SetItalic:
iret := InvokeS(DEdit, "ExecCommand()", DECMD_ITALIC)
;DE_SetItalic(DEdit) <---- I don't know why it's not working. It's the same function.
Return
SetBlue:
iret := InvokeS(DEdit, "ExecCommand()", DECMD_SETFORECOLOR, OLECMDEXECOPT_DODEFAULT, "Blue")
;DE_SetForeColor(DEdit, "0000FF") <-------- I don't know why it's not working
;DE_SetForeColor(pDHtmlEdit, "Blue") ; possible way
;DE_SetForeColor(pDHtmlEdit, "#0000FF") ; also possible
Return
SetUnderline:
iret := InvokeS(DEdit, "ExecCommand()", DECMD_UNDERLINE)
;DE_SetUnderline(DEdit) <-------I don't know why it's not working. It's the same function.
Return
SetImageLink:
iret := InvokeS(DEdit, "ExecCommand()", DECMD_IMAGE, OLECMDEXECOPT_DODEFAULT, "")
;DE_SetImage(DEdit) <-------I don't know why it's not working. It's the same function.
Return
SetHyperLink:
iret := InvokeS(DEdit, "ExecCommand()", DECMD_HYPERLINK, OLECMDEXECOPT_DODEFAULT, "")
;DE_SetHyperLink(DEdit) <-------I don't know why it's not working. It's the same function.
Return
LoadUrl:
url := "http://www.autohotkey.com"
DE_LoadUrl(DEdit, url)
Return
NewDocument:
DE_NewDocument(DEdit)
Return
SaveDocument:
Filedir = %A_ScriptDir%\DhtmlEdit_%A_Now%.htm
DE_SaveDocument(DEdit, FileDir)
Return
GetDocument:
msgbox, % DE_GetDocumentHtml(DEdit)
Return
SetDocument:
htmlcode =
(
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body
style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(255,204,51)" alink
="#ee0000" link="#0000ee" vlink="#551a8b">
<P>
<big style="FONT-FAMILY: Verdana"><big><EM><STRONG>Forgive</STRONG></EM> my <span
style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)" >poor</span> coding style! Yes?
</big></big></P>
<P><BIG style="FONT-FAMILY: Verdana"><BIG>This File is Edited with
<STRONG>DhtmlEdit_Demo</STRONG>
.<br>
<br></P></BIG></BIG>
<ul style="FONT-FAMILY: Verdana">
<li><STRONG><U>First List</U></STRONG>
<li><FONT color=blue>second List</FONT>
<li><A href="http://www.autohotkey.com">what
now?(</A>click then go to AutohotKey)</li>
</ul>
<br style="FONT-FAMILY: Verdana">
<table
style="WIDTH: 440px; FONT-FAMILY: Verdana; HEIGHT: 52px; TEXT-ALIGN: left"
border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td>This is a table<br>
and the right image is directly from AutoHotKey Forum.</td>
<td><A href="http://www.autohotkey.com"><img style="WIDTH: 228px; HEIGHT: 133px"
alt="image cannot be loaed for some reason"
src="http://www.autohotkey.com/docs/images/AutoHotkey_logo.gif"></A></td>
</tr>
</tbody>
</table>
</body>
</html>
)
DE_SetDocumentHtml(DEdit, htmlcode)
htmlcode =
Return
BrowseMode:
DE_BrowseMode(DEdit)
Return
GuiSize:
DE_Move(DEdit, 0, 25, A_GuiWidth, A_GuiHeight)
Return
GuiClose:
Gui, %A_Gui%:Destroy
ReleaseObject(DEdit)
UnloadEasyCOM()
AtlAxWinTerm()
CoUninitialize()
ExitApp