TheProgrammingZone
Login:
Name:
Pass:
 
Home
 
 TheProgrammingZone's front page. All the latest programming and game development news, as well as this week's feature game project!
Articles
 
Visual Basic
VB.Net
C/C++
C#
Java
PHP
Other
Assembly aritcles
Source Code
 
Visual Basic
VB.Net
C/C++
C#
Java
PHP
Assembly
Game Projects
 
 Game projects under development by members of the TheProgrammingZone Community. Show them your support, and check out their games!
Message Board
 A forum to interact with your fellow programmers.
Links
 
Visual Basic
VB.Net
C/C++
C#
Java
PHP
ASP
JavaScript
Assembly Links
Other
Products
 
TheProgrammingZone's products. Check out some of our in house products that we made.
Contact
 
You want to contact the guy behind TheProgrammingZone? Here's where to do it!

 
 


Changing a form's shape with SetWindowRgn - page 1


pages: prev | 1 | next
There are occasions when the standard square shape of a window is too restrictive for our purposes. Fortunately, with a few API calls (and a lot of care) we can make windows any shape we want.

The two API calls which we need to accomplish this are SetWindowRgn and CreatePolygonRgn. The declarations for these are:

Public Type POINTAPI
X As Long
Y As Long
End Type
Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

We also need to know the existing dimension of the window we are going to change so we need:

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long


Note that lpPoint must not be passed using ByVal, as what you are passing to the API is a pointer to the structure from VB.
In order to change the shape of the window, you need to create a shaped region (defined as an array of x and y co-ordinates held in a POINTAPI structure) and then force the window to use this region.

Example: Creating a Christmas Tree shaped window

'\\ --[MakeTreeShaped]------------------------------------
'\\ Makes the window tree shapes and forces it to repaint
'\\ itself
'\\ ------------------------------------------------------
Public Sub MakeTreeShaped(ByVal mWnd As Form)

Dim XMasTree() As POINTAPI
Dim wndWidth As Long
Dim wndHeight As Long
Dim hRegion As Long
Dim mWndRect As RECT
Dim lRet As Long

If mWnd.hWnd > 0 Then
lRet = GetWindowRect(mwnd.hWnd, mWndRect)
ReDim XMasTree(0 To 11) As POINTAPI
wndWidth = mWndRect.Right - mWndRect.Left
wndHeight = mWndRect.Bottom - mWndRect.Top
'\\ Start at top...
XMasTree(0).X = wndWidth / 2
XMasTree(0).Y = 0
XMasTree(1).X = 0
XMasTree(1).Y = wndHeight / 3
XMasTree(2).X = wndWidth / 3
XMasTree(2).Y = wndHeight / 3
XMasTree(3).X = 0
XMasTree(3).Y = 2 * (wndHeight / 3)
XMasTree(4).X = wndWidth / 3
XMasTree(4).Y = 2 * (wndHeight / 3)
XMasTree(5).X = wndWidth / 3
XMasTree(5).Y = wndHeight
XMasTree(6).X = 2 * (wndWidth / 3)
XMasTree(6).Y = wndHeight
XMasTree(7).X = 2 * (wndWidth / 3)
XMasTree(7).Y = 2 * (wndHeight / 3)
XMasTree(8).X = wndWidth
XMasTree(8).Y = 2 * (wndHeight / 3)
XMasTree(9).X = 2 * (wndWidth / 3)
XMasTree(9).Y = wndHeight / 3
XMasTree(10).X = wndWidth
XMasTree(10).Y = wndHeight / 3
XMasTree(11).X = wndWidth / 2
XMasTree(11).Y = 0

'\\ Create this polygon region
mRegion = CreatePolygonRgn(XMasTree(0), 12, 1)
If mRegion > 0 Then
lret = SetWindowRgn(mWnd.hWnd, mRegion, True)
If lret = 0 Then
Debug.Print "SetWindowRgn failed"
End If
Else
Debug.Print "Failed to make region!"
End If
End If

mWnd.Refresh

End Sub

This assumes a loaded visual basic form called mWnd.
If the API calls fail they won't throw a visual basic error, which is why you must check that mRegion is greater than zero (a valid region) before calling SetWindowRgn. However, if the API calls fail, the visual basic intrinsic object Err will have its LastDllError member value set.


pages: prev | 1 | next
TheProgrammingZone 2024
Site Map