Draw a 'web-browser' on a hDC (Screen-output) (Windows only)
Draw a 'web-browser' on a hDC (Screen-output) (Windows only)
i have seen a code where the
output of a web-interface is drawn on
a hdc .
as far as i know, there are only a few lines
which did that.
see http://www.ionicwind.com/forums/index.p ... 589.0.html
the problem, i'm not familar enough with
all those AtlAx*** and OLE stuff, neither with that
_IID_IHTMLDocument****
.
Anyone got a few working tricks for me ?
output of a web-interface is drawn on
a hdc .
as far as i know, there are only a few lines
which did that.
see http://www.ionicwind.com/forums/index.p ... 589.0.html
the problem, i'm not familar enough with
all those AtlAx*** and OLE stuff, neither with that
_IID_IHTMLDocument****
.
Anyone got a few working tricks for me ?
SPAMINATOR NR.1
You don't need to worry about the AtlAx*** stuff as the PB web-gadget will handle all of that for you.
Interestingly, I can only get this to work properly if the hdc is indeed a screen DC. I suspect that a memory DC needs some poking around with the mapping modes etc.
What I have done, therefore, is arrange for the rendering to be directed to a borderless window. From here you can blt it to an off screen image if you wish etc.
Interestingly, I can only get this to work properly if the hdc is indeed a screen DC. I suspect that a memory DC needs some poking around with the mapping modes etc.
What I have done, therefore, is arrange for the rendering to be directed to a borderless window. From here you can blt it to an off screen image if you wish etc.
Code: Select all
Import "uuid.lib"
IID_IHTMLDocument2
IID_IViewObject
EndImport
Procedure.i RenderHTMLToHDC(hdc, left, top, right, bottom, url$)
Protected result, winID, webID, oWebGadget.IWebBrowser2, oDispatch.iDispatch, oDocument.IHTMLDocument2, oViewObject.IViewObject, rc.RECT
Protected bstr, text$
If url$
;Open an invisible web gadget to host the document.
winID = OpenWindow(#PB_Any, 0, 0, right-left, bottom-top, "", #PB_Window_Invisible)
If winID
webID = WebGadget(#PB_Any, 0, 0, right-left, bottom-top, url$)
If webID
While WindowEvent()
Wend
oWebGadget = GetWindowLong_(GadgetID(webID),#GWL_USERDATA)
If oWebGadget
If oWebGadget\Get_Document(@oDispatch) = #S_OK
If oDispatch\QueryInterface(@IID_IHTMLDocument2, @oDocument) = #S_OK
Repeat
oDocument\Get_ReadyState(@bstr)
text$ = PeekS(bstr, -1, #PB_Unicode)
SysFreeString_(bstr)
If text$ = "complete"
Break
EndIf
WindowEvent()
ForEver
If oDocument\Queryinterface(@IID_IViewObject, @oViewObject) = #S_OK
SetRect_(rc, left, top, right, bottom)
If oViewObject\Draw(#DVASPECT_CONTENT, -1, 0, 0, 0, hdc, rc, 0, 0, 0) = #S_OK
result = #True
EndIf
oViewObject\Release()
EndIf
oDocument\Release()
EndIf
oDispatch\Release()
EndIf
EndIf
EndIf
CloseWindow(winID)
EndIf
EndIf
ProcedureReturn result
EndProcedure
width = 600
height = 600
If OpenWindow(0, 0, 0, 600, 70, "ImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(1, 10, 10, 580, 20, "http://www.purebasic.com")
ButtonGadget(2, 550, 40, 40, 20, "GO")
Repeat
eventID = WaitWindowEvent()
Select eventID
Case #PB_Event_Gadget
Select EventGadget()
Case 2
If IsWindow(1)
CloseWindow(1)
EndIf
OpenWindow(1, 0, 0, width, height, "", #PB_Window_BorderLess)
hdc = StartDrawing(WindowOutput(1))
If hdc
result = RenderHTMLToHDC(hdc, 0, 0, width, height, GetGadgetText(1))
StopDrawing()
EndIf
EndSelect
EndSelect
Until eventID = #PB_Event_CloseWindow
EndIf
I may look like a mule, but I'm not a complete ass.
-
- Enthusiast
- Posts: 469
- Joined: Sun Mar 16, 2008 9:18 am
- netmaestro
- PureBasic Bullfrog
- Posts: 8405
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
You don't need a window to render to, an image will do:srod wrote:What I have done, therefore, is arrange for the rendering to be directed to a borderless window
Code: Select all
Import "uuid.lib"
IID_IHTMLDocument2
IID_IViewObject
EndImport
Procedure.i RenderHTMLToHDC(hdc, left, top, right, bottom, url$)
Protected result, winID, webID, oWebGadget.IWebBrowser2, oDispatch.iDispatch, oDocument.IHTMLDocument2, oViewObject.IViewObject, rc.RECT
Protected bstr, text$
If url$
;Open an invisible web gadget to host the document.
winID = OpenWindow(#PB_Any, 0, 0, right-left, bottom-top, "", #PB_Window_Invisible)
If winID
webID = WebGadget(#PB_Any, 0, 0, right-left, bottom-top, url$)
If webID
While WindowEvent()
Wend
oWebGadget = GetWindowLong_(GadgetID(webID),#GWL_USERDATA)
If oWebGadget
If oWebGadget\Get_Document(@oDispatch) = #S_OK
If oDispatch\QueryInterface(@IID_IHTMLDocument2, @oDocument) = #S_OK
Repeat
oDocument\Get_ReadyState(@bstr)
text$ = PeekS(bstr, -1, #PB_Unicode)
SysFreeString_(bstr)
If text$ = "complete"
Break
EndIf
WindowEvent()
ForEver
If oDocument\Queryinterface(@IID_IViewObject, @oViewObject) = #S_OK
SetRect_(rc, left, top, right, bottom)
If oViewObject\Draw(#DVASPECT_CONTENT, -1, 0, 0, 0, hdc, rc, 0, 0, 0) = #S_OK
result = #True
EndIf
oViewObject\Release()
EndIf
oDocument\Release()
EndIf
oDispatch\Release()
EndIf
EndIf
EndIf
CloseWindow(winID)
EndIf
EndIf
ProcedureReturn result
EndProcedure
width = 600
height = 600
If OpenWindow(0, 0, 0, 600, 70, "ImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(1, 10, 10, 580, 20, "http://www.purebasic.com")
ButtonGadget(2, 550, 40, 40, 20, "GO")
Repeat
eventID = WaitWindowEvent()
Select eventID
Case #PB_Event_Gadget
Select EventGadget()
Case 2
dc = CreateDC_("DISPLAY",0,0,0)
hdc = CreateCompatibleDC_(dc)
CreateImage(0,width,height,24)
SelectObject_(hdc, ImageID(0))
If hdc
result = RenderHTMLToHDC(hdc, 0, 0, width, height, GetGadgetText(1))
EndIf
DeleteDC_(dc)
DeleteDC_(hdc)
OpenWindow(1,0,0,width,height,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(3,0,0,0,0,ImageID(0))
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
CloseWindow(1)
EndSelect
EndSelect
Until eventID = #PB_Event_CloseWindow
EndIf
BERESHEIT
Actually, that exactly explains the effect that I was seeing. I should have realised! Then again, it fails also with a bitmap created via CreateCompatibleBitmap_() when the resulting bitmap is 32-bits. I don't see how PB's image commands can cause this - unless it is the use of the imagegadget? Testing...freak wrote:> I wonder why the 32-bit image fails?
Because in 4.40 PB uses a real alphachannel in 32bit images and Windows screws it up. If you plan to mix PB 4.40 images with api then 24bit is the way to go.
**EDIT : it is indeed the image gadget! I understand now Freak; thanks.

I may look like a mule, but I'm not a complete ass.
- netmaestro
- PureBasic Bullfrog
- Posts: 8405
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Not sure how you were going about it my coderusty friend, but it's fine here, with the image gadget:
Did you remember to reference the original screen dc in CreateCompatibleBitmap? The memory dc won't have the right attributes.
Code: Select all
Import "uuid.lib"
IID_IHTMLDocument2
IID_IViewObject
EndImport
Procedure.i RenderHTMLToHDC(hdc, left, top, right, bottom, url$)
Protected result, winID, webID, oWebGadget.IWebBrowser2, oDispatch.iDispatch, oDocument.IHTMLDocument2, oViewObject.IViewObject, rc.RECT
Protected bstr, text$
If url$
;Open an invisible web gadget to host the document.
winID = OpenWindow(#PB_Any, 0, 0, right-left, bottom-top, "", #PB_Window_Invisible)
If winID
webID = WebGadget(#PB_Any, 0, 0, right-left, bottom-top, url$)
If webID
While WindowEvent()
Wend
oWebGadget = GetWindowLong_(GadgetID(webID),#GWL_USERDATA)
If oWebGadget
If oWebGadget\Get_Document(@oDispatch) = #S_OK
If oDispatch\QueryInterface(@IID_IHTMLDocument2, @oDocument) = #S_OK
Repeat
oDocument\Get_ReadyState(@bstr)
text$ = PeekS(bstr, -1, #PB_Unicode)
SysFreeString_(bstr)
If text$ = "complete"
Break
EndIf
WindowEvent()
ForEver
If oDocument\Queryinterface(@IID_IViewObject, @oViewObject) = #S_OK
SetRect_(rc, left, top, right, bottom)
If oViewObject\Draw(#DVASPECT_CONTENT, -1, 0, 0, 0, hdc, rc, 0, 0, 0) = #S_OK
result = #True
EndIf
oViewObject\Release()
EndIf
oDocument\Release()
EndIf
oDispatch\Release()
EndIf
EndIf
EndIf
CloseWindow(winID)
EndIf
EndIf
ProcedureReturn result
EndProcedure
width = 600
height = 600
If OpenWindow(0, 0, 0, 600, 70, "ImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(1, 10, 10, 580, 20, "http://www.purebasic.com")
ButtonGadget(2, 550, 40, 40, 20, "GO")
Repeat
eventID = WaitWindowEvent()
Select eventID
Case #PB_Event_Gadget
Select EventGadget()
Case 2
dc = CreateDC_("DISPLAY",0,0,0)
hdc = CreateCompatibleDC_(dc)
hBMP = CreateCompatibleBitmap_(dc, width, height)
GetObject_(hbmp, SizeOf(bitmap), @bmp.bitmap)
With bmp
Debug \bmbitspixel
EndWith
SelectObject_(hdc, hBMP)
If hdc
result = RenderHTMLToHDC(hdc, 0, 0, width, height, GetGadgetText(1))
EndIf
DeleteDC_(dc)
DeleteDC_(hdc)
OpenWindow(1,0,0,width,height,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(3,0,0,0,0,hBMP)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
CloseWindow(1)
EndSelect
EndSelect
Until eventID = #PB_Event_CloseWindow
EndIf
BERESHEIT
- netmaestro
- PureBasic Bullfrog
- Posts: 8405
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Yes your code fails. My original test code is identical to yours including the use of the correct DC.
The problem is that the image gadget seems to be treating the 32-bit image as entirely transparent.

Last edited by srod on Sun Aug 23, 2009 7:02 pm, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
- netmaestro
- PureBasic Bullfrog
- Posts: 8405
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- netmaestro
- PureBasic Bullfrog
- Posts: 8405
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
The image is created fine (as you'll see if you use DrawImage() to blt directly to the new window etc.) The problem is with the imagegadget which is to be expected what with all the changes to PB 4.4 as explained by Freak above.
In fact, if you change the Vista display settings to use a 16-bit desktop then both mine and your code work fine Lloyd. Final confirmation that it is the interaction between the image gadget and 32-bit images under PB 4.4.
In fact, if you change the Vista display settings to use a 16-bit desktop then both mine and your code work fine Lloyd. Final confirmation that it is the interaction between the image gadget and 32-bit images under PB 4.4.
I may look like a mule, but I'm not a complete ass.