C# Winform實現炫酷的透明動畫介面

DSkin的部落格發表於2015-07-09

做過.NET Winform窗體美化的人應該都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以實現窗體的任意透明,效果很好,不會有毛邊。不過使用這個API之後,會有一個問題就是無法使用普通控制元件,而且沒有Paint訊息。為了解決這個問題,有兩種方法。

一、使用雙層窗體,底層窗體使用UpdateLayeredWindow作為背景,上層窗體用普通窗體,並且可以使用TransparencyKey或者Region來實現去除不需要的窗體內容,讓上層窗體能看到底層的窗體。

二、直接單層窗體,使用控制元件的DrawToBitmap把控制元件影像繪製到UpdateLayeredWindow的窗體上,這樣就可以看到普通控制元件了。不過這個也有問題:1.控制元件內容不能自動更新  2.效率低,很多控制元件使用DrawToBitmap繪製出的影像不完整,甚至繪製不出影像。比如TextBox無法顯示游標,WebBrowser無法顯示內容。

三、採用DirectUI技術,重寫所有基礎控制元件。效果最好,不過工作量巨大。

使用UpdateLayeredWindow時,一般是需要對Bitmap快取起來,通過設定剪輯區域,區域性重繪來提高效率。另外還可以非同步重繪,模擬Winform的失效到重繪。

有些人會說為什麼不直接用WPF啊,Wpf和Winform各有優缺點,適應不同的場合。Winform相對於使用更簡單一些,系統要求更低。當然需要看人的習慣了和擅長的。

UpdateLayeredWindow 基本使用方法:

重寫窗體的 CreateParams 屬性

protected   override  CreateParams CreateParams
           {
              get
                  {
                 CreateParams cp  =   base .CreateParams;
                 cp.ExStyle  |=   0x00080000 ;  //  WS_EX_LAYERED 擴充套件樣式
                  return  cp;
             }
         }

API呼叫:

public   void  SetBitmap(Bitmap bitmap,  byte  opacity)
    {
     if  (bitmap.PixelFormat  !=  PixelFormat.Format32bppArgb)
         throw   new  ApplicationException( "點陣圖必須是32位包含alpha 通道" );

    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 ));   // 建立GDI點陣圖控制程式碼,效率較低
        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.ULW_ALPHA);
    }
     finally 
         {
        Win32.ReleaseDC(IntPtr.Zero, screenDc);
         if  (hBitmap  !=  IntPtr.Zero)
              {
            Win32.SelectObject(memDc, oldBitmap);

            Win32.DeleteObject(hBitmap);
        }
        Win32.DeleteDC(memDc);
    }
}

API宣告:

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
         {
         public  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 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( " 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 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( " 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 ;
}

需要呈現影像的時候呼叫 SetBitmap 方法。只要優化好,呈現效率比普通的Paint重繪方式高很多,並且不卡不閃爍,支援任意透明。

下面是自己開發出來的效果:

這個是用OpenGL繪製的

推薦一款C#介面庫:DSkin介面庫(Winform平臺首個DirectUI介面庫)  http://d.cskin.net

還有一個也是我開發的免費介面庫LayeredSkin http://bbs.cskin.net/forum-56-1.html 也可以實現很多效果
Winform也可以很炫麗的!

相關文章