Add Flare with Paint and Transparency Keys to your C# projects

AID: 4382
  • Status: Published

1920 points

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can be easily solved by simply using a graphics creation tool such as Paint, Gimp, or Adobe Photoshop and the Transparency key tool found within the C# program.

To ensure you have the correct amount of space for all form objects needed and even room for adding features down the road, design of your new program becomes the key to a successful build. In keeping with that tradition of design we will need to first layout how we would like our form to look at the end of our project.

For this example we will be creating a mock MP3 Player. I have decided to make the form appear in the shape of a banner with the display information and buttons in the center. Here is how I would like my form to look when it is complete:

 
image001.jpg
  • 4 KB
  • Screen1
Screen1


As you can tell I am not an artist but for the purpose of this article let’s pretend it is the greatest design ever. You can do this step with nothing but a scratch piece of paper and pen just so you have a visual goal to work towards. Next we want to conceptualize a layout on the form to see if it meets your needs.

image002.jpg
  • 9 KB
  • Screen 2
Screen 2


Once again, despite the artistry it appears to be in line with my concept.

The next step is where we turn idea into reality. From here going forward we will need to use an image creation program. I will use the basic MS PAINT program to create the form mask that we need. In your program of choice re-create the form that you want to see. It is best to put it in the middle of a slightly larger image. Remember to outline where your form items will be located.

image003.jpg
  • 20 KB
  • Screen 3
Screen 3


Next we want to fill the surrounding area with a solid color. This color should usually be something obnoxious as it will be easier to locate later down the line. In this example I choose lime green. The white area where your objects will be placed should also be colored to your liking at this point.

image004.jpg
  • 20 KB
  • Screen 4
Screen 4


This final mask image will show our form in the middle area and the surrounding green area as the mask. Save the image to your desktop or an equally accessible location.

Now we will move to the Visual Studio / C# portion of this process. Create a new Windows Application in Visual Studio and add a PictureBox to the form. Next you will want to select the image mask you just created.
Expand the image and form until it fits correctly. At the end your form should look similar to this example:

image005.jpg
  • 22 KB
  • Screen 5
Screen 5


In the properties box we will be setting the FormBorderStyle property to “None” to eliminate the top blue title bar. This is where our first bit of code comes into play. Because of a lack of the title bar we need to move the form directly. To achieve this effect you will need to add the following:

[At the top]

using System.Runtime.InteropServices; 
                                    
1:

Select allOpen in new window



[Above public Form1()]

public static extern bool ReleaseCapture();
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,
                 int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")][In pictureBox1_MouseDown event]

private void Form1_MouseDown(object sender,
        System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window




Source: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx

You should now be able to move your form without a title bar.

The final step is setting the transparency key. Please note that you need to select Form1 for this and not the picturebox. From the properties box scroll down to TransparencyKey and set it to the same obnoxious color at your pictures background.

Add and code your objects, run the program and there you have it, a sweet looking form ready to impress your end users.

image006.jpg
  • 22 KB
  • Screen 6
Screen 6



If you are unhappy with the layout just re-draw another form and try again. The possibilities are endless.
    Asked On
    2011-01-19 at 12:47:08ID4382
    Tags
    Topic

    C# Programming Language

    Views
    1198

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top C# Experts

    1. kaufmed

      566,376

      Sage

      500 points yesterday

      Profile
      Rank: Genius
    2. BuggyCoder

      240,764

      Guru

      10 points yesterday

      Profile
      Rank: Sage
    3. navneethegde

      158,560

      Guru

      0 points yesterday

      Profile
      Rank: Wizard
    4. CodeCruiser

      140,708

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    5. TheLearnedOne

      137,350

      Master

      2,800 points yesterday

      Profile
      Rank: Savant
    6. wdosanjos

      124,511

      Master

      3,500 points yesterday

      Profile
      Rank: Genius
    7. AndyAinscow

      107,357

      Master

      0 points yesterday

      Profile
      Rank: Genius
    8. Dhaest

      98,335

      Master

      0 points yesterday

      Profile
      Rank: Genius
    9. Idle_Mind

      91,914

      Master

      0 points yesterday

      Profile
      Rank: Savant
    10. tommyBoy

      90,068

      Master

      0 points yesterday

      Profile
      Rank: Genius
    11. nishantcomp2512

      89,000

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    12. MlandaT

      86,553

      Master

      0 points yesterday

      Profile
      Rank: Genius
    13. Chinmay_Patel

      80,818

      Master

      0 points yesterday

      Profile
      Rank: Genius
    14. ddayx10

      66,538

      Master

      0 points yesterday

      Profile
      Rank: Sage
    15. anarki_jimbel

      66,132

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    16. ambience

      63,764

      Master

      0 points yesterday

      Profile
      Rank: Sage
    17. ukerandi

      62,593

      Master

      1,000 points yesterday

      Profile
      Rank: Guru
    18. apeter

      60,772

      Master

      0 points yesterday

      Profile
      Rank: Sage
    19. JamesBurger

      60,305

      Master

      0 points yesterday

      Profile
      Rank: Sage
    20. sedgwick

      52,750

      Master

      1,600 points yesterday

      Profile
      Rank: Genius
    21. emoreau

      50,917

      Master

      0 points yesterday

      Profile
      Rank: Genius
    22. ged325

      50,311

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    23. anuradhay

      49,977

      2,500 points yesterday

      Profile
      Rank: Guru
    24. techChallenger1

      47,638

      0 points yesterday

      Profile
      Rank: Guru
    25. mas_oz2003

      43,540

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame