Making parts of form invisible

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I found and tried this example code:
VB.NET:
BackColor = Color.Red
    ' Make the background color of form display transparently.
    TransparencyKey = BackColor

This works well for making the whole form invisible, but I tried to make an image in Paint that was a 300x300 square with the corners painted red, hoping they'd disappear if I set that as the background image, but that didn't work. How would I go about doing that? Can I make a Paint image with parts painted red so those parts won't show? Also, using that code, most of the form was invisible, but I could still see the blue title bar and the blue around the whole edge of the form. Can I get rid of that too?
 
Yes you can make an image with transparent regions but you need to save the image to some lossless format. Saving images in lossy type formats such as gif or jpeg will compress the image which may cause the pixels to become different colors.
To be rid of the titlebar and border, set the FormBorderStyle property of the form to None
 
What format would be a lossless format? And if I wanted to do what I tried before about drawing something in the invisible color to take that section out, would it work to just set that image as the background? Thanks.
 
It's not entirely necessary to use a lossless type but if you do use a lossy type, you have to avoid anti-aliasing.
png and tiff image formats are lossless.
Setting the image as the background combined with setting the correct TransparencyKey will acomplish what you want.
Here's a good article: Coding4Fun: Tricking Out Your Applications
 
Thanks for that article. I found a program last night called ElementsEx that is supposed to be pretty easy for letting you choose your form shape and background. The examples look really good, but I think they're written in C#. Since I'm new to VB, I'd really like to see the VB code for doing it. The program also doesn't really have a help file for how to use it. Maybe they built the help into the Visual Studio help. I haven't looked yet.
 
You could do another thing though: instead of setting the image as the form's background, try this (it's from memory, so sorry if it has bugs):

Private Sub Form1_Paint(sender as Object, e as System.PaintEventArgs) Handles MyBase.Paint
Dim I as Image=Image.FromFile("C:\MyPic.gif")
e.Graphics.DrawImage(I, New RectangleF(0,0,Me.width,Me.height))
End Sub

If you use a gif, it's back image must be transparent, so you would see through it. Set the form's transparencykey to anything you want. Hope it helps.
 
In that case it is dead easy.....


Import the system.drawing.drawing2D namespace

You can have a form in literally any shape you like if you use a graphicspath.

set the forms borderstyle to none and set the control box property to none.

'Do this in the onpaint event of the form.

VB.NET:
Setstyle(controlstyles.DoubleBuffer,true)
Setstyle(controlstyles.AllpaintinginWMPaint,true)
setstyle(controlstyles.Userpaint,true)
setstyle(controlstyles.ResizeRedraw,true)
 
e.graphics.smoothingmode = smoothingmode.antialias
Dim Gp as new graphicspath
Gp.addellipse(1,1,me.width-2,me.height-2)
e.graphics.fillpath(new solidbrush(color.blue),gp)
e.graphics.drawpath(new pen(color.black,-1),gp)
gp.dispose

You can also use lineargradient brushes and pathgradient brushes to achieve a more professional effect.

Set the forms backcolor to control and also set the forms transparency key to control
'Or whatever color you are least likely to use.
 
Anothing thing you should know....
Any time you have a piece of c# code you'd like to reproduce in VB go here first.
http://www.kamalpatel.net/ConvertCSharp2VB.aspx

It does a fairly good conversion though it won't tell you if there are any additional refrences or imports you need to make. Only once since I started using this site have I had to retype from scratch a piece of c# code into VB. And that was my fault. I didn't grab enough of the original example for the vb code to work.
 
Back
Top