![]() |
Multimedia »
General Graphics »
Image Display
Intermediate
License: The Code Project Open License (CPOL)
Alpha Blended Windows FormsBy SK GeniusSet a 32-bit image as the background for your Windows form |
C# (C# 2.0), VB, Windows, .NET (.NET 2.0), Visual Studio, WinForms, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I always liked adding custom backgrounds to my forms, as it makes them stand out in a crowd. But I was always limited to lines at 45 degrees, because if you had a curve... well, let's be honest, it looks awful. So I spent many an hour trying various methods of getting the background to be blended with the desktop, and eventually, I ended up with this.
And be warned, this is my first article. I know, almost three years, and I finally get up and do something.
This exists thanks to Rui Godinho Lopes, who wrote an article on creating layered Windows. This uses his code (PerPixelAlphaForm.cs), so check out the original article here. And don't forget to read the author's copyright notice.
As for using the code, I made it simple. Three steps is all you need:
[STAThread]
public static void Main(string[] args)
{
//Set up an instance of your form
MainForm myForm = new MainForm();
//Set up your background, all it needs is an image, your form
// and whatever settings you like (or no settings at all)
AlphaBG bg = new AlphaBG("..\\..\\app back.png", myForm,
AlphaBG.Settings.DrawControls |
AlphaBG.Settings.MovementFade);
//I also added in a function to set the cursor ;-)
bg.SetCursor("..\\..\\cursor.png", new Point(0,0));
//And go!
Application.Run(bg);
}
Protected Sub OnCreateMainForm()
Dim bg As New AlphaBG("Image.PNG", Me, AlphaBG.Settings.All)
bg.SetCursor("ImagePNG", New Point(0, 0))
bg.StartPosition = FormStartPosition.CenterScreen
bg.ShowDialog()
End Sub
'Thanks to eusta for this code snippet
AlphaBG is overloaded to take either a string that points to your image, or a Bitmap of the image, so you can keep your background in your resources.
And by default, none of the settings are activated, so you only need to define the things you want.
SetCursor is also overloaded to take a string, bitmap, or an actual cursor (you cannot define the hot point if you are passing an actual cursor).
The DLL and source come with XML documentation, so descriptions of functions or settings should come up in your Intellisense / code completion / whatever else.
Instead of calling:
this.WindowState = FormsWindowState.Minimized;
Call:
this.Owner.WindowState = FormsWindowState.Minimized;
Otherwise, your main form would minimize, leaving the background all alone on your desktop.
If you have a control with a transparent background, or your control gets a black background or border, you can have AlphaBG give it a background to avoid this problem.
To have AlphaBG set your control's background, simply set its tag to the text alpha, and make sure that the AlphaControls setting is set.
There are a variety of settings you can apply to your background to achieve the desired effect. For example, you may just want to have the 32-bit background, with no fading, and you don't mind the forms moving at slightly different times. (It's usually a little difficult to notice). For this, you simply don't enable any of the settings. But for everything else:
DrawControls - Draws the form controls to the background before the form is moved around the screen, which provides smoother movement. If you have a lot of controls, this may take some time, and cause a delay between the user dragging the form, and the form actually moving. MovementFade - Fades the form when the background is clicked (after a short delay), and then fades the form back in when the user releases the mouse. FadeClose - Fades out the form when the user closes it. FadeMainForm - If you are not drawing the controls to the background, then you can have the main form fade out with the background, else it would linger at full opacity while the background faded, until the application was closed. AlphaControls - Set this if you are using any controls that have a transparent/ translucent background. AlphaBG will then set the background image of these controls to match the background of your form. alpha. All - Applies all of the settings. Well, as you may (or may not) know, layered windows are quite limited. I'm not entirely sure what they can and can't do, but drawing controls didn't seem like too much fun. And adding in a function to update the window every time something changed seemed like too much of an effort.
So, all it does is start your form as a child window. This means that it will always be on top of the layered window, and now it handles all its own drawing. But of course, now you have two separate forms, which cause a few problems such as:
Nope, that was really the only problem. I tried setting the child windows position every time the background was moved, but then you had jumpy movement.
So, what happens (if you have DrawControls)?
Another handy feature is fading, AlphaBG will fade the window for you if you want. (Well, since you wanted some fancy shmancy 32-bit background, I thought you might want some nice effects). Now I did encounter a problem with this. If you use any custom 32-bit images on your form, or have a control with a transparent background, when the form faded, you got a dark patch around your control the same colour as the form's background colour.
So, to fix that, AlphaBG gets each control and set its background image to the area of the AlphaBG that it covers. Problem solved, no dark patches, and no one's the wiser. A downside is that you can't use background images on controls using this feature.
Note: You must have AlphaControls set, and have the controls tag equal to the text alpha.
There is one problem that still remains. If you have the option set to draw the controls when you move the form. If you have more than a few controls, then the form will lag while it draws everything, so the window may appear to 'jump' from one position to the next. But hopefully, you won't notice that too much. And besides, you don't have to enable the option if you don't want to.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Dec 2007 Editor: Deeksha Shenoy |
Copyright 2007 by SK Genius Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |