Guest User

Untitled

a guest
Sep 22nd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.93 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class RegionForm
  4.     Inherits SingleLayeredForm
  5.     Private StartX, StartY, EndX, EndY As Integer, IsDrawing As Boolean = False
  6.     Private FillBrush As New SolidBrush(Color.FromArgb(200, Color.Orange))
  7.  
  8.     Sub New()
  9.         Location = GetStartPoint()
  10.         Size = GetSize()
  11.  
  12.         TopMost = True
  13.         _ClickThrought = False
  14.     End Sub
  15.  
  16.     Private Function GetStartPoint() As Point
  17.         For Each Screen As Screen In Screen.AllScreens
  18.             GetStartPoint.X = If(Screen.Bounds.X < GetStartPoint.X, Screen.Bounds.X, GetStartPoint.X)
  19.             GetStartPoint.Y = If(Screen.Bounds.Y < GetStartPoint.Y, Screen.Bounds.Y, GetStartPoint.Y)
  20.         Next
  21.     End Function
  22.  
  23.     Private Function GetSize() As Size
  24.         For Each Screen As Screen In Screen.AllScreens
  25.             GetSize.Height += If(Screen.Bounds.Height > GetSize.Height, Screen.Bounds.Height, GetSize.Height)
  26.             GetSize.Width += Screen.Bounds.Width
  27.         Next
  28.     End Function
  29.  
  30.  
  31.     Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
  32.         StartX = e.X : StartY = e.Y
  33.         IsDrawing = True
  34.     End Sub
  35.     Protected Overrides Sub OnMouseUp(e As System.Windows.Forms.MouseEventArgs)
  36.         IsDrawing = False
  37.     End Sub
  38.  
  39.     Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
  40.         If Not IsDrawing Then Exit Sub
  41.  
  42.         EndX = e.X : EndY = e.Y
  43.         Invalidate()
  44.     End Sub
  45.  
  46.     Private Fnt As New Font("Verdana", 20)
  47.     Private Sub RegionForm_Paint(G As System.Drawing.Graphics) Handles Me.Paint
  48.         Dim Watch = Stopwatch.StartNew
  49.  
  50.         Dim region = New Rectangle(Math.Min(StartX, EndX), Math.Min(StartY, EndY), Math.Abs(StartX - EndX), Math.Abs(StartY - EndY))
  51.         G.FillRectangle(FillBrush, region)
  52.  
  53.         ' Draw the FPS
  54.         Dim str As String = Math.Round(1000 / Watch.ElapsedMilliseconds, 0) & " FPS"
  55.         Dim sF = G.MeasureString(str, Fnt)
  56.         G.FillRectangle(Brushes.White, 0, 0, sF.Width, sF.Height)
  57.         G.DrawString(str, Fnt, Brushes.Black, New Point)
  58.     End Sub
  59. End Class
  60.  
  61. Public Class SingleLayeredForm
  62.     Inherits Form
  63.     Protected _ClickThrought As Boolean
  64.  
  65. #Region " API Stuff "
  66.     Private Const AC_SRC_OVER As Byte = 0
  67.     Private Const AC_SRC_ALPHA As Byte = 1
  68.     Private Const ULW_ALPHA As Int32 = 2
  69.  
  70.     Private Declare Auto Function CreateCompatibleDC Lib "gdi32.dll" (hDC As IntPtr) As IntPtr
  71.     Private Declare Auto Function GetDC Lib "user32.dll" (hWnd As IntPtr) As IntPtr
  72.  
  73.     <DllImport("gdi32.dll", ExactSpelling:=True)> _
  74.     Private Shared Function SelectObject(hDC As IntPtr, hObj As IntPtr) As IntPtr
  75.     End Function
  76.  
  77.     <DllImport("user32.dll", ExactSpelling:=True)> _
  78.     Private Shared Function ReleaseDC(hWnd As IntPtr, hDC As IntPtr) As Integer
  79.     End Function
  80.  
  81.     Private Declare Auto Function DeleteDC Lib "gdi32.dll" (hDC As IntPtr) As Integer
  82.     Private Declare Auto Function DeleteObject Lib "gdi32.dll" (hObj As IntPtr) As Integer
  83.     Private Declare Auto Function UpdateLayeredWindow Lib "user32.dll" (hwnd As IntPtr, hdcDst As IntPtr, ByRef pptDst As Point, ByRef psize As Size, hdcSrc As IntPtr, ByRef pptSrc As Point, crKey As Int32, ByRef pblend As BLENDFUNCTION, dwFlags As Int32) As Integer
  84.     Private Declare Auto Function ExtCreateRegion Lib "gdi32.dll" (lpXform As IntPtr, nCount As UInteger, rgnData As IntPtr) As IntPtr
  85.  
  86.     <StructLayout(LayoutKind.Sequential)> _
  87.     Private Structure Size
  88.         Public cx As Int32
  89.         Public cy As Int32
  90.  
  91.         Public Sub New(x As Int32, y As Int32)
  92.             cx = x
  93.             cy = y
  94.         End Sub
  95.     End Structure
  96.  
  97.     <StructLayout(LayoutKind.Sequential, Pack:=1)> _
  98.     Private Structure BLENDFUNCTION
  99.         Public BlendOp As Byte
  100.         Public BlendFlags As Byte
  101.         Public SourceConstantAlpha As Byte
  102.         Public AlphaFormat As Byte
  103.     End Structure
  104.  
  105.     <StructLayout(LayoutKind.Sequential)> _
  106.     Private Structure Point
  107.         Public x As Int32
  108.         Public y As Int32
  109.  
  110.         Public Sub New(x As Int32, y As Int32)
  111.             Me.x = x
  112.             Me.y = y
  113.         End Sub
  114.  
  115.         Public Shared Operator +(x As Point, y As Point) As Point
  116.             Return New Point(x.x + y.x, x.y + y.y)
  117.         End Operator
  118.  
  119.     End Structure
  120. #End Region
  121.  
  122.     Sub New(Optional ByVal ClickThrought As Boolean = True)
  123.         SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
  124.         FormBorderStyle = Windows.Forms.FormBorderStyle.None
  125.         StartPosition = FormStartPosition.Manual
  126.         _ClickThrought = ClickThrought
  127.     End Sub
  128.  
  129.     'WS_EX_LAYERED, WS_EX_TRANSPARENT
  130.     Protected Overrides ReadOnly Property CreateParams() As CreateParams
  131.         Get
  132.             Dim cParms As CreateParams = MyBase.CreateParams
  133.             cParms.ExStyle = cParms.ExStyle Or &H80000
  134.             If _ClickThrought Then cParms.ExStyle = cParms.ExStyle Or &H20
  135.  
  136.             Return cParms
  137.         End Get
  138.     End Property
  139.  
  140.     Public Sub SetBits(B As Drawing.Bitmap, Optional ByVal p As Drawing.Point = Nothing)
  141.         If Not IsHandleCreated Or DesignMode Then Exit Sub
  142.  
  143.         Dim oldBits As IntPtr = IntPtr.Zero
  144.         Dim screenDC As IntPtr = GetDC(IntPtr.Zero)
  145.         Dim hBitmap As IntPtr = IntPtr.Zero
  146.         Dim memDc As IntPtr = CreateCompatibleDC(screenDC)
  147.  
  148.         Try
  149.             Dim topLoc As New Point(Left, Top)
  150.             If Not IsNothing(p) Then topLoc += New Point(p.X, p.Y)
  151.  
  152.             Dim bitMapSize As New Size(B.Width, B.Height)
  153.             Dim blendFunc As New BLENDFUNCTION()
  154.             Dim srcLoc As New Point(0, 0)
  155.  
  156.             hBitmap = B.GetHbitmap(Drawing.Color.FromArgb(0))
  157.             oldBits = SelectObject(memDc, hBitmap)
  158.  
  159.             blendFunc.BlendOp = AC_SRC_OVER
  160.             blendFunc.SourceConstantAlpha = 255
  161.             blendFunc.AlphaFormat = AC_SRC_ALPHA
  162.             blendFunc.BlendFlags = 0
  163.  
  164.             UpdateLayeredWindow(Handle, screenDC, topLoc, bitMapSize, memDc, srcLoc, _
  165.              0, blendFunc, ULW_ALPHA)
  166.         Finally
  167.             If hBitmap <> IntPtr.Zero Then
  168.                 SelectObject(memDc, oldBits)
  169.                 DeleteObject(hBitmap)
  170.             End If
  171.             ReleaseDC(IntPtr.Zero, screenDC)
  172.             DeleteDC(memDc)
  173.         End Try
  174.     End Sub
  175.  
  176.     Public Shadows Sub Invalidate()
  177.         Dim B As New Drawing.Bitmap(ClientSize.Width, ClientSize.Height)
  178.         Dim G As Drawing.Graphics = Drawing.Graphics.FromImage(B) : G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
  179.  
  180.         RaiseEvent Paint(G)
  181.  
  182.         G.Dispose()
  183.         SetBits(B)
  184.         B.Dispose()
  185.     End Sub
  186.  
  187.     Public Shadows Event Paint(G As Drawing.Graphics)
  188. End Class
Add Comment
Please, Sign In to add comment