Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class FormOverlay
- Private overlayControl As FormOverlayControl
- Private formSize As Size
- Private boundsRect As Rectangle
- Public Property Image As Image
- Get
- Return overlayControl.Image
- End Get
- Set(value As Image)
- overlayControl.Image = value
- End Set
- End Property
- Public ReadOnly Property ImageSize As Size
- Get
- Return overlayControl.PictureBox.Size
- End Get
- End Property
- Public Property ParentForm As Form
- Get
- Return overlayControl.ParentForm
- End Get
- Private Set(value As Form)
- overlayControl.ParentForm = value
- End Set
- End Property
- Sub New(parentForm As Form)
- overlayControl = New FormOverlayControl(parentForm)
- Me.ParentForm = parentForm
- End Sub
- Sub New(parentForm As Form, image As Image)
- Me.New(parentForm)
- overlayControl.Image = image
- End Sub
- Public Sub Disable()
- If ParentForm.Contains(overlayControl) Then ParentForm.Controls.Remove(overlayControl)
- End Sub
- Public Sub Enable()
- If Not ParentForm.Contains(overlayControl) Then
- ParentForm.Controls.Add(overlayControl)
- overlayControl.BringToFront()
- End If
- End Sub
- Private Class FormOverlayControl
- Inherits Control
- Public PictureBox As PictureBox
- Private clientImage As Image
- Private boundsRect As Rectangle
- Public Property Image As Image
- Get
- Return PictureBox.Image
- End Get
- Set(value As Image)
- PictureBox.Image = value
- PictureBox.Size = PictureBox.Image.Size
- PictureBox.Location = New Point((Width / 2) - (PictureBox.Width / 2), (Height / 2) - (PictureBox.Height / 2))
- End Set
- End Property
- Public Property ParentForm As Form
- Sub New(parentForm As Form)
- PictureBox = New PictureBox()
- PictureBox.BackColor = Color.Transparent
- SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
- Me.DoubleBuffered = True
- Me.Size = parentForm.ClientSize
- Me.ParentForm = parentForm
- clientImage = GetClientImage()
- End Sub
- Protected Overrides Sub OnCreateControl()
- If Not Controls.Contains(PictureBox) Then Controls.Add(PictureBox)
- MyBase.OnCreateControl()
- End Sub
- Protected Overrides Sub OnPaint(e As PaintEventArgs)
- e.Graphics.DrawImage(clientImage, 0, 0)
- Using overlayBrush As New SolidBrush(Color.FromArgb(180, Color.Black))
- e.Graphics.FillRectangle(overlayBrush, boundsRect)
- End Using
- End Sub
- Protected Overrides Sub OnSizeChanged(e As EventArgs)
- boundsRect = New Rectangle(0, 0, Width, Height)
- MyBase.OnSizeChanged(e)
- End Sub
- Private Function GetClientImage() As Bitmap
- Dim bmp As New Bitmap(ParentForm.Width, ParentForm.Height)
- Dim clientRect As Rectangle = RectangleToScreen(ParentForm.ClientRectangle)
- Using g As Graphics = Graphics.FromImage(bmp)
- g.CopyFromScreen(ParentForm.Location.X + clientRect.X, ParentForm.Location.Y + clientRect.Y, 0, 0, ParentForm.Size, CopyPixelOperation.SourceCopy)
- End Using
- Return bmp
- End Function
- End Class
- End Class
Advertisement
Add Comment
Please, Sign In to add comment