PureBasic Forum
https://www.purebasic.fr/english/

Draw a 'web-browser' on a hDC (Screen-output) (Windows only)
https://www.purebasic.fr/english/viewtopic.php?f=13&t=38737
Page 1 of 2

Author:  Rings [ Sun Aug 23, 2009 8:14 am ]
Post subject:  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 ?

Author:  srod [ Sun Aug 23, 2009 2:09 pm ]
Post subject: 

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.

Code:
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

Author:  UserOfPure [ Sun Aug 23, 2009 2:45 pm ]
Post subject: 

srod wrote:
From here you can blt it to an off screen image if you wish

Hi, what the heck does that mean? :lol: I want to save the image that was drawn on the window, and I know your quote above is a means to do that, but I have no idea what to do.....

Author:  netmaestro [ Sun Aug 23, 2009 3:27 pm ]
Post subject: 

srod wrote:
What I have done, therefore, is arrange for the rendering to be directed to a borderless window

You don't need a window to render to, an image will do:

Code:
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

Author:  srod [ Sun Aug 23, 2009 6:14 pm ]
Post subject: 

Well bugger me; I had tried that netty, but I used a 32-bit image which fails for some reason? I even tried a bitmap created via CreateCompatibleBitmap_() etc. I didn't think of trying a 24-bit image!

I wonder why the 32-bit image fails?

Author:  freak [ Sun Aug 23, 2009 6:31 pm ]
Post subject: 

> 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.

Author:  srod [ Sun Aug 23, 2009 6:39 pm ]
Post subject: 

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.


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...

**EDIT : it is indeed the image gadget! I understand now Freak; thanks. :)

Author:  netmaestro [ Sun Aug 23, 2009 6:51 pm ]
Post subject: 

Not sure how you were going about it my coderusty friend, but it's fine here, with the image gadget:
Code:
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

Did you remember to reference the original screen dc in CreateCompatibleBitmap? The memory dc won't have the right attributes.

Author:  srod [ Sun Aug 23, 2009 6:56 pm ]
Post subject: 

Same problem on my system.

The bitmap created is a 32-bit one and the image-gadget does not display the image correctly. Remove the image gadget and draw directly to the window and all is fine.

Author:  netmaestro [ Sun Aug 23, 2009 7:00 pm ]
Post subject: 

srod wrote:
Same problem on my system

Did you notice this part?
netmaestro wrote:
but it's fine here, with the image gadget

Does my last code fail on your system?

Author:  srod [ Sun Aug 23, 2009 7:01 pm ]
Post subject: 

Yes your code fails. My original test code is identical to yours including the use of the correct DC. :wink: The problem is that the image gadget seems to be treating the 32-bit image as entirely transparent.

Author:  netmaestro [ Sun Aug 23, 2009 7:02 pm ]
Post subject: 

Are you using 4.40 beta 2 or 1?

Author:  srod [ Sun Aug 23, 2009 7:03 pm ]
Post subject: 

Beta 2.

As I said above, the image gadget is treating the 32-bit image as entirely transparent.

Author:  netmaestro [ Sun Aug 23, 2009 7:08 pm ]
Post subject: 

Ok, iirc you are running Vista. I just tried my code (which is fine on XP) on my wife's Vista machine and it does indeed fail.

Author:  srod [ Sun Aug 23, 2009 7:12 pm ]
Post subject: 

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.

Page 1 of 2 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/