日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#Winform實現(xiàn)炫酷的透明動畫界面

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

成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設、網(wǎng)站設計、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務蒲城,10多年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108

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

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

 三、采用DirectUI技術(shù),重寫所有基礎控件。效果最好,不過工作量巨大。

使用UpdateLayeredWindow時,一般是需要對Bitmap緩存起來,通過設置剪輯區(qū)域,局部重繪來提高效率。另外還可以異步重繪,模擬Winform的失效到重繪。

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

UpdateLayeredWindow 基本使用方法:

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

重寫窗體的 CreateParams 屬性

API調(diào)用:

 
 
  1. public   void  SetBitmap(Bitmap bitmap,  byte  opacity)
  2.     {
  3.      if  (bitmap.PixelFormat  !=  PixelFormat.Format32bppArgb)
  4.          throw   new  ApplicationException( "位圖必須是32位包含alpha 通道" );
  5.  
  6.     IntPtr screenDc  =  Win32.GetDC(IntPtr.Zero);
  7.     IntPtr memDc  =  Win32.CreateCompatibleDC(screenDc);
  8.     IntPtr hBitmap  =  IntPtr.Zero;
  9.     IntPtr oldBitmap  =  IntPtr.Zero;
  10.  
  11.      try 
  12.          {
  13.         hBitmap  =  bitmap.GetHbitmap(Color.FromArgb( 0 ));   // 創(chuàng)建GDI位圖句柄,效率較低
  14.         oldBitmap  =  Win32.SelectObject(memDc, hBitmap);
  15.  
  16.         Win32.Size size  =   new  Win32.Size(bitmap.Width, bitmap.Height);
  17.         Win32.Point pointSource  =   new  Win32.Point( 0 ,  0 );
  18.         Win32.Point topPos  =   new  Win32.Point(Left, Top);
  19.         Win32.BLENDFUNCTION blend  =   new  Win32.BLENDFUNCTION();
  20.         blend.BlendOp              =  Win32.AC_SRC_OVER;
  21.         blend.BlendFlags           =   0 ;
  22.         blend.SourceConstantAlpha  =  opacity;
  23.         blend.AlphaFormat          =  Win32.AC_SRC_ALPHA;
  24.  
  25.         Win32.UpdateLayeredWindow(Handle, screenDc,  ref  topPos,  ref  size, memDc,  ref  pointSource,  0 ,  ref  blend, Win32.ULW_ALPHA);
  26.     }
  27.      finally 
  28.          {
  29.         Win32.ReleaseDC(IntPtr.Zero, screenDc);
  30.          if  (hBitmap  !=  IntPtr.Zero)
  31.               {
  32.             Win32.SelectObject(memDc, oldBitmap);
  33.              
  34.             Win32.DeleteObject(hBitmap);
  35.         }
  36.         Win32.DeleteDC(memDc);
  37.     }

API聲明:

 
 
  1. class  Win32
  2.     {
  3.      public   enum  Bool
  4.         {
  5.         False  =   0 ,
  6.         True
  7.     } ;
  8.  
  9.  
  10.     [StructLayout(LayoutKind.Sequential)]
  11.      public   struct  Point
  12.          {
  13.          public  Int32 x;
  14.          public  Int32 y;
  15.  
  16.           public  Point(Int32 x, Int32 y) 
  17.           {  this .x  =  x;  this .y  =  y; }
  18.     }
  19.  
  20.  
  21.     [StructLayout(LayoutKind.Sequential)]
  22.      public   struct  Size
  23.          {
  24.          public  Int32 cx;
  25.          public  Int32 cy;
  26.  
  27.           public  Size(Int32 cx, Int32 cy) 
  28.             {  this .cx  =  cx;  this .cy  =  cy; }
  29.     }
  30.  
  31.  
  32.     [StructLayout(LayoutKind.Sequential, Pack  =   1 )]
  33.      struct  ARGB
  34.         {
  35.          public   byte  Blue;
  36.          public   byte  Green;
  37.          public   byte  Red;
  38.          public   byte  Alpha;
  39.     }
  40.  
  41.  
  42.     [StructLayout(LayoutKind.Sequential, Pack  =   1 )]
  43.      public   struct  BLENDFUNCTION
  44.          {
  45.          public   byte  BlendOp;
  46.          public   byte  BlendFlags;
  47.          public   byte  SourceConstantAlpha;
  48.          public   byte  AlphaFormat;
  49.     }
  50.  
  51.  
  52.      public   const  Int32 ULW_COLORKEY  =   0x00000001 ;
  53.      public   const  Int32 ULW_ALPHA  =   0x00000002 ;
  54.      public   const  Int32 ULW_OPAQUE  =   0x00000004 ;
  55.  
  56.      public   const   byte  AC_SRC_OVER  =   0x00 ;
  57.      public   const   byte  AC_SRC_ALPHA  =   0x01 ;
  58.  
  59.  
  60.     [DllImport( " user32.dll " , ExactSpelling  =   true , SetLastError  =   true )]
  61.      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);
  62.  
  63.     [DllImport( " user32.dll " , ExactSpelling  =   true , SetLastError  =   true )]
  64.      public   static   extern  IntPtr GetDC(IntPtr hWnd);
  65.  
  66.     [DllImport( " user32.dll " , ExactSpelling  =   true )]
  67.      public   static   extern   int  ReleaseDC(IntPtr hWnd, IntPtr hDC);
  68.  
  69.     [DllImport( " gdi32.dll " , ExactSpelling  =   true , SetLastError  =   true )]
  70.      public   static   extern  IntPtr CreateCompatibleDC(IntPtr hDC);
  71.  
  72.     [DllImport( " gdi32.dll " , ExactSpelling  =   true , SetLastError  =   true )]
  73.      public   static   extern  Bool DeleteDC(IntPtr hdc);
  74.  
  75.     [DllImport( " gdi32.dll " , ExactSpelling  =   true )]
  76.      public   static   extern  IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  77.  
  78.     [DllImport( " gdi32.dll " , ExactSpelling  =   true , SetLastError  =   true )]
  79.      public   static   extern  Bool DeleteObject(IntPtr hObject);
  80.  
  81.     [DllImport( " user32.dll " , EntryPoint  =   " SendMessage " )]
  82.      public   static   extern   int  SendMessage( int  hWnd,  int  wMsg,  int  wParam,  int  lParam);
  83.     [DllImport( " user32.dll " , EntryPoint  =   " ReleaseCapture " )]
  84.  
  85.      public   static   extern   int  ReleaseCapture();
  86.      public   const   int  WM_SysCommand  =   0x0112 ;
  87.      public   const   int  SC_MOVE  =   0xF012 ;
  88.  
  89.      public   const   int  SC_MAXIMIZE  =   61488 ;
  90.      public   const   int  SC_MINIMIZE  =   61472 ;

需要呈現(xiàn)圖像的時候調(diào)用 SetBitmap 方法。只要優(yōu)化好,呈現(xiàn)效率比普通的Paint重繪方式高很多,并且不卡不閃爍,支持任意透明。

下面是自己開發(fā)出來的效果:

這個是用OpenGL繪制的


文章題目:C#Winform實現(xiàn)炫酷的透明動畫界面
文章鏈接:http://www.5511xx.com/article/cdpopgj.html