Page 1 of 1

Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 3:45 am
by IdeasVacuum
Anyone know how to do that programmatically?

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 3:57 am
by Derren
Not without parsing HTML or CSS.
CSS files can be included, so you'd have to follow the paths an search for the right values.

It's probably easier to check the "internal" size of the webgadget (scroll position). But I don't know how to do that.
Probably some Win-Api (hopefully^^)

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 1:50 pm
by RASHAD
Hi

Code: Select all

OpenWindow(0, 0, 0, 1024,840, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
WebGadget(0, 10, 10, 300, 65, "http://www.purebasic.com")
ButtonGadget(1,10,810,60,25,"Resize")

Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
          Quit = 1
     
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 1
               SetGadgetAttribute(0, #PB_Web_ScrollX, 2000)
               SetGadgetAttribute(0, #PB_Web_ScrollY, 2000)
               ResizeGadget(0,10,10,GetGadgetAttribute(0, #PB_Web_ScrollY)+60,GetGadgetAttribute(0, #PB_Web_ScrollY)+60)
          EndSelect
  EndSelect
Until Quit = 1


Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 3:19 pm
by MachineCode
@RASHAD, your example fails miserably with www.google.com (sorry to say).

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 4:11 pm
by IdeasVacuum
Hi Rashad

Well, funny thing is that the code only wants to work with "http://www.purebasic.com". I have tried several websites (mostly home pages) now and they all re-size to a small 45x45 square (including this forum) :shock:

So, I thought "maybe the Window has to be re-sized too", but nope, that makes no difference:

Code: Select all

Enumeration
#Win
#Web
#Btn
EndEnumeration

  OpenWindow(#Win,  0,   0, 1024, 840, "Resize WebGadget", #PB_Window_SystemMenu)
   WebGadget(#Web, 10,  10,  300, 300, "http://www.qinbiying.co.uk")
ButtonGadget(#Btn, 10, 810,   60,  25, "Resize")

Repeat

  Select WaitWindowEvent()
     
      Case #PB_Event_CloseWindow
          Quit = 1
     
      Case #PB_Event_Gadget
          Select EventGadget()
           Case #Btn
               SetGadgetAttribute(#Web, #PB_Web_ScrollX, 2000)
               SetGadgetAttribute(#Web, #PB_Web_ScrollY, 2000)

               iNewW.i = GetGadgetAttribute(#Web, #PB_Web_ScrollX) + 60
               iNewH.i = GetGadgetAttribute(#Web, #PB_Web_ScrollY) + 60

               ResizeWindow(#Win,0,0,iNewW + 10,iNewH + 10)
               ResizeGadget(#Web,10,10,iNewW,iNewH)
          EndSelect
  EndSelect

Until Quit = 1
Edit: I was wondering if GetGadgetAttribute with #PB_Web_ScrollX/Y was the culprit, but seems it could be SetGadgetAttribute, which I thought should set the WebGadget scroll bars to a specific position, but hard to tell if that works or not (not knowing the range):

Code: Select all

Enumeration
#Win
#Web
#Btn
EndEnumeration

Procedure OpenWin()
;------------------
  OpenWindow(#Win,  0,   0, 1024, 840, "Resize WebGadget", #PB_Window_SystemMenu)
   WebGadget(#Web, 10,  10,  500, 300, "http://www.qinbiying.co.uk")
SetGadgetAttribute(#Web, #PB_Web_ScrollX, 200)
SetGadgetAttribute(#Web, #PB_Web_ScrollY, 200)
ButtonGadget(#Btn, 10, 810,   60,  25, "Resize")

EndProcedure

OpenWin()

Repeat

  Select WaitWindowEvent()
     
      Case #PB_Event_CloseWindow
          Quit = 1
     
      Case #PB_Event_Gadget
          Select EventGadget()
           Case #Btn
               SetGadgetAttribute(#Web, #PB_Web_ScrollX, 2000)
               SetGadgetAttribute(#Web, #PB_Web_ScrollY, 2000)

               iNewW.i = GetGadgetAttribute(#Web, #PB_Web_ScrollX) + 60
               iNewH.i = GetGadgetAttribute(#Web, #PB_Web_ScrollY) + 60

               ResizeWindow(#Win,0,0,iNewW + 10,iNewH + 10)
               ResizeGadget(#Web,10,10,iNewW,iNewH)
          EndSelect
  EndSelect

Until Quit = 1
Edit Edit: Ha, my code above does not even work with "http://www.purebasic.com" :mrgreen:

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 4:38 pm
by IdeasVacuum
So, if the Window is re-arranged so that the resize button is always on screen:

Code: Select all

Enumeration
#Win
#Web
#Btn
EndEnumeration

Procedure OpenWin()
;------------------
  OpenWindow(#Win,  0,   0,  520, 350, "Resize WebGadget", #PB_Window_SystemMenu)
   WebGadget(#Web, 10,  40,  500, 300, "http://www.purebasic.com")
ButtonGadget(#Btn, 10,  10,   60,  25, "Resize")

EndProcedure

OpenWin()

Repeat

  Select WaitWindowEvent()
     
      Case #PB_Event_CloseWindow
          Quit = 1
     
      Case #PB_Event_Gadget
          Select EventGadget()
           Case #Btn
               SetGadgetAttribute(#Web, #PB_Web_ScrollX, 2000)
               SetGadgetAttribute(#Web, #PB_Web_ScrollY, 2000)

               iNewW.i = GetGadgetAttribute(#Web, #PB_Web_ScrollX)
               iNewH.i = GetGadgetAttribute(#Web, #PB_Web_ScrollY)

               ResizeWindow(#Win,0,0,iNewW + 20,iNewH + 50)
               ResizeGadget(#Web,10,40,iNewW,iNewH)
          EndSelect
  EndSelect

Until Quit = 1
...hitting resize once gets a wrong result, hitting it again gets a different wrong result :shock:

Can't work out an API approach from the MSDN info, but also it might be that the WebGadget is a special PB control anyway?

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 4:48 pm
by ts-soft
A good or modern WebSide have no fixed Size :mrgreen:
The WebSide should always use the size of your WebGadget and not the other way :wink:

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 5:08 pm
by IdeasVacuum
The WebSide should always use the size of your WebGadget and not the other way
Never happens though, unless the site has a mobile version. Some dynamic sites can get too big height-wise, requiring too much scrolling don't you think? I'm all for limiting web page dimensions to reasonable size and most are.

The reason I want the Gadget to resize is to be able to efficiently grab a snapshot image of the web page. The web gadget for this purpose would be hidden.

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 6:36 pm
by RASHAD
The reason I want the Gadget to resize is to be able to efficiently grab a snapshot image of the web page. The web gadget for this purpose would be hidden
In that case you can use the next code By Freak Modified By RASHAD
I posted that code earlier but no harm to post it again
Use SaveAs .mht

Code: Select all

; Constants for the ExecWB() method of IWebBrowser2

  Enumeration 1 
    #OLECMDID_OPEN          
    #OLECMDID_NEW          
    #OLECMDID_SAVE          
    #OLECMDID_SAVEAS          
    #OLECMDID_SAVECOPYAS    
    #OLECMDID_PRINT          
    #OLECMDID_PRINTPREVIEW    
    #OLECMDID_PAGESETUP      
    #OLECMDID_SPELL          
    #OLECMDID_PROPERTIES      
    #OLECMDID_CUT            
    #OLECMDID_COPY            
    #OLECMDID_PASTE            
    #OLECMDID_PASTESPECIAL    
    #OLECMDID_UNDO            
    #OLECMDID_REDO              
    #OLECMDID_SELECTALL        
    #OLECMDID_CLEARSELECTION  
    #OLECMDID_ZOOM              
    #OLECMDID_GETZOOMRANGE      
    #OLECMDID_UPDATECOMMANDS    
    #OLECMDID_REFRESH            
    #OLECMDID_STOP              
    #OLECMDID_HIDETOOLBARS    
    #OLECMDID_SETPROGRESSMAX    
    #OLECMDID_SETPROGRESSPOS    
    #OLECMDID_SETPROGRESSTEXT    
    #OLECMDID_SETTITLE            
    #OLECMDID_SETDOWNLOADSTATE  
    #OLECMDID_STOPDOWNLOAD    
    #OLECMDID_ONTOOLBARACTIVATED 
    #OLECMDID_FIND              
    #OLECMDID_DELETE            
    #OLECMDID_HTTPEQUIV        
    #OLECMDID_HTTPEQUIV_DONE    
    #OLECMDID_ENABLE_INTERACTION 
    #OLECMDID_ONUNLOAD          
    #OLECMDID_PROPERTYBAG2      
    #OLECMDID_PREREFRESH        
    #OLECMDID_SHOWSCRIPTERROR 
    #OLECMDID_SHOWMESSAGE      
    #OLECMDID_SHOWFIND        
    #OLECMDID_SHOWPAGESETUP    
    #OLECMDID_SHOWPRINT        
    #OLECMDID_CLOSE          
    #OLECMDID_ALLOWUILESSSAVEAS 
    #OLECMDID_DONTDOWNLOADCSS 
    #OLECMDID_UPDATEPAGESTATUS 
    #OLECMDID_PRINT2 
    #OLECMDID_PRINTPREVIEW2 
    #OLECMDID_SETPRINTTEMPLATE 
    #OLECMDID_GETPRINTTEMPLATE 
  EndEnumeration 

Enumeration 0
  #OLECMDEXECOPT_DODEFAULT     
  #OLECMDEXECOPT_PROMPTUSER       
  #OLECMDEXECOPT_DONTPROMPTUSER   
  #OLECMDEXECOPT_SHOWHELP       
EndEnumeration 

; Variant stuff

Enumeration 
  #VT_EMPTY           = 0
  #VT_NULL            = 1
  #VT_I2              = 2
  #VT_I4              = 3
  #VT_R4              = 4
  #VT_R8              = 5
  #VT_CY              = 6
  #VT_DATE            = 7
  #VT_BSTR            = 8
  #VT_DISPATCH        = 9
  #VT_ERROR           = 10
  #VT_BOOL            = 11
  #VT_VARIANT         = 12
  #VT_UNKNOWN         = 13
  #VT_DECIMAL         = 14
  #VT_I1              = 16
  #VT_UI1             = 17
  #VT_UI2             = 18
  #VT_UI4             = 19
  #VT_I8              = 20
  #VT_UI8             = 21
  #VT_INT             = 22
  #VT_UINT            = 23
  #VT_VOID            = 24
  #VT_HRESULT         = 25
  #VT_PTR             = 26
  #VT_SAFEARRAY       = 27
  #VT_CARRAY          = 28
  #VT_USERDEFINED     = 29
  #VT_LPSTR           = 30
  #VT_LPWSTR          = 31
  #VT_RECORD          = 36
  #VT_FILETIME        = 64
  #VT_BLOB            = 65
  #VT_STREAM          = 66
  #VT_STORAGE         = 67
  #VT_STREAMED_OBJECT = 68
  #VT_STORED_OBJECT   = 69
  #VT_BLOB_OBJECT     = 70
  #VT_CF              = 71
  #VT_CLSID           = 72
  #VT_BSTR_BLOB       = $0fff
  #VT_VECTOR          = $1000
  #VT_ARRAY           = $2000
  #VT_BYREF           = $4000
  #VT_RESERVED        = $8000
  #VT_ILLEGAL         = $ffff
  #VT_ILLEGALMASKED   = $0fff
  #VT_TYPEMASK        = $0fff
EndEnumeration

Structure BRECORD
  *pvRecord.l
  *pRecInfo.IRecordInfo
EndStructure


If OpenWindow(0, 0, 0, 600, 600, "WebBRowser", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)
    
    WebGadget(0, 10, 10, 580, 555, "http://www.google.com/")
    WebObject.IWebBrowser2 = GetWindowLongPtr_(GadgetID(0), #GWL_USERDATA)
    
    ButtonGadget(1, 10, 570, 100, 20, "Save as:")
    StringGadget(2, 115, 570, 300, 20, "C:\Test.html")
    ButtonGadget(3, 425, 570, 80, 20, "Copy")
    ButtonGadget(4, 510, 570, 80, 20, "Print")
    
Repeat
  Select WaitWindowEvent()
      
        Case #PB_Event_CloseWindow
          Quit = 1
       
        Case #PB_Event_Gadget
          Select EventGadget()
           Case 1            
              filename$ = GetGadgetText(2)
              
              ; convert to unicode
              filename_Unicode = AllocateMemory(Len(filename$)*2+2)
              MultiByteToWideChar_(#CP_ACP, 0, @filename$, -1, filename_Unicode, Len(filename$)*2+2)                
              
              ; create bstr
              filename_BSTR = SysAllocString_(filename_Unicode)
              
              ; fill variant
              VariantIn.VARIANT
              VariantIn\vt = #VT_BSTR
              VariantIn\bstrVal = filename_BSTR
                      
              If WebObject\ExecWB(#OLECMDID_SAVEAS, #OLECMDEXECOPT_PROMPTUSER, @VariantIn, 0) = #S_OK
                MessageRequester("","Saved successfully")
              Else
                MessageRequester("","could not save file")
              EndIf
              
              FreeMemory(filename_Unicode)
              SysFreeString_(filename_BSTR)
              
           Case 3
              WebObject\ExecWB(#OLECMDID_SELECTALL,#OLECMDEXECOPT_DONTPROMPTUSER,0,0)  ;Select All
              WebObject\ExecWB(#OLECMDID_COPY, #OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)
              WebObject\ExecWB(#OLECMDID_CLEARSELECTION,#OLECMDEXECOPT_DONTPROMPTUSER,0,0)
              MessageRequester("", GetClipboardText(), 0)
              
           Case 4
              WebObject\ExecWB(#OLECMDID_CLOSE, #OLECMDEXECOPT_PROMPTUSER, 0, 0)
              ;WebObject\ExecWB(#OLECMDID_PRINT, #OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)   ;No PrintRequester
          EndSelect
          
        Case #PB_Event_SizeWindow
            GetClientRect_(WindowID(0),r.RECT)
            ResizeGadget(0,10,10,r\right-r\left-20,r\bottom-r\top-45)
            ResizeGadget(1,10,r\bottom-30,100,20)
            ResizeGadget(2,120,r\bottom-30,300,20)
            ResizeGadget(3,r\right-175,r\bottom-30,80,20)
            ResizeGadget(4,r\right-90,r\bottom-30,80,20)
               
  EndSelect
Until Quit = 1
  
EndIf



Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 8:56 pm
by RASHAD

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 13, 2012 11:05 pm
by IdeasVacuum
That is excellent Rashad, thank you. Saving as an MHT file is good (added advantage of being able to use it as an email).

MHT Suits my purpose, but for anyone reading this, the MHT format has not kept up with the rest of the web world so only the simple web pages are captured 100%.

Saving as HTML Complete on the other hand does completely preserve the page, but obviously not as a single file.

Re: Resize WebGadget to WebPage size

Posted: Wed Mar 14, 2012 3:42 am
by RASHAD
Hi IdeasVacuum :mrgreen:
If you are not satisfied with MHT go blame M$ (If you can :P )

Re: Resize WebGadget to WebPage size

Posted: Wed Mar 14, 2012 4:16 am
by IdeasVacuum
..... they seem to have abandoned MHT. Great shame, years ago I used it to quickly make email news letters using that format (renamed to .eml). Mind you, MS Outlook/Express can use those files but Thunderbird does not like them.

Re: Resize WebGadget to WebPage size

Posted: Tue Mar 20, 2012 3:09 pm
by Seymour Clufley
MHT is dying a death because DataURI means that all resources can be included in the page as an HTML file. This code does it.

Re: Resize WebGadget to WebPage size

Posted: Thu Mar 22, 2012 4:24 am
by IdeasVacuum
...That's nothing short of brilliant Seymour, very nice work. 8)