Jimi2000

OverlayForm

Jan 27th, 2018
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. Transparent Form
  2. All Borders/Control Boxes to None
  3. .StartPosition = Manual
  4. .TransparncyKey = WhiteSmoke
  5. .BackColor = WhiteSmoke
  6. .ShowInTaskbar = False
  7.  
  8. Public Class TForm
  9.  
  10.     Private _Text As String
  11.     Private TextPosition As Point
  12.     Private _Brush As SolidBrush = New SolidBrush(Color.White)
  13.     Private _Flags As StringFormatFlags = StringFormatFlags.NoWrap
  14.  
  15.     Public Enum Alignment
  16.         Left = 0
  17.         Right = 1
  18.         Center = 2
  19.     End Enum
  20.  
  21.     Public Sub New()
  22.         InitializeComponent()
  23.     End Sub
  24.     Public Overrides Property Text() As String
  25.         Get
  26.             Return Me._Text
  27.         End Get
  28.         Set(ByVal value As String)
  29.             _Text = value
  30.             Me.Invalidate()
  31.         End Set
  32.     End Property
  33.  
  34.     Public Property OverlayPosition As Alignment
  35.  
  36.     Private Sub TForm_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
  37.  
  38.         e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
  39.         e.Graphics.TextContrast = 12
  40.         Dim _Size As SizeF = e.Graphics.MeasureString(Me._Text, Me.Font,
  41.                                                       New SizeF(Me.Width, Me.Height),
  42.                                                       New StringFormat(Me._Flags))
  43.  
  44.         e.Graphics.DrawString(Me._Text, Me.Font, Me._Brush, New RectangleF(TextAlign(_Size.Width), _Size))
  45.  
  46.     End Sub
  47.  
  48.     Private Sub TForm_ForeColorChanged(sender As Object, e As EventArgs) Handles Me.ForeColorChanged
  49.         Me._Brush = New SolidBrush(Me.ForeColor)
  50.         Me.Invalidate()
  51.     End Sub
  52.  
  53.     Public Sub Reposition(ParentPosition As Point, ParentSize As Size)
  54.         Select OverlayPosition
  55.             Case Alignment.Left
  56.                 Me.Location = New Point(ParentPosition.X + 20, ParentPosition.Y + 40)
  57.             Case Alignment.Right
  58.                 Me.Location = New Point(ParentSize.Width - Me.Width - 20, ParentPosition.Y + 40)
  59.             Case Alignment.Center
  60.                  Me.Location = New Point(ParentPosition.X + 20 + (ParentSize.Width \ 2) - (Me.Width \ 2), ParentPosition.Y + 40)
  61.         End Select
  62.     End Sub
  63.  
  64.     Private Function TextAlign(TextWidth As Single) As PointF
  65.         Select Case OverlayPosition
  66.             Case Alignment.Left
  67.                 Return New PointF(1, 1)
  68.             Case Alignment.Right
  69.                 Return New PointF((Me.Width - TextWidth) - 1, 1)
  70.             Case Alignment.Center
  71.                 If TextWidth > Me.Width Then TextWidth = Me.Width - 2
  72.                 Return New PointF(CSng((Me.Width - TextWidth) / 4) - 1, 1)
  73.         End Select
  74.     End Function
  75.  
  76. End Class
  77. ----------------------------------------------------------------------
  78. Changes to the Hosting Form:
  79.  
  80. Public Class Form1
  81.  
  82.     Private Overlay As New TForm
  83.  
  84.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
  85.         Me.MinimumSize = New Size(200, 100)
  86.         Overlay.Size = Me.MinimumSize
  87.         Overlay.OverlayPosition = TForm.Alignment.Center
  88.         Overlay.Reposition(Me.Location, Me.Size)
  89.         //Set the Text Property to show the Overlay Title
  90.         Overlay.Text = "Picture Title"
  91.         Overlay.Show(Me)
  92.         Overlay.Visible = False
  93.     End Sub
  94.  
  95.     Private Sub Form1_Move(sender As Object, e As EventArgs) Handles Me.Move
  96.         Overlay.Reposition(Me.Location, Me.Size)
  97.     End Sub
  98.  
  99.     Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
  100.         Overlay.Reposition(Me.Location, Me.Size)
  101.     End Sub
  102.  
  103.     Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
  104.         ShowOverlay(300)
  105.     End Sub
  106.  
  107.     Private Async Sub ShowOverlay(Delay As Integer)
  108.         Await Task.Delay(Delay)
  109.         Overlay.Visible = True
  110.         Me.Focus()
  111.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment