aspx | Bonjour,
Je suis sur un projet test qui m'affiche un Windows Form qui suit la souris. Le Form est constitué d'un PNG (type splashscreen) dont j'obtiens la transparence en retapant les fonctions natives Win32 GDI. Le truc c'est que j'aimerais afficher un texte transparent qui suit ce Form à l'aide de TextOut. J'ai essayé de spécifier en paramètre le HDC du Form courant mais cela n'affiche rien... J'arrive à afficher du texte sur le DC du screen cependant, mais il ne suit pas mon form du coup...
Pour ce qui est du code, tout se passe dans la méthode Form_Paint(object sender, PaintEventArgs e). Le HDC ici est nul, ce qui permet d'écrire sur le screen directement.
Code :
- IntPtr Hdc = API.GetDC(IntPtr.Zero);
- IntPtr memDc = API.CreateCompatibleDC(Hdc);
- IntPtr hBitmap = IntPtr.Zero;
- IntPtr oldBitmap = IntPtr.Zero;
- try
- {
- //Display-image
- Bitmap bmp = new Bitmap(backgroundImage);
- hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent
- oldBitmap = API.SelectObject(memDc, hBitmap);
-
- //Display-rectangle
- Size size = bmp.Size;
- Point pointSource = new Point(0, 0);
- Point topPos = new Point(this.Left, this.Top);
- //Set up blending options
- API.BLENDFUNCTION blend = new API.BLENDFUNCTION();
- blend.BlendOp = API.AC_SRC_OVER;
- blend.BlendFlags = 0;
- blend.SourceConstantAlpha = alpha;
- blend.AlphaFormat = API.AC_SRC_ALPHA;
- API.SetBkMode(Hdc, API.MODE_TRANSPARENT);
- API.TextOut(Hdc, this.Left, this.Top, text, text.Length);
- API.UpdateLayeredWindow(this.Handle, Hdc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, API.ULW_ALPHA);
- //Clean-up
- bmp.Dispose();
- API.ReleaseDC(IntPtr.Zero, Hdc);
-
- if (hBitmap != IntPtr.Zero)
- {
- API.SelectObject(memDc, oldBitmap);
- API.DeleteObject(hBitmap);
- }
- API.DeleteDC(memDc);
- }
- catch (Exception)
- {
- }
|
Pour la classe API qui permet d'utiliser les fonctions natives :
Code :
- internal class API
- {
- public const byte AC_SRC_OVER = 0x00;
- public const byte AC_SRC_ALPHA = 0x01;
- public const Int32 ULW_ALPHA = 0x00000002;
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- public struct BLENDFUNCTION
- {
- public byte BlendOp;
- public byte BlendFlags;
- public byte SourceConstantAlpha;
- public byte AlphaFormat;
- }
-
- [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
- 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);
- [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern IntPtr GetDC(IntPtr hWnd);
- [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
- [DllImport("user32.dll", ExactSpelling = true)]
- public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
- [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern bool DeleteDC(IntPtr hdc);
- [DllImport("gdi32.dll", ExactSpelling = true)]
- public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
- [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern bool DeleteObject(IntPtr hObject);
- [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
- public static extern int TextOut(IntPtr hDC, int x, int y, string lpString, int nCount);
- [DllImport("gdi32.dll" )]
- public static extern int SetBkMode(IntPtr hdc, int mode);
- // Background Modes
- public const int MODE_TRANSPARENT = 1;
- public const int MODE_OPAQUE = 2;
- }
|
J'utilise cette méthode là car la transparence marche plutôt bien, j'aimerais juste pouvoir rajouter du texte à côté en plus. Si quelqu'un voit où est le pépin...
Merci d'avance! |