locked
Determining actual BackColor of Control. RRS feed

  • Question

  • I have a user control that hosts a WebBrowser control in order to display some formatted text.

    I want to set the background of the document in the WebBrowser such that it blends in and it is not apparent that a WebBrowser control is being used at all.

    Is there any property that gives the actual BackColor of the control?

    I've found that when hosted on a tab page and with VisualStyles enabled the BackColor is #FBFCFF but can't see an obvious way of determining this at run time.

    Sunday, July 17, 2011 1:55 PM

All replies

  • If you mean the background color of the webpage presented in the control, then that probably will be difficult.  It may be in the body element of the html, or in a css class referenced by that, or a div that occupies the entire page, just to name a few ways the background can be set in a page.
     

    --
    Mike
    Sunday, July 17, 2011 2:21 PM
  • No. That isn't what I mean.

    I'm setting the WebBrowser contents myself

    myWebBrowser.DocumentText = @"
    <html>
    <head>
      <title></title>
    </head>
    <body style=""background-color: #FBFCFF; font-family: Microsoft Sans Serif; font-size: 8.25pt;
      padding: 0; margin: 0"">
      <!-- Other HTML goes here -->
    </body>
    </html>";
    

    But I don't want to hardcode the background colour as it will be wrong if the user control is not hosted on a tab page with visual styles (and perhaps not even guaranteed there either?) so I would like to retrieve that programatically somehow.

    Sunday, July 17, 2011 2:34 PM
  • Where ever you need the background of the host, you can use "this.Parent.BackColor" to get the host background color.  You would need to generate the html dynamically to match that to get the effect you want.
     

    --
    Mike
    Sunday, July 17, 2011 2:42 PM
  • That doesn't work.

    The background colour property of the Parent TabPage is Transparent but when  UseVisualStyleBackColor is set to true and Application.EnableVisualStyles(); is set (both of which happen by default when creating a new WindowsForms project in VS2010) it doesn't actually behave in a transparent manner. Instead it gets given an off white colour (at least for me on XP. Not sure if this is OS dependant)


    Sunday, July 17, 2011 2:56 PM
  • Bumping this in case anyone knows the answer...

    Saturday, August 6, 2011 2:11 PM
  • No. That isn't what I mean.

    I'm setting the WebBrowser contents myself

     

    myWebBrowser.DocumentText = @"
    <html>
    <head>
     <title></title>
    </head>
    <body style=""background-color: #FBFCFF; font-family: Microsoft Sans Serif; font-size: 8.25pt;
     padding: 0; margin: 0"">
     <!-- Other HTML goes here -->
    </body>
    </html>";
    

    But I don't want to hardcode the background colour as it will be wrong if the user control is not hosted on a tab page with visual styles (and perhaps not even guaranteed there either?) so I would like to retrieve that programatically somehow.

     


    So what color do you want to retrieve? Isn't the container's backcolor so that to make the webbrowser unnoticable used?
    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 9, 2011 7:40 AM
  • I've already explained earlier in the thread and can't think of anyway of making things clearer. If you try the following in a new VS2010 Windows Forms project you will see what I mean.

    1. Create a user control containing a web browser control and code that populates the web browser with an HTML document.

    2. On a Windows Form add a tab page control. Add 2 instances of the UserControl one on the tab page and one off it.

    3. Attempt to get both instances of the web browser control to blend in to their respective backgrounds. You will find that Parent.BackColor is useless for the control on the tabpage.

     

     

    Thursday, August 11, 2011 12:55 PM