Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. ' The state of our little button
  2. Private _buttState As ButtonState = ButtonState.Normal
  3. Private _buttPosition As New Rectangle()
  4.  
  5. <DllImport("user32.dll")> _
  6. Private Shared Function GetWindowDC(hWnd As IntPtr) As IntPtr
  7. End Function
  8. <DllImport("user32.dll")> _
  9. Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As Rectangle) As Integer
  10. End Function
  11. <DllImport("user32.dll")> _
  12. Private Shared Function ReleaseDC(hWnd As IntPtr, hDC As IntPtr) As Integer
  13. End Function
  14. Protected Overrides Sub WndProc(ByRef m As Message)
  15. Dim x As Integer, y As Integer
  16. Dim windowRect As New Rectangle()
  17. GetWindowRect(m.HWnd, windowRect)
  18.  
  19. Select Case m.Msg
  20. ' WM_NCPAINT
  21. ' WM_PAINT
  22. Case &H85, &Ha
  23. MyBase.WndProc(m)
  24.  
  25. DrawButton(m.HWnd)
  26.  
  27. m.Result = IntPtr.Zero
  28.  
  29. Exit Select
  30.  
  31. ' WM_ACTIVATE
  32. Case &H86
  33. MyBase.WndProc(m)
  34. DrawButton(m.HWnd)
  35.  
  36. Exit Select
  37.  
  38. ' WM_NCMOUSEMOVE
  39. Case &Ha0
  40. ' Extract the least significant 16 bits
  41. x = (CInt(m.LParam) << 16) >> 16
  42. ' Extract the most significant 16 bits
  43. y = CInt(m.LParam) >> 16
  44.  
  45. x -= windowRect.Left
  46. y -= windowRect.Top
  47.  
  48. MyBase.WndProc(m)
  49.  
  50. If Not _buttPosition.Contains(New Point(x, y)) AndAlso _buttState = ButtonState.Pushed Then
  51. _buttState = ButtonState.Normal
  52. DrawButton(m.HWnd)
  53. End If
  54.  
  55. Exit Select
  56.  
  57. ' WM_NCLBUTTONDOWN
  58. Case &Ha1
  59. ' Extract the least significant 16 bits
  60. x = (CInt(m.LParam) << 16) >> 16
  61. ' Extract the most significant 16 bits
  62. y = CInt(m.LParam) >> 16
  63.  
  64. x -= windowRect.Left
  65. y -= windowRect.Top
  66.  
  67. If _buttPosition.Contains(New Point(x, y)) Then
  68. _buttState = ButtonState.Pushed
  69. DrawButton(m.HWnd)
  70. Else
  71. MyBase.WndProc(m)
  72. End If
  73.  
  74. Exit Select
  75.  
  76. ' WM_NCLBUTTONUP
  77. Case &Ha2
  78. ' Extract the least significant 16 bits
  79. x = (CInt(m.LParam) << 16) >> 16
  80. ' Extract the most significant 16 bits
  81. y = CInt(m.LParam) >> 16
  82.  
  83. x -= windowRect.Left
  84. y -= windowRect.Top
  85.  
  86. If _buttPosition.Contains(New Point(x, y)) AndAlso _buttState = ButtonState.Pushed Then
  87. _buttState = ButtonState.Normal
  88. ' [[TODO]]: Fire a click event for your button
  89. ' however you want to do it.
  90. DrawButton(m.HWnd)
  91. Else
  92. MyBase.WndProc(m)
  93. End If
  94.  
  95. Exit Select
  96.  
  97. ' WM_NCHITTEST
  98. Case &H84
  99. ' Extract the least significant 16 bits
  100. x = (CInt(m.LParam) << 16) >> 16
  101. ' Extract the most significant 16 bits
  102. y = CInt(m.LParam) >> 16
  103.  
  104. x -= windowRect.Left
  105. y -= windowRect.Top
  106.  
  107. If _buttPosition.Contains(New Point(x, y)) Then
  108. m.Result = DirectCast(18, IntPtr)
  109. Else
  110. ' HTBORDER
  111. MyBase.WndProc(m)
  112. End If
  113.  
  114. Exit Select
  115. Case Else
  116.  
  117. MyBase.WndProc(m)
  118. Exit Select
  119. End Select
  120. End Sub
  121.  
  122. Private Sub DrawButton(hwnd As IntPtr)
  123. Dim hDC As IntPtr = GetWindowDC(hwnd)
  124. Dim x As Integer, y As Integer
  125.  
  126. Using g As Graphics = Graphics.FromHdc(hDC)
  127. ' Work out size and positioning
  128. Dim CaptionHeight As Integer = Bounds.Height - ClientRectangle.Height
  129. Dim ButtonSize As Size = SystemInformation.CaptionButtonSize
  130. x = Bounds.Width - 4 * ButtonSize.Width
  131. y = (CaptionHeight - ButtonSize.Height) 2
  132. _buttPosition.Location = New Point(x, y)
  133.  
  134. ' Work out color
  135. Dim color As Brush
  136. If _buttState = ButtonState.Pushed Then
  137. color = Brushes.LightGreen
  138. Else
  139. color = Brushes.Red
  140. End If
  141.  
  142. ' Draw our "button"
  143. g.FillRectangle(color, x, y, ButtonSize.Width, ButtonSize.Height)
  144. End Using
  145.  
  146. ReleaseDC(hwnd, hDC)
  147. End Sub
  148.  
  149. Private Sub Form1_Load(sender As Object, e As EventArgs)
  150. _buttPosition.Size = SystemInformation.CaptionButtonSize
  151. End Sub
Add Comment
Please, Sign In to add comment