Results 1 to 6 of 6

Thread: [RESOLVED] Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Resolved [RESOLVED] Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

    Hello everybody,
    I've recently searched all over the internet on how to make a transparent picturebox.
    I've found many threads, but they all used knowledge I didn't have.


    Basically, I have many pictureboxes that are used as a background. (I defined them as 'Tile x')
    Above them is an avatar that is supposed to walk freely on the map. Problem is, the avatar carries a background.
    Is there any way to make his background invisible without crazy 8-monkey codes?

    <a href="https://gyazo.com/6614a5c9b06ddd426cfde51cbe5ecbef"><img src="https://i.gyazo.com/6614a5c9b06ddd426cfde51cbe5ecbef.png" alt="https://gyazo.com/6614a5c9b06ddd426cfde51cbe5ecbef" width="720"/></a>



    Also, I read a thread about making a picturebox transparent (here) and I need to "Locate the constructor for my control class". If the solution is right, how do I do that?

    Thanks in advanced! :)

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Re: Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

    Please ignore the gyazo mess, I tried to attach an image.
    Here's a link instead.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

    I don't believe those methods will work.
    The picturebox being transparent means that you will see the form's background in the transparent area. Since you have pictureboxe tiles covering the form. The top picturebox will show the background of the form that is underneath the character, not the contents of the pictureboxes that are underneath it.

    One way you could do this, is use a borderless form to hold your character (assume it is named CharacterFrm), and use CharacterFrm.Show(Me) when showing the form so it will always appear above the main form. Now if you set its background color to transparent, you will see everything under that form around the character. A possibly major issue is that the character could walk right off the game window across your desktop.

    A better approach would be to draw your tiles and game characters, rather than use pictureboxes to hold them and move them.

    The following post has a few drawing examples I've posted before. You might find them entertaining, but perhaps complicated for your current needs and capability. I'm sure there are other example, from myself and others as well, but I don't have the time to look for them right now.
    http://www.vbforums.com/showthread.p...55#post5088355
    Last edited by passel; Jun 22nd, 2018 at 02:47 AM.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Re: Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

    Okay, thanks so very much.

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

    Re: Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

    Ignore the CodeProject answer you linked to. It is nonsense: you can set a PictureBox background to Transparent without extra code.

    If you do that, and your avatar really does have a transparent background, you have a limited kind of transparency. But in Windows Forms, transparency means showing the Parent background (usually the Form). Probably the grey background you are seeing in the avatar is the form's BackColor.

    Anyway, your game set-up with tiles in separate picture boxes will not work. There can only be one Parent at a time, so the avatar can't float over multiple tiles. Here's one possible way around that, assuming you start with the present layout of "tiles" in picture boxes in the Form designer.

    At runtime take a snapshot of the form and its visible controls using DrawToBitmap, and use that snapshot as the form's background image. Here's a code snippet you could use in the Form's Load sub:
    Code:
         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None 'temporarily hide the form border
          Dim bmp As New Bitmap(Me.Width, Me.Height)
          Me.DrawToBitmap(bmp, Me.ClientRectangle)
          Me.BackgroundImage = bmp
          Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
    You may also want to temporarily hide the avatar picture box, if you added that in the designer.

    Next hide all the tiles with the PictureBox.Hide method. You can still get the tile positions from the PictureBox properties (Left, Top etc.).

    BB

    edit: I see that Passel got in first (as usual). Using a separate form is a method I often recommend too. The way I suggested above is an alternative which doesn't have the "going off form" problem. There are other ways to solve it: make sure the top form always has the same size and position as the background form (using the Move and Layout event handlers) and animate your avatar on that form. I agree that drawing the background and avatar in the Paint sub is a cleaner solution. Among other reasons it allows partial transparency and hence anti-aliasing.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Re: Transparent Picturebox / Locating the constructor for my control class (NEWBIE)

    Thank you, Boops Boops and Passel!
    I've used Boop's solution and it worked perfectly!
    I'll try to learn how to paint in visual basic, and I'll use your advice Passel.
    Thanks again, this saved me and my project :P

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