Transparente Form: Label sehr unschöne Darstellung

  • VB.NET
  • .NET (FX) 4.0

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von BradApfel.

    Transparente Form: Label sehr unschöne Darstellung

    Abend,

    ist kein sonderlich großes Problem, allerdings stört mich die Darstellung eines Labels auf einer transparenten Form ein wenig. Damit die Form Transparent ist, habe ich den ​TransparencyKey auf HotPink gestellt, ebenso die Hintergrundfarbe. Das Problem an der Sache ist, dass das Label bzw. der Text eine Umrandung in der festgelegten Farbe hat. Das sieht so aus:

    Gibt es da einen besonderen Trick ? Den "Effekt" welchen ich erreichen möchte sehe ich sehr oft in irgendwelchen "Kiddy"-Programmen.. bekomme ihn aber nicht hin ?(

    Grüße

    BradApfel schrieb:

    Den "Effekt" welchen ich erreichen möchte
    kennen wir noch nicht. :/
    Kannst Du davon ein Bild posten?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Schreib den Text doch mit GDI+
    Dann fällt der Rand wech.


    MSDN
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    @Schamash
    Dürfte nicht funktionieren - der TransparencyKey funktioniert ja nicht nach dem Anteil der Farbe sondern danach, ob ein Pixel genau diese Farbe hat.

    @BradApfel
    Ich kenne nur die folgende Methode über Gdi32 (Link):
    Spoiler anzeigen

    C#-Quellcode

    1. //
    2. // Copyright © 2002-2004 Rui Godinho Lopes <rui@ruilopes.com>
    3. // All rights reserved.
    4. //
    5. // This source file(s) may be redistributed unmodified by any means
    6. // PROVIDING they are not sold for profit without the authors expressed
    7. // written consent, and providing that this notice and the authors name
    8. // and all copyright notices remain intact.
    9. //
    10. // Any use of the software in source or binary forms, with or without
    11. // modification, must include, in the user documentation ("About" box and
    12. // printed documentation) and internal comments to the code, notices to
    13. // the end user as follows:
    14. //
    15. // "Portions Copyright © 2002-2004 Rui Godinho Lopes"
    16. //
    17. // An email letting me know that you are using it would be nice as well.
    18. // That's not much to ask considering the amount of work that went into
    19. // this.
    20. //
    21. // THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    22. // EXPRESS OR IMPLIED. USE IT AT YOUT OWN RISK. THE AUTHOR ACCEPTS NO
    23. // LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
    24. //
    25. using System;
    26. using System.Drawing;
    27. using System.Drawing.Imaging;
    28. using System.Windows.Forms;
    29. using System.Runtime.InteropServices;
    30. // class that exposes needed win32 gdi functions.
    31. class Win32
    32. {
    33. public enum Bool
    34. {
    35. False= 0,
    36. True
    37. };
    38. [StructLayout(LayoutKind.Sequential)]
    39. public struct Point
    40. {
    41. public Int32 x;
    42. public Int32 y;
    43. public Point(Int32 x, Int32 y) { this.x= x; this.y= y; }
    44. }
    45. [StructLayout(LayoutKind.Sequential)]
    46. public struct Size {
    47. public Int32 cx;
    48. public Int32 cy;
    49. public Size(Int32 cx, Int32 cy) { this.cx= cx; this.cy= cy; }
    50. }
    51. [StructLayout(LayoutKind.Sequential, Pack=1)]
    52. struct ARGB
    53. {
    54. public byte Blue;
    55. public byte Green;
    56. public byte Red;
    57. public byte Alpha;
    58. }
    59. [StructLayout(LayoutKind.Sequential, Pack=1)]
    60. public struct BLENDFUNCTION
    61. {
    62. public byte BlendOp;
    63. public byte BlendFlags;
    64. public byte SourceConstantAlpha;
    65. public byte AlphaFormat;
    66. }
    67. public const Int32 ULW_COLORKEY = 0x00000001;
    68. public const Int32 ULW_ALPHA = 0x00000002;
    69. public const Int32 ULW_OPAQUE = 0x00000004;
    70. public const byte AC_SRC_OVER = 0x00;
    71. public const byte AC_SRC_ALPHA = 0x01;
    72. [DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
    73. public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
    74. [DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
    75. public static extern IntPtr GetDC(IntPtr hWnd);
    76. [DllImport("user32.dll", ExactSpelling=true)]
    77. public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    78. [DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
    79. public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
    80. [DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
    81. public static extern Bool DeleteDC(IntPtr hdc);
    82. [DllImport("gdi32.dll", ExactSpelling=true)]
    83. public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
    84. [DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
    85. public static extern Bool DeleteObject(IntPtr hObject);
    86. }
    87. /// <para>Your PerPixel form should inherit this class</para>
    88. /// <author><name>Rui Godinho Lopes</name><email>rui@ruilopes.com</email></author>
    89. class PerPixelAlphaForm : Form
    90. {
    91. public PerPixelAlphaForm()
    92. {
    93. // This form should not have a border or else Windows will clip it.
    94. FormBorderStyle = FormBorderStyle.None;
    95. }
    96. /// <para>Changes the current bitmap.</para>
    97. public void SetBitmap(Bitmap bitmap)
    98. {
    99. SetBitmap(bitmap, 255);
    100. }
    101. /// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para>
    102. public void SetBitmap(Bitmap bitmap, byte opacity)
    103. {
    104. if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
    105. throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
    106. // The ideia of this is very simple,
    107. // 1. Create a compatible DC with screen;
    108. // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
    109. // 3. Call the UpdateLayeredWindow.
    110. IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
    111. IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
    112. IntPtr hBitmap = IntPtr.Zero;
    113. IntPtr oldBitmap = IntPtr.Zero;
    114. try {
    115. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
    116. oldBitmap = Win32.SelectObject(memDc, hBitmap);
    117. Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
    118. Win32.Point pointSource = new Win32.Point(0, 0);
    119. Win32.Point topPos = new Win32.Point(Left, Top);
    120. Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
    121. blend.BlendOp = Win32.AC_SRC_OVER;
    122. blend.BlendFlags = 0;
    123. blend.SourceConstantAlpha = opacity;
    124. blend.AlphaFormat = Win32.AC_SRC_ALPHA;
    125. Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
    126. }
    127. finally {
    128. Win32.ReleaseDC(IntPtr.Zero, screenDc);
    129. if (hBitmap != IntPtr.Zero) {
    130. Win32.SelectObject(memDc, oldBitmap);
    131. //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
    132. Win32.DeleteObject(hBitmap);
    133. }
    134. Win32.DeleteDC(memDc);
    135. }
    136. }
    137. protected override CreateParams CreateParams
    138. {
    139. get {
    140. CreateParams cp = base.CreateParams;
    141. cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
    142. return cp;
    143. }
    144. }
    145. }
    @nafets
    Um das mit Gewissheit sagen zuz können müssten wir wissen wie der "Effekt" genau aussieht.
    So sähe das mit GDI+ aus

    @BradApfel hab nochmal eins mit Schwarzer Schrift hinterlegt.

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    2. Dim g As Graphics = PictureBox1.CreateGraphics
    3. Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, Color.Black, Color.White, Drawing2D.LinearGradientMode.Horizontal)
    4. Dim myFont As New Font("Times New Roman", 10)
    5. g.DrawString("Test Test", myFont, mybrush, New RectangleF(10, 10, 100, 200))
    6. End Sub



    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Schamash“ ()

    @RodFromGermany
    Ein Bild habe ich jetzt nicht parat. Der Text (Label) auf dem Foto ist normalerweise schwarz, leider hat er diese Umrandung und kann nicht auf einer transparenter Form platziert werden.

    @Schamash
    Schaue ich mir auf jeden Fall mal an.

    @nafets
    Sieht interessant aus, allerdings trifft das leider nicht zu.
    -

    Habe es mal mit verschiedenen Schriftarten probiert, ohne Erfolg. Was mir jedoch aufgefallen ist, dass 1 Pixel am Rand des Textes die Hintergrundfarbe der Form annimmt. Sprich es wird warum auch immer ein Rand um den Text gezogen. Eine andere Farbe ist auch nicht die Lösung, Pink ist nur ein Beispiel.

    Hintergrundfarbe im Designer geändert:

    ("TEST TEST..." ist das ausgeführte Programm, wo das Label den Rand hat.)

    @BradApfel Hier war vor kurzem ein Thread mit einem ähnlichen Problem.
    Da sollte der farbige Rand weg und die Schrift rein einfarbig sein, such mal danach.
    Das geht mit einer simplen Property.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!