Wednesday, January 12, 2011

Tricks for custom form design in VB.NET

In this tutorial we are going to learn some simple tricks for changing a simple form to cool design in VB.NET (i am using 2005) off-course beginners level.

 

so which one is looking cool to you?I hope that's the right one.
lets see how to achieve this,first choose an image(better to select *.png or *.gif with transparent background) which you want to use as your form template,you can do this by selecting BackgorundImage in form properties or you can use me.BackgroundImage and remember to add it as a resource file not a local file.once your image is imported you have to set it's layout by  
BackgroundImageLayout=None or  me.BackgroundImageLayout=ImageLayout.None 
and change the form size to cover the image completely,something like
 
we are allmost done just have to hide the original form which holds our image,this is a two step process
1.) Hide the form border.
2.) Make transparent the form background.

first step is easy just go to form properties and set 
FormBorderStyle=None or me.FormBorderStyle=Windows.Forms.FormBorderStyle.None 
however when you make your form border-less you can't move it,So we have to add some extra code here to avoid that problem.Copy this code anywhere in your form coding body.
#Region " ClientAreaMove Handling "
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If m.Result = HTCLIENT Then m.Result = HTCAPTION
                'If m.Result.ToInt32 = HTCLIENT Then m.Result = IntPtr.op_Explicit(HTCAPTION) 'Try this in VS.NET 2002/2003 if the latter line of code doesn't do it... thx to Suhas for the tip.
            Case Else
                'Make sure you pass unhandled messages back to the default message handler.
                MyBase.WndProc(m)
        End Select
    End Sub
#End Region
now your form can move even without a border (don't ask me how this code works cause i don't completely  understand either,and all credit goes to the original coder)

Second step is pretty easy you just have to set form backcolor to transparent however you can't set 
BackColor=Transperant   or Me.BackColor = Color.Transparent 
because this is not supported by VB.NET.
so again need to do a little trick here,go to form properties and set BackColor to anycolor which is rarely used and ensure you are not  going to use it any where else.(In this case i am using hot pink,because that would be the most ugly color for me :P ) so again form properties
BackColor=HotPink or Me.BackColor = Color.HotPink
now we have to set the TransparencyKey exaclty same to color we choose (in this case HotPink)
TransparencyKey=HotPink or Me.TransparencyKey = Color.HotPink

that's it! your form is now boderless and transparent and after some coding this will look like......
isn't it cool? now go make one for yourself  :)

10 comments:

  1. Thank you very much!

    The code to move is fantastic and your designs are excellent.

    ReplyDelete
  2. It works for me..tnx u. It's a big help.

    ReplyDelete
  3. Thank you very much!
    But I need a code that works on a picture for moving a form.

    ReplyDelete
    Replies
    1. i didn't get it?could you be more specific plz.

      Delete
  4. Replies
    1. The screen shot i mentioned in the post are in fact taken on windows xp.

      Delete
  5. worked on 2010 tnx

    ReplyDelete

Like this post? let me know just leave a comment..