2 Attachment(s)
[RESOLVED] Convert Form Shape To Image Shape
Hello Guys
i use this glass to convert the form shape to image shape like this .. my question is when i put controls on the form it will disappear .. how can show the controls
Before
Attachment 116035
After
Attachment 116037
The Class Which i use (rtaLayeredForm)
Code:
Imports System.Runtime.InteropServices
'-----------------------------------------------------------
'
' Ragheed Al-Tayeb .... [email protected]
'
'
'
'-----------------------------------------------------------
Public Class rtaLayeredForm
Private mParent As Form
Public ReadOnly Property Parent() As Form
Get
Return mParent
End Get
End Property
Private mMovable As Boolean = True
Public Property Movable() As String
Get
Return mMovable
End Get
Set(ByVal value As String)
mMovable = value
End Set
End Property
Public Sub SetImage(ByVal frm As Form, ByVal btm As Bitmap)
Try
mParent = frm
AddHandler frm.MouseDown, AddressOf OnMouseDown
AddHandler frm.MouseMove, AddressOf OnMouseMove
Dim exStyle As Int32 = APIs.GetWindowLong(frm.Handle.ToInt32, APIs.GWL_EXSTYLE)
APIs.SetWindowLong(frm.Handle.ToInt32, APIs.GWL_EXSTYLE, exStyle Or APIs.WS_EX_LAYERED)
If Not (Bitmap.IsCanonicalPixelFormat(btm.PixelFormat)) Or Not (Bitmap.IsAlphaPixelFormat(btm.PixelFormat)) Then
Throw New ApplicationException("The picture must be 32bit picture with alpha channel.")
End If
Dim hDC As Int32 = APIs.GetDC(0)
Dim hBtm As Int32
Dim memDC As Int32 = APIs.CreateCompatibleDC(hDC)
Dim Bits As Int32
Dim dstPos As New APIs.POINTAPI(frm.Left, frm.Top)
Dim srcPos As New APIs.POINTAPI(0, 0)
Dim Siz As New APIs.SizeAPI(btm.Width, btm.Height)
Dim Opt As New APIs.BLEND_OPTIONS
Opt.SourceConstantAlpha = 255
Opt.AlphaFormat = 1
Opt.BlendOp = 0
Opt.BlendFlags = 0
Try
hBtm = btm.GetHbitmap(Color.Black).ToInt32
Bits = APIs.SelectObject(memDC, hBtm)
APIs.UpdateLayeredWindow(frm.Handle, hDC, dstPos, Siz, memDC, srcPos, 0, Opt, 2)
Catch ex As Exception
End Try
If hBtm <> 0 Then
APIs.SelectObject(memDC, Bits)
APIs.DeleteObject(hBtm)
End If
APIs.ReleaseDC(0, hDC)
APIs.DeleteDC(memDC)
Catch ex As Exception
End Try
End Sub
Private mLastLocation As Point
Private Sub OnMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
mLastLocation = e.Location
End Sub
Private Sub OnMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (mMovable) AndAlso (e.Button = MouseButtons.Left) Then
Dim pos As Point = Parent.PointToScreen(e.Location)
pos.Offset(-mLastLocation.X, -mLastLocation.Y)
Parent.Location = pos
End If
End Sub
Private Class APIs
Public Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As Int32) As Int32
Public Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Int32) As Int32
Public Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Int32, ByVal hObject As Int32) As Int32
Public Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Int32, ByVal hdc As Int32) As Int32
Public Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Int32) As Int32
Public Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Int32) As Int32
Public Declare Function UpdateLayeredWindow Lib "user32.dll" (ByVal hWnd As Int32, ByVal hdcDst As Int32, ByRef pptDst As POINTAPI, ByRef psize As SizeAPI, ByVal hdcSrc As Int32, ByRef pptSrc As POINTAPI, ByVal crKey As Int32, ByRef pblend As BLEND_OPTIONS, ByVal dwFlags As Int32) As Int32
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongW" (ByVal hwnd As Int32, ByVal nIndex As Int32) As Int32
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongW" (ByVal hwnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32
Public Const GWL_EXSTYLE As Int32 = -20
Public Const WS_EX_LAYERED As Int32 = &H80000
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure BLEND_OPTIONS
Public BlendOp As Byte
Public BlendFlags As Byte
Public SourceConstantAlpha As Byte
Public AlphaFormat As Byte
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure SizeAPI
Public cx As Int32
Public cy As Int32
Sub New(ByVal cx As Int32, ByVal cy As Int32)
Me.cx = cx
Me.cy = cy
End Sub
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure POINTAPI
Public x As Int32
Public y As Int32
Sub New(ByVal x As Int32, ByVal y As Int32)
Me.x = x
Me.y = y
End Sub
End Structure
Public Declare Function ExtCreateRegion Lib "gdi32.dll" ( _
ByRef lpXform As XFORM, _
ByVal nCount As Int32, _
ByRef lpRgnData As RGNDATA) As Int32
<StructLayout(LayoutKind.Sequential)> _
Public Structure XFORM
Public eM11 As Single
Public eM12 As Single
Public eM21 As Single
Public eM22 As Single
Public eDx As Single
Public eDy As Single
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure RGNDATA
Public rdh As RGNDATAHEADER
Public Buffer As Byte
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure RGNDATAHEADER
Public dwSize As Int32
Public iType As Int32
Public nCount As Int32
Public nRgnSize As Int32
Public rcBound As RECT
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure
End Class
End Class
the code which i use in the form
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Dim Layer As New rtaLayeredForm()
Layer.SetImage(Me, Me.BackgroundImage)
End Sub
Thanks A Lot :)
Re: Convert Form Shape To Image Shape
Haven't looked at the API, but if the buttons are already drawn on the image then instead of adding button controls to the form you could just add the drawn button cords to a list of rectangle, then in the forms mouse_up event check if the mouse pointer is in the area of a drawn button image and do something if it is, something like...
Code:
' list for button cords.
Private drawnButtons As New List(Of Rectangle)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' add cords for an exit button: @ location 10,30, size = 75, 23
drawnButtons.Add(New Rectangle(10, 30, 75, 23))
' add cords for minimize button: @ location 10,60, size = 75, 23
drawnButtons.Add(New Rectangle(10, 60, 75, 23))
End Sub
Private Sub Form1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If drawnButtons(0).Contains(e.Location) Then ' close button
Me.Close()
ElseIf drawnButtons(1).Contains(e.Location) Then ' Minimize button
Me.WindowState = FormWindowState.Minimized
End If
End Sub
Re: Convert Form Shape To Image Shape
Thanks a lot mr.Edgemeal :)