0Hawk0

VB.NET Form Overlay

Apr 5th, 2015
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.64 KB | None | 0 0
  1. Class FormOverlay
  2.  
  3.     Private overlayControl As FormOverlayControl
  4.  
  5.     Private formSize As Size
  6.  
  7.     Private boundsRect As Rectangle
  8.  
  9.     Public Property Image As Image
  10.         Get
  11.             Return overlayControl.Image
  12.         End Get
  13.         Set(value As Image)
  14.             overlayControl.Image = value
  15.         End Set
  16.     End Property
  17.  
  18.     Public ReadOnly Property ImageSize As Size
  19.         Get
  20.             Return overlayControl.PictureBox.Size
  21.         End Get
  22.     End Property
  23.  
  24.     Public Property ParentForm As Form
  25.         Get
  26.             Return overlayControl.ParentForm
  27.         End Get
  28.         Private Set(value As Form)
  29.             overlayControl.ParentForm = value
  30.         End Set
  31.     End Property
  32.  
  33.     Sub New(parentForm As Form)
  34.  
  35.         overlayControl = New FormOverlayControl(parentForm)
  36.  
  37.         Me.ParentForm = parentForm
  38.  
  39.     End Sub
  40.  
  41.     Sub New(parentForm As Form, image As Image)
  42.  
  43.         Me.New(parentForm)
  44.  
  45.         overlayControl.Image = image
  46.  
  47.     End Sub
  48.  
  49.     Public Sub Disable()
  50.         If ParentForm.Contains(overlayControl) Then ParentForm.Controls.Remove(overlayControl)
  51.     End Sub
  52.  
  53.     Public Sub Enable()
  54.         If Not ParentForm.Contains(overlayControl) Then
  55.             ParentForm.Controls.Add(overlayControl)
  56.             overlayControl.BringToFront()
  57.         End If
  58.     End Sub
  59.  
  60.     Private Class FormOverlayControl
  61.         Inherits Control
  62.  
  63.         Public PictureBox As PictureBox
  64.  
  65.         Private clientImage As Image
  66.  
  67.         Private boundsRect As Rectangle
  68.  
  69.         Public Property Image As Image
  70.             Get
  71.                 Return PictureBox.Image
  72.             End Get
  73.             Set(value As Image)
  74.                 PictureBox.Image = value
  75.                 PictureBox.Size = PictureBox.Image.Size
  76.                 PictureBox.Location = New Point((Width / 2) - (PictureBox.Width / 2), (Height / 2) - (PictureBox.Height / 2))
  77.             End Set
  78.         End Property
  79.  
  80.         Public Property ParentForm As Form
  81.  
  82.         Sub New(parentForm As Form)
  83.  
  84.             PictureBox = New PictureBox()
  85.             PictureBox.BackColor = Color.Transparent
  86.  
  87.             SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
  88.  
  89.             Me.DoubleBuffered = True
  90.             Me.Size = parentForm.ClientSize
  91.  
  92.             Me.ParentForm = parentForm
  93.  
  94.             clientImage = GetClientImage()
  95.  
  96.         End Sub
  97.  
  98.         Protected Overrides Sub OnCreateControl()
  99.             If Not Controls.Contains(PictureBox) Then Controls.Add(PictureBox)
  100.             MyBase.OnCreateControl()
  101.         End Sub
  102.  
  103.         Protected Overrides Sub OnPaint(e As PaintEventArgs)
  104.  
  105.             e.Graphics.DrawImage(clientImage, 0, 0)
  106.  
  107.             Using overlayBrush As New SolidBrush(Color.FromArgb(180, Color.Black))
  108.                 e.Graphics.FillRectangle(overlayBrush, boundsRect)
  109.             End Using
  110.  
  111.         End Sub
  112.  
  113.         Protected Overrides Sub OnSizeChanged(e As EventArgs)
  114.             boundsRect = New Rectangle(0, 0, Width, Height)
  115.             MyBase.OnSizeChanged(e)
  116.         End Sub
  117.  
  118.         Private Function GetClientImage() As Bitmap
  119.  
  120.             Dim bmp As New Bitmap(ParentForm.Width, ParentForm.Height)
  121.             Dim clientRect As Rectangle = RectangleToScreen(ParentForm.ClientRectangle)
  122.  
  123.             Using g As Graphics = Graphics.FromImage(bmp)
  124.                 g.CopyFromScreen(ParentForm.Location.X + clientRect.X, ParentForm.Location.Y + clientRect.Y, 0, 0, ParentForm.Size, CopyPixelOperation.SourceCopy)
  125.             End Using
  126.  
  127.             Return bmp
  128.  
  129.         End Function
  130.  
  131.     End Class
  132.  
  133. End Class
Advertisement
Add Comment
Please, Sign In to add comment