C # WinForm for a cool, transparent animation interface

Source: Internet
Author: User

People who have done. NET WinForm forms should be familiar with Updatelayeredwindow,Updatelayeredwindow can achieve any form of transparency, the effect is very good, there will be no raw edges. After using this API, however, there is a problem with the inability to use normal controls and no paint messages. There are two ways to solve this problem.

First, using a two-tier form, the underlying form uses Updatelayeredwindow as the background, the upper form with a normal form, and you can use TransparencyKey or region to implement the removal of unwanted form content, so that the upper form can see the underlying form.

second, direct single-layer form, using the control's drawtobitmap to draw the control image to Updatelayeredwindow on the form so that you can see the normal controls. But there's a problem with this: 1. Control content cannot be updated automatically 2. Inefficient, many controls use DrawToBitmap to draw an incomplete image, or even to draw an image. For example, the textbox cannot display the cursor, WebBrowser cannot display the content.

Third, the use of Directui technology, rewrite all the underlying controls. It works best, but it's a lot of work.

When using Updatelayeredwindow, it is generally necessary to cache the bitmap and increase the efficiency by setting the clipping region and local redrawing. It is also possible to redraw asynchronously, simulating the invalidation of WinForm to redrawing.

Some people would say why not use WPF directly, WPF and WinForm each have advantages and disadvantages, adapt to different occasions. The WinForm is simpler to use and lower in system requirements. Of course, we need to look at people's habits and good at it.

Updatelayeredwindow basic methods of use:

Overriding the CreateParams property of a form

Protected   override  CreateParams createparams           {              get                   {                 createparams cp  =   base. CreateParams;                 Cp. ExStyle  |=   0x00080000;  ws_ex_layered extended Style                   return  CP;             }          

  

API calls:

          public void SetBitmap (Bitmap Bitmap, byte opacity) {if (Bitmap.              PixelFormat! = Pixelformat.format32bppargb) throw new ApplicationException ("Bitmap must be 32 bits containing alpha channel");             IntPtr SCREENDC = WIN32.GETDC (IntPtr.Zero);             IntPtr MEMDC = Win32.createcompatibledc (SCREENDC);             IntPtr hbitmap = IntPtr.Zero;               IntPtr oldbitmap = IntPtr.Zero; try {hbitmap = bitmap.   Gethbitmap (Color.FromArgb (0));                  Creating GDI bitmap handles, less efficient Oldbitmap = Win32.selectobject (MEMDC, HBITMAP); Win32.size Size = new Win32.size (bitmap. Width, Bitmap.                 Height);                 Win32.point Pointsource = new Win32.point (0, 0);                 Win32.point Toppos = new Win32.point (left, Top);                 Win32.blendfunction blend = new Win32.blendfunction (); Blend. Blendop = Win32.ac_src_over;                Blend.                 blendflags = 0; Blend.                 Sourceconstantalpha = opacity; Blend.                  Alphaformat = Win32.ac_src_alpha; Win32.updatelayeredwindow (Handle, SCREENDC, ref toppos, ref size, MEMDC, ref pointsource, 0, ref blend, WIN32.UL             W_alpha);                  } finally {Win32.releasedc (IntPtr.Zero, SCREENDC);                                          if (hbitmap! = IntPtr.Zero) {win32.selectobject (MEMDC, Oldbitmap);                 Win32.deleteobject (HBITMAP);             } win32.deletedc (MEMDC);  }          }

  

API declaration:

      Class Win32 {public enum Bool {False = 0, True           } ;              [StructLayout (layoutkind.sequential)] public struct Point {public Int32 x;                Public Int32 y;  Public point (Int32 x, Int32 y) {this. x = x; this. y = y; }} [StructLayout (layoutkind.sequential)] public struct Size {p              Ublic Int32 CX;                Public Int32 Cy;  Public Size (Int32 CX, Int32 cy) {this. cx = CX; this. cy = cy;              }} [StructLayout (layoutkind.sequential, Pack = 1)] struct ARGB {              public byte Blue;              public byte Green;              public byte Red;         public byte Alpha; } [StructLayout (layoutkind.sequential, Pack = 1)] public struct  blendfunction {public byte Blendop;              public byte Blendflags;              public byte Sourceconstantalpha;         public byte Alphaformat;          } public Const Int32 Ulw_colorkey = 0x00000001;          Public Const Int32 Ulw_alpha = 0x00000002;           Public Const Int32 Ulw_opaque = 0x00000004;          Public Const byte Ac_src_over = 0x00;           Public Const byte Ac_src_alpha = 0x01; [DllImport ("user32.dll", ExactSpelling = True, SetLastError = true)] public static extern Bool U Pdatelayeredwindow (IntPtr hwnd, INTPTR HDCDST, ref point PPTDST, ref Size psize, IntPtr hdcsrc, ref point PPRSRC, in         T32 Crkey, ref blendfunction Pblend, Int32 dwFlags);  [DllImport ("user32.dll", ExactSpelling = True, SetLastError = true)] public static extern IntPtr          GetDC (IntPtr hWnd); [DllImport ("User32.dll", ExactSpelling = true)] public static extern int ReleaseDC (IntPtr hWnd, IntPtr HDC); [DllImport ("Gdi32.dll", ExactSpelling = True, SetLastError = true)] public static extern IntPtr          CreateCompatibleDC (IntPtr HDC); [DllImport ("Gdi32.dll", ExactSpelling = True, SetLastError = true)] public static extern Bool De          LETEDC (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 De          Leteobject (IntPtr hobject);  [DllImport ("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage (int         hWnd, int wmsg, int wParam, int lParam); [DllImport ("user32.dll", EntryPoint = "releasecapture")] public  static extern int releasecapture ();          public const int wm_syscommand = 0x0112;           public const int sc_move = 0xf012;          public const int sc_maximize = 61488;     public const int sc_minimize = 61472;  }

  

The SetBitmap method is called when the image needs to be rendered. As long as the optimization, rendering efficiency is much higher than the normal paint redraw method, and the card does not blink, support arbitrary transparency.

Here are the results of your own development:

This one was drawn with OpenGL.

Recommended a C # Interface library: Dskin Interface Library (WinForm platform First Directui interface library) http://d.cskin.net

WinForm can also be very flashy!

C # WinForm for a cool, transparent animation interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.