Click here to Skip to main content
Licence CPOL
First Posted 10 Dec 2009
Views 11,532
Downloads 76
Bookmarked 7 times

Twinkle twinkle little star

By | 14 Dec 2009 | Article
How to make irregular shaped forms in VB.NET.

Introduction

Once upon a time, I needed to impress my boss. We had to have our annual discussion about salary. So I wrote an application showing my "very advanced programming skills".

Background

Now I have translated this fabulous application to VB.NET.

Using the code

Just run the code, and you will see all the magnificent results the code produces.

Points of interest

The basic APIs:

Structure RECT
    Public Left As Int32
    Public Top As Int32
    Public Right As Int32
    Public Bottom As Int32
End Structure
 
Public Structure POINTAPI
    Dim X As Int32
    Dim Y As Int32
End Structure
 
 
    Declare Function DeleteObject Lib "gdi32" (ByVal hObject As IntPtr) As Boolean
    Declare Function GetStockObject Lib "gdi32.dll" (ByVal nIndex As Int32) As IntPtr
    Declare Function GetWindowRgn Lib "user32" (ByVal hwnd As IntPtr,
        ByVal hRgn As IntPtr) As IntPtr
    Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As IntPtr,
        ByVal hRgn As IntPtr, ByVal bRedraw As Boolean) As Int32
 
    'for round form
    Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Int32, ByVal Y1 As Int32,
        ByVal X2 As Int32, ByVal Y2 As Int32, ByVal X3 As Int32, ByVal Y3 As Int32) As IntPtr
    'for star form
    Declare Function CreatePolygonRgn Lib "gdi32" (ByRef lpPoint As POINTAPI,
        ByVal nCount As Int32, ByVal nPolyFillMode As Int32) As IntPtr
    'for elliptic form
    Declare Function CreateEllipticRgn Lib "gdi32.dll" (ByVal X1 As Int32,
        ByVal Y1 As Int32, ByVal X2 As Int32, ByVal Y2 As Int32) As IntPtr
 
    Declare Function CreateRectRgn Lib "gdi32.dll" (ByVal X1 As Int32, ByVal Y1 As Int32,
        ByVal X2 As Int32, ByVal Y2 As Int32) As IntPtr
    Declare Function GetWindowDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
    Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr,
        ByVal hdc As IntPtr) As Int32
 
    'for frame of form
    Declare Function FrameRgn Lib "gdi32" (ByVal hdc As IntPtr, ByVal hRgn As IntPtr,
        ByVal hBrush As IntPtr, ByVal nWidth As Int32, ByVal nHeight As Int32) As IntPtr
    Private Const BLACK_BRUSH As Int32 = 4

(Note: POINTAPI is not in the framework.)

The making of the star:

Private Sub MakePolygonForm(ByVal InForm As Form, ByVal Offset As Single)
    Const WINDING As Int32 = 2
    Const RADIEKONVERT As Double = 3.1416 / 180
    Const GRADER As Int32 = 360
    Const MAXRADER As Int32 = 200
 
    'change this to 6 for a David star, try alsso 5,7,8,9,10
    Const SPETSAR As Int32 = 7
 
    Dim x, y As Int32
    Dim xCenter, yCenter, Radie, Punkter, I As Int32
    Dim Vinkel As Double
    Dim Rotation As Int32
    Dim arPoints(MAXRADER) As POINTAPI
    Dim RECT As RECT
    Dim rgn As IntPtr
 
    RECT.Right = 300
    RECT.Left = 0
    RECT.Top = 0
    RECT.Bottom = 300
    xCenter = CInt((RECT.Right - RECT.Left) / 2)
    yCenter = CInt((RECT.Bottom - RECT.Top) / 2)
 
    Rotation = CInt(GRADER / (2 * SPETSAR))
    Punkter = 2 * SPETSAR
    Radie = yCenter
    For I = 0 To Punkter - 1
        If (I Mod 2) = 0 Then
            Radie = CInt((Radie / 2))
        Else
            Radie = yCenter
        End If
        'remove Offset to make it stay put: Vinkel = I * Rotation * RADIEKONVERT
        Vinkel = I * Rotation * RADIEKONVERT + Offset
        x = CInt(xCenter + (System.Math.Cos(Vinkel) * Radie))
        y = CInt(yCenter + (System.Math.Sin(Vinkel) * Radie))
        arPoints(I).X = x
        arPoints(I).Y = y
    Next
    rgn = CreatePolygonRgn(arPoints(0), Punkter, WINDING)
    Call SetWindowRgn(Me.Handle, rgn, True)
    If CInt(rgn) <> 0 Then DeleteObject(rgn)
End Sub

If you want another form of the form, use:

MakeRoundForm(Me)

or:

MakeEllipticForm(Me)

You can adjust the thickness of the frame in FrameRgn:

    Private Function FrameWindowRgn(ByVal hwnd As IntPtr) As IntPtr 
    Dim hRgn,hDC As IntPtr 
    hDC = GetWindowDC(hwnd)   ' Get a DC for non-client area 
    If CInt(hDC) <>   0 Then 
    hRgn = CreateRectRgn(0, 0, 0, 0) ' Create empty region 
    If CInt(hRgn) <>      0 Then 
    If CInt(GetWindowRgn(hwnd, hRgn)) <> 0 Then 
    ' to make border thicker, adjust the 2:s to something else
    FrameWindowRgn = FrameRgn(hDC,hRgn, GetStockObject(BLACK_BRUSH), 2, 2) ' Draw the frame 
    End If 
    End If 
    End If 
    If CInt(hRgn)  <>  0 Then DeleteObject(hRgn) 'release resources 
    If CInt(hDC)  <>  0 Then  ReleaseDC(hwnd, hDC) 'release resources 
    End Function

License

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

About the Author

Michael Rosqvist

Web Developer

Sweden Sweden

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberMr.PoorEnglish23:07 10 Jan '10  
GeneralIf you want to move the star PinmemberMichael Rosqvist9:17 4 Jan '10  
GeneralA more NETtish way of drawing the star [modified] PinmemberMichael Rosqvist5:45 16 Dec '09  
GeneralMy vote of 2 PinmemberPSU Steve3:25 15 Dec '09  
GeneralMy vote of 1 PinmemberRichard MacCutchan10:19 13 Dec '09  
Generalcool PinmemberChrist Kennedy19:27 10 Dec '09  
GeneralRe: cool [modified] PinmemberMichael Rosqvist19:30 10 Dec '09  
GeneralMy vote of 1 Pinmemberxliqz7:40 10 Dec '09  
GeneralRe: My vote of 1 [No you are a troll] PinmemberTheArchitectmc6:54 30 Dec '09  
GeneralAll Right, Then Pinmembersam.hill5:58 10 Dec '09  
GeneralMy vote of 1 PinmemberWilliam John Adam Trindade5:41 10 Dec '09  
GeneralRe: My vote of 1 PinmemberMichael Rosqvist6:32 10 Dec '09  
GeneralRe: My vote of 4 PinmemberNightJammer17:48 10 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Dec 2009
Article Copyright 2009 by Michael Rosqvist
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid