Draw a 'web-browser' on a hDC (Screen-output) (Windows only)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Rings
Moderator
Moderator
Posts: 1403
Joined: Sat Apr 26, 2003 1:11 am

Draw a 'web-browser' on a hDC (Screen-output) (Windows only)

Post by Rings »

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 ?
SPAMINATOR NR.1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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: 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.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Post by UserOfPure »

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.....
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8189
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

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: 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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?
I may look like a mule, but I'm not a complete ass.
freak
PureBasic Team
PureBasic Team
Posts: 5833
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> 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.
quidquid Latine dictum sit altum videtur
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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. :)
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8189
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Not sure how you were going about it my coderusty friend, but it's fine here, with the image gadget:

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 
Did you remember to reference the original screen dc in CreateCompatibleBitmap? The memory dc won't have the right attributes.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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.
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8189
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

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?
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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.
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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8189
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Are you using 4.40 beta 2 or 1?
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Beta 2.

As I said above, the image gadget is treating the 32-bit image as entirely transparent.
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8189
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

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.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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.
I may look like a mule, but I'm not a complete ass.
Post Reply