Results 1 to 17 of 17

Thread: Suggestions on WebBrowser and form sizing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Suggestions on WebBrowser and form sizing

    I am trying to find a way to have the form on which there is a WebBowser control automatically size to the size of the content being displayed in the WebBrowser. The particular function is making a call to a very specific site which will display varying bits of information thus causing the WebBorwser to generate a scroll bar or not. What I want to accomplish is the form and WebBrowser to automatically grow or shrink based on what is on the page, which I won't know in advance. I don't seem to find a way to determine the height and width the browser needs to be fully displayed. I know I can set the forms height and width dynamically, but determining what size I need after the document loads is the issue.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Re: Suggestions on WebBrowser and form sizing

    No suggestions?

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    Personally, I see no way to do that. How can you pre-determine the size of a control and Form based on info you have no way of knowing what it is. Even if you could I think your Form would become very distracting to anyone using it as the whole Form/Control resizes everytime it's used and what if it resizes beyond the limits of the screen. Depending on what you are doing with the info on the page and it's graphical contents you might be able to rebuild the page to fit your fixed Form/Control size but that would be a very difficult task I think
    Last edited by jmsrickland; May 7th, 2015 at 11:19 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Re: Suggestions on WebBrowser and form sizing

    I found this thread and it sounds like exactly what I am looking for but VB does not like the two DIM lines.

    http://www.vbforums.com/showthread.p...rowser-control

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Suggestions on WebBrowser and form sizing

    The link you gave us is for .Net, not VB. The .Net webbrowser control isn't the same as the one VB uses. However, if the webbrowser does offer a Document object that offers a Body object that offers a ScrollRectangle property, you would need a RECT and SIZE structure definition added to your form/module. Verify first that ScrollRectangle property exists somewhere. Suggest pressing F2 (Object Browser) and typing ScrollRectangle in the search box and see if you get lucky. I am not on a box with VB installed, so I can't verify it for you.

    FYI
    Code:
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Type SIZE
        Cx As Long
        Cy As Long
    End Type
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Suggestions on WebBrowser and form sizing

    Quote Originally Posted by mickeykelley View Post
    I found this thread and it sounds like exactly what I am looking for but VB does not like the two DIM lines.

    http://www.vbforums.com/showthread.p...rowser-control
    For VB6 you can try it this way (place WebBrowser1 on a Form beforehand):

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      WebBrowser1.Navigate2 "google.com"
    End Sub
    
    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
      With WebBrowser1.Document.documentElement
        Debug.Print .scrollWidth, .scrollHeight
      End With
    End Sub
    Olaf

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    It will change the Form but not the control since the .scrollWidth and .scrollHeight is the same as the control Width and Height


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Suggestions on WebBrowser and form sizing

    Quote Originally Posted by jmsrickland View Post
    It will change the Form but not the control since the .scrollWidth and .scrollHeight is the same as the control Width and Height
    No, it is not (at least not in my test I just did on Win8) -
    the Control.Width and .Height differs from the scrollWidth and -Height of its content.

    If you make the Control somewhat smaller on your form - and then do this test here:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      ScaleMode = vbPixels
      Debug.Print WebBrowser1.Width, WebBrowser1.Height
      
      WebBrowser1.Navigate2 "google.com"
    End Sub
    
    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
      With WebBrowser1.Document.documentElement
        Debug.Print .scrollWidth, .scrollHeight
      End With
    End Sub
    You will see that it prints out different values - e.g. on my machine I got:
    Code:
     301           249 
     980           460
    (the larger values being the Documents scrollWidth/Height - and the quite small sized Control then showing both ScrollBars)

    Olaf

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    Over and over again I ran you code and over and over I get the same results. I'm running IE 7.0 on XP.

    Code:
    228           274
    228           274
    228           274
    228           274
    So, it will work for some and not work for others. Let's hope it will work for OP
    Last edited by jmsrickland; May 8th, 2015 at 11:46 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Suggestions on WebBrowser and form sizing

    Hmm, just ran it on two XP-VMs I have - one quite old (still with IE6) -
    the other one updated (running IE8) - and on both machines I cannot
    reproduce what you say:

    I refrain from posting the result of the XP-IE8-Version - but here's a ScreenShot from the old XP-VM I've just taken:




    Maybe try to navigate to a different URL than Google - just ensure that you
    have the IE-Control that small, that the site you navigated to is enforced to show Scrollbars.


    Olaf

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    I navigate to my own site and each time I run project I change physical size of control and always make it smaller than page size.

    I also did google because I copied your code without change then I used my site. I also tried Navigate as well as Navigate2


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Suggestions on WebBrowser and form sizing

    Quote Originally Posted by jmsrickland View Post
    I navigate to my own site and each time I run project I change physical size of control and always make it smaller than page size.

    I also did google because I copied your code without change then I used my site. I also tried Navigate as well as Navigate2
    Seems like you didn't use the code exactly as I posted it (although claiming so in your post #9 -
    that's not nice, because it has cost me 10minutes or so (digging out the old XP-VM) - for nothing.

    Why can't you post the concrete snippet of code (exactly as you have it, including the
    differing navigation-target and your resizing-attempts)?

    This would make it much easier to help (in case you need any, and describe what you want to achieve).

    Olaf

  13. #13
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    Actually your code didn't work. It had an error so I had to do something to prevent the error, so, yes, it was not exactly the same but it was basically likes yours.

    This is the code from my project as you posted it

    Code:
    Option Explicit
    
    Private Sub Form_Load()
     ScaleMode = vbPixels
    
     Debug.Print WebBrowser1.Width, WebBrowser1.Height
    
     WebBrowser1.Navigate2 "google.com"
    End Sub
    
    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
      With WebBrowser1.Document.documentElement
        Debug.Print .scrollWidth, .scrollHeight
      End With
    End Sub
    The highlighted line gives error: Object variable or With Block variable not set

    So, I made a slight change like this

    Code:
    Option Explicit
    
    Private Sub Form_Load()
     ScaleMode = vbPixels
    
     Debug.Print WebBrowser1.Width, WebBrowser1.Height
    
     WebBrowser1.Navigate2 "google.com"
    End Sub
    
    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
     If InStr(URL, "google.com") > 0 Then
       With WebBrowser1.Document.documentElement
         Debug.Print .scrollWidth, .scrollHeight
       End With
     End If
    End Sub
    Then I changed it to WebBrowser1.Navigate2 "www.futureedge.com"

    Then I changed it to WebBrowser1.Navigate "google.com"

    Then I changed it to WebBrowser1.Navigate "www.futureedge.com"

    In addition I made the control size different in the design mode each time I ran the project


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Suggestions on WebBrowser and form sizing

    Well, what shall I do, other than pointing out which methods to use, to
    determine the scrollrange in case the document is larger than the viewport.

    When they don't work, then you're doing something wrong in your code
    (as e.g. not showing your resizing code or whatever you were meaning
    with your mentioning of it).

    You also need to learn, how to check whether an Object is nothing -
    filtering out all URLs but google will not help much to fix your code.

    Olaf

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    Quote Originally Posted by Schmidt View Post
    Well, what shall I do, other than pointing out which methods to use, to
    determine the scrollrange in case the document is larger than the viewport.

    When they don't work, then you're doing something wrong in your code
    (as e.g. not showing your resizing code or whatever you were meaning
    with your mentioning of it).
    There is no resizing code. I stated I did that in design mode. I posted exactly what I have. When I ran your code exactly as you posted I got that error so all I did was add that addition If statement. Besides, it shouldn't matter how one navigates to any website if it doesn't work it doesn't work which tells me your code only works for you under your exact circumstances and my code doesn't work for me under my circumstances so it isn't foolproof.


    Quote Originally Posted by Schmidt View Post
    You also need to learn, how to check whether an Object is nothing -
    filtering out all URLs but google will not help much to fix your code.

    Actually that was just for this example only. Ordinaraly I don't use hard code like that I use a variable with the URL I navigate to but in this example I hard coded google. You need to learn that when we post code here that they are examples only and may or may not reflect the actual code that the programmer uses for other things.
    Last edited by jmsrickland; May 8th, 2015 at 11:29 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Suggestions on WebBrowser and form sizing

    Well I finally got it to at least get different sizes. I can't get it to resize correctly for any site. I did msn.com and it almost resizes correctly except it still leaves the scrollbars where just a very small amount needs to be scrolled. Google does not come out right; I get 618 x 436 where you get 980 x 460. Other sites do not change at all.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Suggestions on WebBrowser and form sizing

    Quote Originally Posted by jmsrickland View Post
    Well I finally got it to at least get different sizes. I can't get it to resize correctly for any site. I did msn.com and it almost resizes correctly except it still leaves the scrollbars where just a very small amount needs to be scrolled. Google does not come out right; I get 618 x 436 where you get 980 x 460. Other sites do not change at all.
    Since you seem unable to enhance it (getting it right from my suggested approach) -
    I guess I have to deliver code which is more suitable for Copy&Paste ... <sigh>.

    So, here we go (although this is now a little enhanced from what I've posted,
    you still should do your own adaptions, because it's still only a simple example).

    What I left out in the previous example was early-binding to the types defined in the:
    "MS HTML Object Library"
    (which should now be included over the Reference-Dialogue)

    Open a new, empty Std-Exe-Project in VB6:

    Into Form1 (without placing any Controls on it), paste the following code:
    Code:
    Option Explicit
    
    Private Sub Form_Click()
      Form2.ShowAndResizeTo "google.com", Me
    '  Form2.ShowAndResizeTo "www.futureedge.com", Me
    '  Form2.ShowAndResizeTo "msn.com", Me
     
    End Sub
    Now add an additional Form (Form2) - place a WebBrowser1-Control on it, and paste the following Code:
    Code:
    Option Explicit
    
    Private mURL As String, mMaxWidth As Long, mMaxHeight As Long, mStopIt As Boolean
     
    Public Sub ShowAndResizeTo(URL As String, Optional ParentForm As Form)
      BackColor = vbMagenta
      ScaleMode = vbPixels
      WebBrowser1.Move 0, 0, 20, 20
      mStopIt = False
      mURL = vbNullString
     
      WebBrowser1.Navigate2 URL
      Show , ParentForm
    End Sub
     
    Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
      If Len(mURL) = 0 Then mURL = URL
    End Sub
     
    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
      If Len(mURL) And Not mStopIt Then
        Dim Body As HTMLBody, DocE As OldHTMLDocument, Wnd2 As HTMLWindow2
     
        On Error Resume Next
          Set DocE = pDisp.Document.documentElement
          Set Body = pDisp.Document.Body
          Set Wnd2 = WebBrowser1.Document.Script.window
        On Error GoTo 0
    
        If Not DocE Is Nothing Then
          If mMaxWidth < DocE.scrollWidth Then mMaxWidth = DocE.scrollWidth
          If mMaxHeight < DocE.scrollHeight Then mMaxHeight = DocE.scrollHeight
        End If
        If Not Body Is Nothing Then
          If mMaxWidth < Body.scrollWidth Then mMaxWidth = Body.scrollWidth
          If mMaxHeight < Body.scrollHeight Then mMaxHeight = Body.scrollHeight
        End If
        
        If URL = mURL Then
          mStopIt = True
          
          Caption = "MaxWidth: " & mMaxWidth & ", MaxHeight: " & mMaxHeight
    
          If Not Wnd2 Is Nothing Then
            If mMaxWidth > Wnd2.Screen.availWidth Then mMaxWidth = Wnd2.Screen.availWidth
            If mMaxHeight > Wnd2.Screen.availHeight Then mMaxHeight = Wnd2.Screen.availHeight
          End If
          
          WebBrowser1.Move 0, 0, mMaxWidth + 20, mMaxHeight + 20
          'Resizing the Form around the now resized Control is left as an exercise to the reader!
        End If
      End If
    End Sub
    The above (after clicking Form1) will result in the following (on a german Win8):
    (I've made the BackColor of the Form2 magenta, to make it more obvious, where the Ctl-Resizing ended)



    I've also tested it on XP+IE6 (to make sure the used Types from the MS-HTML-ObjectLib are compatible)



    And here again a Shot from Win8 - this time navigating to "msn.com" (the determined MaxScroll-Sizes are
    reported in the Caption), to give an example for a site which expands enormously downwards
    (though the code already contains a safeguard, to not resize the MS-BrowserControl to these huge
    y-Dimensions... but as can be seen again by the magenta Background - at least the Ctl-Width was set):


    Feel free to come up with other rude replies, on what I did wrong *this time* whilst helping you on your way.

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width