It is currently Thu Oct 29, 2020 3:34 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Draw a 'web-browser' on a hDC (Screen-output) (Windows only)
PostPosted: Sun Aug 23, 2009 8:14 am 
Offline
Moderator
Moderator
User avatar

Joined: Sat Apr 26, 2003 1:11 am
Posts: 1394
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 2:09 pm 
Offline
PureBasic Expert
PureBasic Expert

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

_________________
I may look like a mule, but I'm not a complete ass.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 2:45 pm 
Offline
Enthusiast
Enthusiast

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 3:27 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
Location: Fort Nelson, BC, Canada
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

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 6:14 pm 
Offline
PureBasic Expert
PureBasic Expert

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 6:31 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5813
Location: Germany
> 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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 6:39 pm 
Offline
PureBasic Expert
PureBasic Expert

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 6:51 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
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:
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.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 6:56 pm 
Offline
PureBasic Expert
PureBasic Expert

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 7:00 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
Location: Fort Nelson, BC, Canada
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?

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 7:01 pm 
Offline
PureBasic Expert
PureBasic Expert

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

_________________
I may look like a mule, but I'm not a complete ass.


Last edited by srod on Sun Aug 23, 2009 7:02 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 7:02 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
Location: Fort Nelson, BC, Canada
Are you using 4.40 beta 2 or 1?

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 7:03 pm 
Offline
PureBasic Expert
PureBasic Expert

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 7:08 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8143
Location: Fort Nelson, BC, Canada
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.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 23, 2009 7:12 pm 
Offline
PureBasic Expert
PureBasic Expert

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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 41 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye