Results 1 to 10 of 10

Thread: [Resolved]Rectangles

  1. #1

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    33

    [Resolved]Rectangles

    ok, i know how to draw a rectangle and fill it but how do i make it visible over a WebBrowser control, also how would i make the FillRectangle 60% transparent? i have found no understandable results on google. could someone please help me figure this out?

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim myGraphics As Graphics
    Dim MyBrush As New SolidBrush(Color.FromArgb(128, 0, 0, 255))
    myGraphics = Graphics.FromHwnd(TextBox1.Text)
    myGraphics.FillRectangle(MyBrush, New Rectangle(0, 0, 100, 100))

    End Sub
    Last edited by vipkid; Jun 29th, 2010 at 01:23 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Rectangles

    I don't think you're going to be able to do what you want. Generally speaking, to draw on a control you handle its Paint event and then do your GDI+ drawing there. I'm fairly sure that the WebBrowser control doesn't support that though. You could just stick a Panel over the top of the WebBrowser but then you can't make it transparent. If what you want is possible then I would expect it to require complex unmanaged code. Maybe it would be easier in WPF than in WinForms.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Rectangles

    Hi Vipkid,
    You can use an ordinary form to show a partly transparent rectangle in front of the web browser. You need to set various properties of the form to make it work well. If the user can move/resize the form, code the forms Move and Resize events to keep them aligned; otherwise set the bounds in a similar way in the Load event. The following code goes on the form (Form1) which hosts the web browser (WebBrowser1):

    vb.net Code:
    1. Private WithEvents frm As New Form With _
    2.     {.FormBorderStyle = Windows.Forms.FormBorderStyle.None, .Owner = Me, _
    3.      .StartPosition = FormStartPosition.Manual, .Visible = False, _
    4.      .ShowInTaskbar = False, .BackColor = Color.Gray, .Opacity = 0.6}
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         If frm.Visible Then
    8.             frm.Hide()
    9.         Else
    10.             frm.Show(Me)
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move, Me.Resize
    15.         frm.Bounds = RectangleToScreen(WebBrowser1.Bounds)
    16.     End Sub
    You only need the WithEvents keyword if you want to do something with frm's events, for example the Click event.


    @jmcilhinney. John, I never see anyone else recommending forms for solving this kind of transparency problem. Is there some good reason for not doing so?

    BB
    Last edited by boops boops; Jun 29th, 2010 at 06:00 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Rectangles

    Quote Originally Posted by boops boops View Post
    @jmcilhinney. John, I never see anyone else recommending forms for solving this kind of transparency problem. Is there some good reason for not doing so?
    It's not really something I considered. It is a hack, and it may have issues under certain unforeseen circumstances, but sometimes a hack is the best option. If it works, it works.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Rectangles

    Quote Originally Posted by jmcilhinney View Post
    It's not really something I considered. It is a hack, and it may have issues under certain unforeseen circumstances, but sometimes a hack is the best option. If it works, it works.
    I'm not sure what you mean by a "hack" here. What kind of issues do you have in mind? Really it's just a modeless dialog box without a dialog.

    After playing around with it for a while I did find a small flaw. If the user manually resizes the main form to so small that the browser is invisible, a small square of frm can remain visible. The Move/Resize event can be improved to eliminate it:
    vb Code:
    1. Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move, Me.Resize
    2.         Dim r As Rectangle = Rectangle.Intersect(WebBrowser1.Bounds, Me.ClientRectangle)
    3.         If r.Width <= frm.MinimumSize.Width OrElse r.Height <= frm.MinimumSize.Height Then
    4.             frm.Hide()
    5.         Else
    6.             frm.Bounds = RectangleToScreen(r)
    7.         End If
    8.     End Sub
    Does that count as a hack or as a refinement?

    BB
    Last edited by boops boops; Jun 29th, 2010 at 12:27 PM.

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Rectangles

    I would definitely consider it a hack to have a partly transparent form act as if it is a drawing on the webbrowser. The point is that something is acting like it is something else. But hacks are not always bad, and I don't see any other way either so I'd say go for it.

  7. #7

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    33

    Re: Rectangles

    Thanks heaps boops boops, this has helped me heaps, its very simple but effective, there was no issue with the resize the form is locked to a certain size so the first code worked perfect with a few alterations.

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Rectangles

    Quote Originally Posted by NickThissen View Post
    I would definitely consider it a hack to have a partly transparent form act as if it is a drawing on the webbrowser. The point is that something is acting like it is something else. But hacks are not always bad, and I don't see any other way either so I'd say go for it.
    I can see the point of preferring PictureBoxes to show pictures, Radiobuttons for exclusive options and so on. But a Windows Form is hugely versatile. You can use it as a free-floating dialog box, a conventional UI main form or the child of another form. Besides hosting controls, it has special properties to support graphical uses such as DoubleBuffered, Opacity and TransparencyKey.

    So I don't see what is wrong with using a form as a purely graphical element as in the OP's question. I am not sure how to check this, but I get the impression it requires no more extra memory than a PictureBox or a Panel. It's not as though I were suggesting any obscure coding tactics: there's not an API or a WndProc in sight. It just takes one line in the Load event, frm.Show(Me), and small Move/Resize handler to keep it in place. Everything else can be selected in the designer, apart from Owner= which is actually superfluous since the ownership is set in the Show statement.

    Calling something a hack conveys a sense of unreliability. It "may have issues" or "is not always bad" as you put it. So my question stands. I would still like to know whether there is really something unreliable about using forms this way.

    BB

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [Resolved]Rectangles

    I never said a hack was something bad. Especially if there are no other options, a hack is obviously good because it's the only way to do what you want. If there are other options that give you the exact same behavior with 'better', more conventional methods, then of course the hack is not recommended, but that doesn't seem to be the case here.

    I only call it a hack because it is a 'work around' for something that is impossible (painting on a webbrowser).

    As for unreliable things with this method, I can't really think of anything, but that doesn't mean there aren't any. That's the "danger" of these things: you can't really be sure that it will work 100&#37; of the time. The fact remains that there is no 'hard' link between the 'painting form' and the underlying webbrowser. If you close all loop holes, then theoretically you could mimic such a link but you can't be sure if you know all loop holes. Of course, with sufficient testing you can make sure that nothing will go wrong in most of the cases. I've seen you recommend this method for various other things too, so I expect it will be tested sufficiently by now so it should all be fine.

    I guess what I'm trying to say is that windows doesn't know about your connection between these two forms, so there is no reason to assume that it will go right. I can imagine windows hiding one of the windows, for some reason, while not hiding the other. It tends to do stuff like that once in a while

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [Resolved]Rectangles

    Quote Originally Posted by NickThissen View Post
    I never said a hack was something bad. Especially if there are no other options, a hack is obviously good because it's the only way to do what you want. If there are other options that give you the exact same behavior with 'better', more conventional methods, then of course the hack is not recommended, but that doesn't seem to be the case here.

    I only call it a hack because it is a 'work around' for something that is impossible (painting on a webbrowser).

    As for unreliable things with this method, I can't really think of anything, but that doesn't mean there aren't any. That's the "danger" of these things: you can't really be sure that it will work 100&#37; of the time. The fact remains that there is no 'hard' link between the 'painting form' and the underlying webbrowser. If you close all loop holes, then theoretically you could mimic such a link but you can't be sure if you know all loop holes. Of course, with sufficient testing you can make sure that nothing will go wrong in most of the cases. I've seen you recommend this method for various other things too, so I expect it will be tested sufficiently by now so it should all be fine.

    I guess what I'm trying to say is that windows doesn't know about your connection between these two forms, so there is no reason to assume that it will go right. I can imagine windows hiding one of the windows, for some reason, while not hiding the other. It tends to do stuff like that once in a while
    The relationship between them is rock solid. An Owned Form is locked to its owner in the Z-order, and they are automatically hidden, minimized and closed together. If one is Topmost, so is the other. As my example above shows, coordinating them in the XY direction is a simple task. The relation is no less secure than that between an MDIParent and an MDIChild.

    The Framework provides ample support for Owned forms. There are at least three ways to set Form A as the owner of Form B: B.Owner=A, A.AddOwnedForm(B) and B.Show(A). Other Form members associated with ownership are RemoveOwnedForm and the OwnedForms property (array of Forms). MSDN gives a modeless Search/Replace window as an example for an owned form but that is clearly not exclusive.

    So it surprises me that so many people seem unaware of Owned Forms. Combined with the Form.Opacity property, it opens up many possibilities for using superimposed forms for graphic purposes. See for another example my CodeBank contribution "Very Simple Slide Show with Cross Fading".

    BB
    Last edited by boops boops; Jun 30th, 2010 at 08:14 AM.

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