Click here to Skip to main content
6,304,948 members and growing! (18,227 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Image Display     Intermediate License: The Code Project Open License (CPOL)

Alpha Blended Windows Forms

By SK Genius

Set 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
Posted:4 Oct 2007
Updated:3 Dec 2007
Views:31,319
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
17 votes for this article.
Popularity: 4.88 Rating: 3.96 out of 5
2 votes, 11.8%
1

2
2 votes, 11.8%
3
5 votes, 29.4%
4
8 votes, 47.1%
5
Blended form at 50% opacity
Form using AlphaBG, on movement fade, with a custom cursor set.

32-bit blending with the desktop
The corner of the form, blending onto the desktop.

Introduction

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.

Background

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.

Using the Code

As for using the code, I made it simple. Three steps is all you need:

  1. Create your Windows form
  2. Add a reference to AlphaBG.dll
  3. Add in a couple of lines of code

C# Code

[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);
}

VB Code

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.

Important - Minimizing your Form

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.

Important - Transparent Controls

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.

AlphaBG Settings

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.
    Note: You must set the Tag of the controls which will be affected to alpha.
  • All - Applies all of the settings.

How It All Works

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:

  1. The background and foreground don't move together
  2. ...

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.

Drawbacks

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.

History

  • 4-10-2007
    • Article posted
  • 5-10-2007
    • Updated demo.zip to contain source code
    • Added information about the settings
  • 3-12-2007
    • Updated source code and demo
    • Added VB startup code
    • Added screenshots
    • Edited article text

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SK Genius


Member
*blip*
Occupation: Software Developer
Location: United Kingdom United Kingdom

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralShowInTaskbar PinmemberShockerZz6:16 5 Mar '09  
GeneralSizable form Pinmembermihaipocorschi6:40 9 Feb '09  
GeneralRe: Sizable form PinmemberSK Genius14:22 9 Feb '09  
GeneralRe: Sizable form Pinmembermihaipocorschi22:56 9 Feb '09  
GeneralRe: Sizable form PinmemberSK Genius13:42 10 Feb '09  
GeneralRE: Love it but..... Pinmemberjberenguer15:41 20 Oct '08  
GeneralProblem Controls :( Pinmembereusta5:21 6 Feb '08  
GeneralNicely done PinmemberJeff J Anderson20:52 1 Jan '08  
GeneralCode for VB.NET Pinmembereusta11:54 25 Nov '07  
GeneralRe: Code for VB.NET PinmemberThe Undefeated12:21 25 Nov '07  
GeneralRe: Code for VB.NET PinmemberThe Undefeated12:37 25 Nov '07  
GeneralRe: Code for VB.NET Pinmembereusta13:52 25 Nov '07  
GeneralRe: Code for VB.NET PinmemberThe Undefeated14:01 25 Nov '07  
GeneralRe: Code for VB.NET Pinmembereusta14:33 25 Nov '07  
GeneralRe: Code for VB.NET PinmemberThe Undefeated14:37 25 Nov '07  
GeneralRe: Code for VB.NET Pinmembereusta14:43 25 Nov '07  
GeneralRe: Code for VB.NET PinmemberThe Undefeated14:50 25 Nov '07  
GeneralRe: Code for VB.NET Pinmembereusta15:27 25 Nov '07  
GeneralProblem with moving the form Pinmemberadixor4:35 24 Nov '07  
GeneralRe: Problem with moving the form PinmemberThe Undefeated5:09 24 Nov '07  
GeneralRe: Problem with moving the form Pinmemberadixor1:14 27 Nov '07  
GeneralRe: Problem with moving the form PinmemberSK Genius2:25 27 Nov '07  
GeneralRe: Problem with moving the form Pinmemberadixor6:32 27 Nov '07  
GeneralRe: Problem with moving the form PinmemberSK Genius8:29 27 Nov '07  
GeneralRe: Problem with moving the form Pinmemberadixor2:51 28 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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
Web11 | Advertise on the Code Project