Re: Form Transparency. Help!
Hmmm, you'd have to grab a picture of the background every time you loaded the form, and then cut the form out of the background (according to it's location) and then load that into the form's image, I'd guess.
Cutting a rectangle out would be one thing, but your shaped form makes it a lot harder, I'd guess.
Re: Form Transparency. Help!
What i want to do is basically what the ActiveX control ShapedForm does. Google "shapedform" and download to see what i want. I want to do the same with, except with code.
Re: Form Transparency. Help!
I'd just buy the control, and use that!
Re: Form Transparency. Help!
the control does a crappy job, i used it as a reference to make it more clear.
Re: Form Transparency. Help!
That's what I thought. It'd be a tall order to get perfect!
EDIT: I've been reading up on C#. Form Transparency is a property!
Re: Form Transparency. Help!
Quote:
Originally Posted by TCanuck42
That just makes a circle. Lets say I have a transparent picture of a trapezoid. How can I make the form diappear except for the part with the trapizoid on it?
All you have to do is draw a trapezoid on the form instead of a circle. :)
Re: Form Transparency. Help!
Rich's method is quite nice for this - as suggested, you need to draw whatever shape you want your form to be on the form, then set the area outside of this to the colour which is passed to SetLayeredWindowAttributes.
I don't have VB where I am at the mo, or I'd provide a demonstration. If you wanted a trapezoidal form with rounded corners you would have to build the shape using a combination of lines and circles, either at design time or runtime, then you could use the FloodFill API to fill the outside region with the colour designated for transparency. Then you would probably need to remove any construction lines that might remain, and voila!
If this isn't sorted when I visit CG later, I'll add a demonstration.
I'm assuming that this form is borderless - are you using SendMessage to simulate a click on the header bar so the form can be moved around?
Re: Form Transparency. Help!
Quote:
Originally Posted by dglienna
EDIT: I've been reading up on C#. Form Transparency is a property!
That's one of the Features I like about .NET!
Just look how easy it is to make a Transparent Form:
Code:
' Visual Basic
Public Sub Disappear()
Me.Opacity = 0.32
End Sub
The Opacity can be set to any value from 0.0 to 1.0
The lower the Opacity, the more Transparent.
I know it's a bit besides the fact, but I Just had to share..:D
1 Attachment(s)
Re: Form Transparency. Help!
Found this in my archive.
In Form_Load() it defines an array of POINTAPIs defining a complex shape. (Any trapez possible quite easily).
From this it creates a polygon region and then sets the window region to that.
Result is a form in a complex shape, all outlying parts fully transparent.
It has got a simple exit button to terminate.
Hope that helps, because it is very easy to accomplish.
1 Attachment(s)
Re: Form Transparency. Help!
I'm not sure how this code wouldn't help (I'm aware in the thread Dan_H linked to I didn't post the declarations, which I do here):
Code:
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Sub SetTransparency(piHwnd As Long, piColor As OLE_COLOR)
Const G_E = &HFFEC
Const W_E = &H80000
Const LW_KEY = &H1
SetWindowLong piHwnd, G_E, GetWindowLong(piHwnd, G_E) Or W_E
SetLayeredWindowAttributes piHwnd, piColor, 0, LW_KEY
End Sub
Private Sub Form_Initialize()
Call SetTransparency(Me.hwnd, Me.BackColor)
End Sub
All you need to do is set the BackColor to any other color besides the default (I'd opt for a color you likely won't use in your program).
If you want a shaped form, create an image of the shape you want to have and set the Form Picture to that image (make what you want to disappear the same color as the form's BackColor, or make it a GIF or PNG image with transparency so the form's BackColor will show through) and, voila, a shaped form, like so:
Re: Form Transparency. Help!
Re: Form Transparency. Help!
How did it not work? Was there an error, or did it not become transparent?
If it didn't become transparent, try setting the BackColor of the form to a different color (leaving it as the default or using any of the colors in the System tab will cause it to fail). If it had an error, can you point out the line the error occured on?
Re: Form Transparency. Help!
Quote:
Originally Posted by ChaosTheEternal
How did it not work? Was there an error, or did it not become transparent?
If it didn't become transparent, try setting the BackColor of the form to a different color (leaving it as the default or using any of the colors in the System tab will cause it to fail). If it had an error, can you point out the line the error occured on?
Its strange why system colours dont work, using system colours with some other GDI operations results in a black colour. I.e if you have a blue rectangle moving on a picture box with a system colour when the rectangle is drawn over it results in a black line.
Re: Form Transparency. Help!
Well, the reason I see system colors not working is because their values are different than standard colors. They don't use the regular RGB scheme since each system color is dependent off of the system settings and, in code, are reflected accordingly.
The API calls, though, aren't set to handle that information, and just fail due to an invalid parameter (though they fail without an error, sometimes a good thing, sometimes a bad thing).
1 Attachment(s)
Re: Form Transparency. Help!
As promised, one demonstration. A bit later than planned as I was mucking around with another idea, but anyway, see if this works for you.