C# Winform实现炫酷的透明动画界面

开发 后端
有些人会说为什么不直接用WPF啊,Wpf和Winform各有优缺点,适应不同的场合。Winform相对于使用更简单一些,系统要求更低。当然需要看人的习惯了和擅长的。

做过.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 基本使用方法:

protected   override  CreateParams CreateParams 
           { 
              get 
                  { 
                 CreateParams cp  =   base .CreateParams; 
                 cp.ExStyle  |=   0x00080000 ;  //  WS_EX_LAYERED 扩展样式 
                  return  cp; 
             } 
         }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

重写窗体的 CreateParams 属性

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); 
    } 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

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 ; 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.

需要呈现图像的时候调用 SetBitmap 方法。只要优化好,呈现效率比普通的Paint重绘方式高很多,并且不卡不闪烁,支持任意透明。

下面是自己开发出来的效果:

 

这个是用OpenGL绘制的

 
责任编辑:王雪燕 来源: 博客园
相关推荐

2025-03-03 00:40:29

WinFormUI框架界面

2011-06-20 16:38:33

Qt QWidget Qt4.5

2022-02-11 16:01:14

C语言技巧命令

2016-01-18 10:14:44

jQuery相册动画

2022-07-21 07:05:13

粒子动画CSS

2009-09-01 10:35:59

C# WinForm控

2009-09-07 03:58:42

WinForm传值

2024-07-03 08:21:56

MDI窗体界面

2009-08-17 15:48:47

C# WinForm进

2017-07-18 16:00:09

炫酷动画开源框架APP

2009-08-20 09:30:03

C#开发WinForm

2009-08-20 10:24:52

C#开发WinForm

2024-02-22 13:55:41

前端动画库

2009-08-20 10:10:55

C#透明窗体

2020-01-03 10:50:16

Python编程语言Mac电脑

2025-01-06 11:59:32

2023-10-08 20:32:59

CSS定义Loading

2024-05-29 05:00:00

2024-12-12 16:38:44

2009-09-07 06:48:13

C#透明窗体
点赞
收藏

51CTO技术栈公众号