Re: Transparent Picture Box Control
- From: "Mike Williams" <mikea@xxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jul 2007 07:32:06 +0100
"DanS" <t.h.i.s.n.t.h.a.t@xxxxxxxxxxxxxxxxxxxxx> wrote in message news:Xns99677744F6F81thisnthatadelphianet@xxxxxxxxxxxxxxxxx
I found this source code for a picturebox control that supports
transparency for area's w/o part of the picture in it. It seems to
work great.
http://www.freevbcode.com/ShowCode.asp?ID=4447
The problem is, that apparently I'm to dumb to figure out how this
works. It does no blt'ing or regioning, from what I can tell.
I've just had a look at that code and it uses a standard VB UserControl (with its BackStyle property set to Transparent) to achieve transparency, and it uses the standard VB PaintPicture method to draw the desired image into a hidden VB Autoredraw Picture Box (either normal size or stretched or tiled) so that it ends up with the drawn image (standard, stretched or tiled) in the Image property of the Autoredraw Picture Box. It then assigns that Image property to both the Picture property and the MaskPicture property of the UserControl and it assigns a colour value (by default the colour of the top left pixel of the original image) to the MaskColor property of the UserControl. This results in a userControl that shows the drawn image transparently on the Form on which it is placed. If you're not interested in stretching and tiling (as appears to be the case) then perhaps you would be better off forgetting about that code and creating your own UserControl instead, at least until you get yourself into a position where you understand what is going on so that you will be able to modify the existing code yourself. For a starter, try this:
1. Create a small (perhaps 100 x 100 pixel) drawing on a white
background in MS Paint or something and save it as a bmp.
2. Start a new VB project and use the menu Project / Add User
Control to add a new standard UserControl.
3. In the User Control's properties window set its BackStyle
property to 0 (transparent).
4. Click the button against its Picture property and set it to
the bmp file that you saved at step 1.
5. Do exactly the same with its MaskPicture property,
assigning exactly the same bmp file to it.
6. Set the User Control's MaskColor property to white.
Now click the little cross at the top right corner of the UserControl design window to close it and to come back to your Form. You should now see your UserControl available in your ToolBox. Double Click it in your ToolBox to add it to your Form. Now use the Form's properties window to set the Form's Picture property to a large picture that you have on your machine. You should see that the UserControl is displayed transparently over the background picture on your Form. You can then add code to move that UserControl around your Form to suit your own purposes. The UserControl itself looks after the transparency.
That sort of thing is exactly what the code you have downloaded does, except that it is complicated by the fact that it adds various accessible properties for ease of use and instead of using a "pre-drawn" bmp file for the User Control's Picture and MaskPicture properties it instead uses a bitmap that it draws into the hidden Autoredraw Picture Box, which it draws differently depending on whether you want standard, stretched or tiled. Now that you know what is going on you should be able to modify things yourself (much better than having someone show you how to do it). I'm not sure why you want your picture drawn into the centre of the UserControl though? Why not just make the User Control the same size as the picture? After all, the surrounding area is transparent so it might as well not be there. Or have I misunderstood somehting about your requirements.
By the way, as a sort of footnote, using UserControls to create transparent "sprites" in this way does make it very easy to draw and animate them on your Form, because the UserControl looks after the drawing and the transparency and your code simply needs to set the Left and Top properties to appropriate values at run time to move the sprites around. But do not be fooled into thinking that it is a quick way to draw and animate sprites, because it is not. The fact that your code only needs to alter a couple of values to effectively move the sprite around might lead youto believe that it is a very fast way of doing it, but it is nowhere near as fast as you think because the overheads that the system needs to deal with in order to display the transparent UserControl at its various positions on your Form are quite large and heavy in processor useage. Drawing sprites in the "old fashioned traditional way" by blitting images and masks in various combinations is actually faster, despite the fact that it requires quite a lot of code. DirectX of course is much faster still, but that's another matter entirely.
Anyway, once you have seen exactly what is going on with this "transparent User Control" business you should be able to deal with it yourself reasonably easily.
Mike
.
- Follow-Ups:
- Re: Transparent Picture Box Control
- From: DanS
- Re: Transparent Picture Box Control
- References:
- Transparent Picture Box Control
- From: DanS
- Transparent Picture Box Control
- Prev by Date: Re: Very slow code when loading a combo box with number 1 to 30000 (see code)
- Next by Date: Re: Very slow code when loading a combo box with number 1 to 30000 (see code)
- Previous by thread: Transparent Picture Box Control
- Next by thread: Re: Transparent Picture Box Control
- Index(es):