Aero Efekt Problem

  • VB.NET

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von Dayshadow119.

    Aero Efekt Problem

    Guten Tag,

    ich habe bei einer Form den Aero Efekt angewendet blos hab ich nun ein Problem .
    Wenn ich in meine Richtextbox etwas hineinschreibe ist die Farbe Blau manchmal hell manchmal dunkel
    hier mal ein kleiner Screen:

    Das liegt daran das Labels so nicht auf dem Glass funktionieren. Entweder per GDI zeichnen oder dieses Control benutzen:

    VB.NET-Quellcode

    1. Public Class MegaLabel
    2. Inherits Label
    3. Dim Glass As New Glass_Full
    4. Dim Panel As New Rectangle
    5. Dim _Font As Font
    6. Dim _FontSize As Integer
    7. Dim _Text As String
    8. Private Sub DrawText()
    9. Glass.DrawTextOnGlass(Me.Parent.Handle, _Text, _Font, Panel, _FontSize)
    10. Me.Visible = False
    11. End Sub
    12. Private Sub MegaLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    13. If DesignMode = False Then
    14. Me.Visible = True
    15. _Font = Me.Font
    16. _FontSize = Me.Font.Size
    17. _Text = Me.Text
    18. Me.Visible = False
    19. Panel.Size = New Size(Me.Size.Width, Me.Size.Height)
    20. Panel.Location = New Point(Me.Location.X, Me.Location.Y)
    21. DrawText()
    22. End If
    23. End Sub
    24. End Class


    Quelle: dreamincode.net/forums/topic/1…glass-form-dwmapidll-api/
    @Image: Bin ganz deiner Meinung, man kann nicht alles wissen aber wer es nicht schafft sich ein Text durchzulesen sollte es lieber lassen. Trotzdem habe ich heute einen guten Tag und möchte dem TE helfen.

    Du erstellst eine neue Klasse (Class) und kopierst das hier rein:

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Collections.Generic
    3. Imports System.Text
    4. Imports System.Drawing
    5. Imports System.Windows.Forms
    6. Imports System.Runtime.InteropServices
    7. Imports System.Diagnostics
    8. Friend Class Glass_Full
    9. Private Const DTT_COMPOSITED As Integer = CInt((1 << 13))
    10. Private Const DTT_GLOWSIZE As Integer = CInt((1 << 11))
    11. 'Text format consts
    12. Private Const DT_SINGLELINE As Integer = &H20
    13. Private Const DT_CENTER As Integer = &H1
    14. Private Const DT_VCENTER As Integer = &H4
    15. Private Const DT_NOPREFIX As Integer = &H800
    16. 'Const for BitBlt
    17. Private Const SRCCOPY As Integer = &HCC0020
    18. 'Consts for CreateDIBSection
    19. Private Const BI_RGB As Integer = 0
    20. Private Const DIB_RGB_COLORS As Integer = 0
    21. 'color table in RGBs
    22. Private Structure MARGINS
    23. Public m_Left As Integer
    24. Public m_Right As Integer
    25. Public m_Top As Integer
    26. Public m_Buttom As Integer
    27. End Structure
    28. Private Structure POINTAPI
    29. Public x As Integer
    30. Public y As Integer
    31. End Structure
    32. Private Structure DTTOPTS
    33. Public dwSize As UInteger
    34. Public dwFlags As UInteger
    35. Public crText As UInteger
    36. Public crBorder As UInteger
    37. Public crShadow As UInteger
    38. Public iTextShadowType As Integer
    39. Public ptShadowOffset As POINTAPI
    40. Public iBorderSize As Integer
    41. Public iFontPropId As Integer
    42. Public iColorPropId As Integer
    43. Public iStateId As Integer
    44. Public fApplyOverlay As Integer
    45. Public iGlowSize As Integer
    46. Public pfnDrawTextCallback As IntPtr
    47. Public lParam As Integer
    48. End Structure
    49. Private Structure RECT
    50. Public left As Integer
    51. Public top As Integer
    52. Public right As Integer
    53. Public bottom As Integer
    54. End Structure
    55. Private Structure BITMAPINFOHEADER
    56. Public biSize As Integer
    57. Public biWidth As Integer
    58. Public biHeight As Integer
    59. Public biPlanes As Short
    60. Public biBitCount As Short
    61. Public biCompression As Integer
    62. Public biSizeImage As Integer
    63. Public biXPelsPerMeter As Integer
    64. Public biYPelsPerMeter As Integer
    65. Public biClrUsed As Integer
    66. Public biClrImportant As Integer
    67. End Structure
    68. Private Structure RGBQUAD
    69. Public rgbBlue As Byte
    70. Public rgbGreen As Byte
    71. Public rgbRed As Byte
    72. Public rgbReserved As Byte
    73. End Structure
    74. Private Structure BITMAPINFO
    75. Public bmiHeader As BITMAPINFOHEADER
    76. Public bmiColors As RGBQUAD
    77. End Structure
    78. 'API declares
    79. <DllImport("dwmapi.dll")> _
    80. Private Shared Sub DwmIsCompositionEnabled(ByRef enabledptr As Integer)
    81. End Sub
    82. <DllImport("dwmapi.dll")> _
    83. Private Shared Sub DwmExtendFrameIntoClientArea(ByVal hWnd As IntPtr, ByRef margin As MARGINS)
    84. End Sub
    85. Private Declare Auto Function GetDC Lib "user32.dll" (ByVal hdc As IntPtr) As IntPtr
    86. Private Declare Auto Function SaveDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Integer
    87. Private Declare Auto Function ReleaseDC Lib "user32.dll" (ByVal hdc As IntPtr, ByVal state As Integer) As Integer
    88. Private Declare Auto Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hDC As IntPtr) As IntPtr
    89. <DllImport("gdi32.dll", ExactSpelling:=True)> _
    90. Private Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
    91. End Function
    92. Private Declare Auto Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As Boolean
    93. Private Declare Auto Function DeleteDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Boolean
    94. <DllImport("gdi32.dll")> _
    95. Private Shared Function BitBlt(ByVal hdc As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, _
    96. ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As UInteger) As Boolean
    97. End Function
    98. Private Declare Unicode Function DrawThemeTextEx Lib "UxTheme.dll" (ByVal hTheme As IntPtr, ByVal hdc As IntPtr, ByVal iPartId As Integer, ByVal iStateId As Integer, ByVal text As String, ByVal iCharCount As Integer, _
    99. ByVal dwFlags As Integer, ByRef pRect As RECT, ByRef pOptions As DTTOPTS) As Integer
    100. Private Declare Auto Function DrawThemeText Lib "UxTheme.dll" (ByVal hTheme As IntPtr, ByVal hdc As IntPtr, ByVal iPartId As Integer, ByVal iStateId As Integer, ByVal text As String, ByVal iCharCount As Integer, _
    101. ByVal dwFlags1 As Integer, ByVal dwFlags2 As Integer, ByRef pRect As RECT) As Integer
    102. Private Declare Auto Function CreateDIBSection Lib "gdi32.dll" (ByVal hdc As IntPtr, ByRef pbmi As BITMAPINFO, ByVal iUsage As UInteger, ByVal ppvBits As Integer, ByVal hSection As IntPtr, ByVal dwOffset As UInteger) As IntPtr
    103. Public Sub SetGlass(ByVal Form As Form)
    104. Dim en As Integer = 0
    105. Dim mg As New MARGINS()
    106. mg.m_Buttom = -1
    107. mg.m_Left = -1
    108. mg.m_Right = -1
    109. mg.m_Top = -1
    110. 'make sure you are not on a legacy OS
    111. If System.Environment.OSVersion.Version.Major >= 6 Then
    112. DwmIsCompositionEnabled(en)
    113. 'check if the desktop composition is enabled
    114. If en > 0 Then
    115. DwmExtendFrameIntoClientArea(Form.Handle, mg)
    116. Else
    117. Throw New ApplicationException("This computer does not have the areo interface enabled.")
    118. End If
    119. Else
    120. Throw New ApplicationException("This computer does not have the areo theme capibility.")
    121. End If
    122. End Sub
    123. Private Function IsCompositionEnabled() As Boolean
    124. If Environment.OSVersion.Version.Major < 6 Then
    125. Return False
    126. End If
    127. Dim compositionEnabled As Integer = 0
    128. DwmIsCompositionEnabled(compositionEnabled)
    129. If compositionEnabled > 0 Then
    130. Return True
    131. Else
    132. Return False
    133. End If
    134. End Function
    135. Public Sub FillBlackRegion(ByVal gph As Graphics, ByVal rgn As Rectangle)
    136. Dim rc As New RECT()
    137. rc.left = rgn.Left
    138. rc.right = rgn.Right
    139. rc.top = rgn.Top
    140. rc.bottom = rgn.Bottom
    141. Dim destdc As IntPtr = gph.GetHdc()
    142. 'hwnd must be the handle of form,not control
    143. Dim Memdc As IntPtr = CreateCompatibleDC(destdc)
    144. Dim bitmap As IntPtr
    145. Dim bitmapOld As IntPtr = IntPtr.Zero
    146. Dim dib As New BITMAPINFO()
    147. dib.bmiHeader.biHeight = -(rc.bottom - rc.top)
    148. dib.bmiHeader.biWidth = rc.right - rc.left
    149. dib.bmiHeader.biPlanes = 1
    150. dib.bmiHeader.biSize = Marshal.SizeOf(GetType(BITMAPINFOHEADER))
    151. dib.bmiHeader.biBitCount = 32
    152. dib.bmiHeader.biCompression = BI_RGB
    153. If Not (SaveDC(Memdc) = 0) Then
    154. bitmap = CreateDIBSection(Memdc, dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0)
    155. If Not (bitmap = IntPtr.Zero) Then
    156. bitmapOld = SelectObject(Memdc, bitmap)
    157. BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, _
    158. 0, 0, SRCCOPY)
    159. End If
    160. 'Remember to clean up
    161. SelectObject(Memdc, bitmapOld)
    162. DeleteObject(bitmap)
    163. ReleaseDC(Memdc, -1)
    164. DeleteDC(Memdc)
    165. End If
    166. gph.ReleaseHdc()
    167. End Sub
    168. Public Sub DrawTextOnGlass(ByVal hwnd As IntPtr, ByVal text As [String], ByVal font As Font, ByVal ctlrct As Rectangle, ByVal iglowSize As Integer)
    169. If IsCompositionEnabled() Then
    170. Dim rc As New RECT()
    171. Dim rc2 As New RECT()
    172. rc.left = ctlrct.Left
    173. rc.right = ctlrct.Right + 2 * iglowSize
    174. 'make it larger to contain the glow effect
    175. rc.top = ctlrct.Top
    176. rc.bottom = ctlrct.Bottom + 2 * iglowSize
    177. 'Just the same rect with rc,but (0,0) at the lefttop
    178. rc2.left = 0
    179. rc2.top = 0
    180. rc2.right = rc.right - rc.left
    181. rc2.bottom = rc.bottom - rc.top
    182. Dim destdc As IntPtr = GetDC(hwnd)
    183. 'hwnd must be the handle of form,not control
    184. Dim Memdc As IntPtr = CreateCompatibleDC(destdc)
    185. ' Set up a memory DC where we'll draw the text.
    186. Dim bitmap As IntPtr
    187. Dim bitmapOld As IntPtr = IntPtr.Zero
    188. Dim logfnotOld As IntPtr
    189. Dim uFormat As Integer = DT_SINGLELINE Or DT_CENTER Or DT_VCENTER Or DT_NOPREFIX
    190. 'text format
    191. Dim dib As New BITMAPINFO()
    192. dib.bmiHeader.biHeight = -(rc.bottom - rc.top)
    193. ' negative because DrawThemeTextEx() uses a top-down DIB
    194. dib.bmiHeader.biWidth = rc.right - rc.left
    195. dib.bmiHeader.biPlanes = 1
    196. dib.bmiHeader.biSize = Marshal.SizeOf(GetType(BITMAPINFOHEADER))
    197. dib.bmiHeader.biBitCount = 32
    198. dib.bmiHeader.biCompression = BI_RGB
    199. If Not (SaveDC(Memdc) = 0) Then
    200. bitmap = CreateDIBSection(Memdc, dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0)
    201. ' Create a 32-bit bmp for use in offscreen drawing when glass is on
    202. If Not (bitmap = IntPtr.Zero) Then
    203. bitmapOld = SelectObject(Memdc, bitmap)
    204. Dim hFont As IntPtr = font.ToHfont()
    205. logfnotOld = SelectObject(Memdc, hFont)
    206. Try
    207. Dim renderer As New System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.window.Caption.Active)
    208. Dim dttOpts As New DTTOPTS()
    209. dttOpts.dwSize = CUInt(Marshal.SizeOf(GetType(DTTOPTS)))
    210. dttOpts.dwFlags = DTT_COMPOSITED Or DTT_GLOWSIZE
    211. dttOpts.iGlowSize = iglowSize
    212. DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, text, -1, _
    213. uFormat, rc2, dttOpts)
    214. BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, _
    215. 0, 0, SRCCOPY)
    216. Catch e As Exception
    217. Trace.WriteLine(e.Message)
    218. End Try
    219. 'Remember to clean up
    220. SelectObject(Memdc, bitmapOld)
    221. SelectObject(Memdc, logfnotOld)
    222. DeleteObject(bitmap)
    223. DeleteObject(hFont)
    224. ReleaseDC(Memdc, -1)
    225. DeleteDC(Memdc)
    226. End If
    227. End If
    228. End If
    229. End Sub
    230. End Class


    Dann erstellst du eine zweite Klasse und fügst den von mir oben genannten Code rein.
    Danach erstellst du dein Projekt neu (Rechtsklick auf das Projekt und 'Neu erstellen') nun hast du vermutlich ein Control in der Toolbox was "MegaLabel" etc. heißt und das ziehst du dir auf die Form.