Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Embed an HTML control in your own window ... [Dll]


  • Please log in to reply
112 replies to this topic
BoBo
  • Guests
  • Last active:
  • Joined: --

Embed an HTML control in your own window using plain C
By Jeff Glatt.

Specifically shows how to embed a browser OLE object in your own window, and more generally demonstrates how to manipulate and create COM/OLE objects, in plain C (ie, no MFC, WTL, ATL, .NET, C#, nor even C++). The latter is applicable to many other uses, such as creating your own script engine.

Introduction
There are numerous examples that demonstrate how to embed Internet Explorer as an OLE/COM object in your own window. But these examples typically use Microsoft Foundation Classes (MFC), .NET, C#, or at least Windows Template Library (WTL) because those frameworks have pre-fabricated "wrappers" to easily give you an "HTML control" to embed in your window. If you're trying to use plain C, without MFC, WTL, .NET, C#, or even any C++ code at all, then there is a dearth of examples and information how to deal with COM objects such as IE's IWebBrowser2. Here is an article and working example in C to specifically show you what you need to do in order to embed IE in your own window.

In fact, I've even wrapped up the example C code (to embed IE in your own window) into a Dynamic Link Library (DLL) so that you can simply call one function to display a web page or some HTML string in a window you create. You won't even need to get your hands dirty with COM (unless you plan to modify the source of the DLL).

Before proceeding with this article, you should first read my series of articles regarding COM in plain C. Part 1 discusses information you'll need to know in order to use COM objects. We will also have to deal with objects that have multiple interfaces, as discussed in Part 4. We'll be using automation datatypes, as discussed in Part 2. Finally, we'll also be dealing with events (callbacks), discussed in Part 5.

[More...]



BoBo
  • Guests
  • Last active:
  • Joined: --
Given on the same page:

[Introduction to COM] - What It Is and How to Use It. A tutorial for programmers new to COM that explains how to reuse existing COM components, for example, components in the Windows shell.

[Introduction to COM Part II] - Behind the Scenes of a COM Server
A tutorial for programmers new to COM that explains the internals of COM servers, and how to write your own interfaces in C++

Might be of interest. 8)

BoBo
  • Guests
  • Last active:
  • Joined: --
Please check all other links/cross references pointing to articels of the same author. It looks like here's some potential to improve AHK regarding COM support. But as always, I might be (completely) wrong ... :)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks for the links. I'll browse those links now, and then one of these days I'll dig into COM/OLE.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Great.
Thanx Bobo
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Excellent, I will look into these resources soon. Thanks.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

not-logged-in-daonlyfreez
  • Guests
  • Last active:
  • Joined: --
I got the basics working! 8)

hModule := DllCall("LoadLibrary", "str", "cwebpage.dll")  

OnExit, Cleanup

Gui, Show, w600 h400, test
WinGet, ourHWND, ID, A
res := DLLCall("cwebpage\EmbedBrowserObject", UInt, ourHWND)
If (res != 0 Or ErrorLevel != 0) ; error
  Goto, Cleanup

res := DLLCall("cwebpage\DisplayHTMLPage", UInt, ourHWND, Str, "http://www.autohotkey.com")
If (res != 0 Or ErrorLevel != 0) ; error
  Goto, Cleanup

return

Cleanup:
GuiClose:
DLLCall("cwebpage\UnEmbedBrowserObject", UInt, ourHWND)
DllCall("FreeLibrary", "UInt", hModule)
ExitApp
return


BoBo
  • Guests
  • Last active:
  • Joined: --

I got the basics working!

Dafür dürft ihr dann auch bei der EM Zweiter werden, im Endspiel gegen Deutschland :lol:

Would you mind to provide the mandatory Dll too? Maybe on Autohotkey.net?? Wouldn't it make sense to provide both at the Script&Function section??

Btw. Thx for sharing it. Dank u wel :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@daonlyfreez: Many thanks for the working code :D

hModule := DllCall("LoadLibrary", "str", "cwebpage.dll")  

OnExit, Cleanup 
Gui -Sysmenu +AlwaysOnTop
Gui, Show, x20 y20 w126 h50, IP Address
WinGet, ourHWND, ID, A 
res := DLLCall("cwebpage\EmbedBrowserObject", UInt, ourHWND) 
If (res != 0 Or ErrorLevel != 0) ; error 
  Goto, Cleanup 

res := DLLCall("cwebpage\DisplayHTMLPage", UInt, ourHWND
              , Str, "http://www.netikus.net/show_ip.html") 
If (res != 0 Or ErrorLevel != 0) ; error 
  Goto, Cleanup 

return 

Cleanup: 
GuiClose: 
GuiEscape:
  DLLCall("cwebpage\UnEmbedBrowserObject", UInt, ourHWND) 
  DllCall("FreeLibrary", "UInt", hModule) 
  ExitApp 
return

kWo4Lk1.png

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
This is the link with dll
http://www.codeguru.... ... ebpage.zip

It works nice.

I will remember this page. Very useful.
Posted Image

BoBo
  • Guests
  • Last active:
  • Joined: --
[Step by Step COM Tutorial - By Saurabh Dasgupta]

[COM from scratch - PART ONE - By Aria Ansari.]
[COM from scratch - PART TWO - By Aria Ansari]
[COM from scratch - PART THREE - By Aria Ansari]

Now with a lot of images :wink::D

not-logged-in-daonlyfreez
  • Guests
  • Last active:
  • Joined: --
More elaborate example:

#NoEnv
SetBatchLines, -1
SetWinDelay, 0

; Load cwebpage.dll
cwebpageHandle := DllCall("LoadLibrary", "str", "cwebpage.dll")  

; Exit handler
OnExit, Cleanup

; Create menu
Menu, browserMenu, Add, Back
Menu, browserMenu, Add, Forward
Menu, browserMenu, Add, Refresh
Menu, browserMenu, Add, Stop
Menu, browserMenu, Add, Home
Menu, browserMenu, Add, Search

; Parent window
Gui, 1: Add, Button, y8 x0 w50 gBack, Back
Gui, 1: Add, Button, y8 x50 w50 gBack, Forward
Gui, 1: Add, Button, y8 x100 w50 gRefresh, Refresh
Gui, 1: Add, Button, y8 x150 w50 gStop, Stop
Gui, 1: Add, Button, y8 x200 w50 gHome, Home
Gui, 1: Add, Button, y8 x250 w50 gSearch, Search
Gui, 1: Add, Button, y370 x0 w150 gGoExample, Open example html
Gui, 1: Add, Button, y370 x150 w150 gGoAHKCHM, Open AHK chm
Gui, 1: Add, Button, y370 x300 w150 gGoAHKHome, Go to AHK homepage
Gui, 1: Show, w600 h400, Internet Explorer container in AHK with cwebpage.dll
WinGet, mainGuiHandle, ID, A

; Child window for Browser object
Gui, 2: Margin, 0, 0 
Gui, 2: +ToolWindow -Caption +Border
Gui, 2: Show, w590 h320
WinGet, browserGuiHandle, ID, A
; Set as child window to main gui
Gui, 2: +LastFound
DllCall("SetParent", "uint", WinExist(), "uint", mainGuiHandle)

; Create the Browser object
res := DLLCall("cwebpage\EmbedBrowserObject", "uint", browserGuiHandle)
If (res != 0 Or ErrorLevel != 0) ; error
  Goto, Cleanup

; Position the Browser "control" in the Gui
WinMove, ahk_id %browserGuiHandle%, , 4, 40

; Load welcome html
Gosub, GoExample

; Monitor for right clicks on browser control
; WM_CONTEXTMENU ; 0x7B
OnMessage(0x7B, "Web_WM_CONTEXTMENU")

Return

; *** Routines:

; Browser control rightclick
Web_WM_CONTEXTMENU(wParam, lParam)
{
  Menu, browserMenu, Show
}
Return

; Browser control actions
Back:
res := DLLCall("cwebpage\DoPageAction", "uint", browserGuiHandle, "uint", 0)
Return

Forward:
res := DLLCall("cwebpage\DoPageAction", "uint", browserGuiHandle, "uint", 1)
Return

Home:
res := DLLCall("cwebpage\DoPageAction", "uint", browserGuiHandle, "uint", 2)
Return

Search:
res := DLLCall("cwebpage\DoPageAction", "uint", browserGuiHandle, "uint", 3)
Return

Refresh:
res := DLLCall("cwebpage\DoPageAction", "uint", browserGuiHandle, "uint", 4)
Return

Stop:
res := DLLCall("cwebpage\DoPageAction", "uint", browserGuiHandle, "uint", 5)
Return

; Examples

; Load welcome/example html
GoExample:
HTML2Load = 
(
<html><head><title>HTML Template</title></head><body>
Welcome to the <a href="http://www.codeproject.com/com/cwebpage.asp">
cwebpage.dll</a> browser control embedded into an AutoHotkey GUI
<ol><li>Pressing the <b>Open example html</b> button will load some example html
<li>Pressing the <b>Open AHK chm</b> button will load the startpage of the AutoHotkey chm helpfile locally on your computer.
<font color="green"><b>Cwebpage.dll can open and navigate in chm-files too!</b></font>
<li>Pressing the <b>Go to AHK homepage</b> button will load the AutoHotkey homepage online
<li>Pressing the <b>Open welcome html</b> button will reload this page</ol>
You can control the browser object with the top buttons and the context menu...

<font color="red"><b>Enjoy!</b></font>...
</body></html>
)
Gosub, LoadHTML
Return

GoAHKCHM:
URL2Load = its:%A_ProgramFiles%\AutoHotkey\AutoHotkey.chm::docs\AutoHotkey.htm
Gosub, LoadURL
Return

GoAHKHome:
URL2Load = http://www.autohotkey.com
Gosub, LoadURL
Return

; Load URL
LoadURL:
res := DLLCall("cwebpage\DisplayHTMLPage"
                  , "uint", browserGuiHandle
                  , "str", URL2Load)
If (res != 0 Or ErrorLevel != 0) ; error
  Goto, Cleanup
URL2Load =
Return

; Load HTML
LoadHTML:
res := DLLCall("cwebpage\DisplayHTMLStr"
                  , "uint", browserGuiHandle
                  , "str", HTML2Load)
If (res != 0 Or ErrorLevel != 0) ; error
  Goto, Cleanup
HTML2Load =
Return

; Cleanup and exit
Cleanup:
GuiEscape:
GuiClose:
DLLCall("cwebpage\UnEmbedBrowserObject", "uint", browserGuiHandle)
DllCall("FreeLibrary", "uint", cwebpageHandle)
ExitApp
Return

For your convenience, alternative download links:

cwebpage.dll
cwebpage_src.zip

deefault
  • Members
  • 18 posts
  • Last active: Mar 07 2007 11:04 PM
  • Joined: 03 Aug 2006
Thanks for the great example!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@daonlyfreez: Your examples are great!. Not many people would
be visiting this section. I suggest you to post the links and the examples
in the Scripts & Functions section.

Many thanks again for the working examples!

Regards, :)
kWo4Lk1.png

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Yes, its great...

Although i miss save option.... I didn't notice encapsulation of that part of WebBrowser interface in dll.
Posted Image