最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫(huà)界面

 更新時(shí)間:2015年07月28日 16:45:08   投稿:lijiao  
這篇文章講解了如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫(huà)界面,Winform相對(duì)于Wpf使用更簡(jiǎn)單一些,系統(tǒng)要求更低,需要了解的朋友可以參考下

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

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

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

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

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

有些人會(huì)說(shuō)為什么不直接用WPF啊,Wpf和Winform各有優(yōu)缺點(diǎn),適應(yīng)不同的場(chǎng)合。Winform相對(duì)于使用更簡(jiǎn)單一些,系統(tǒng)要求更低。當(dāng)然需要看人的習(xí)慣了和擅長(zhǎng)的。

UpdateLayeredWindow 基本使用方法:

protected  override CreateParams CreateParams 
      { 
       get 
         { 
         CreateParams cp =  base .CreateParams; 
         cp.ExStyle |=  0x00080000 ; // WS_EX_LAYERED 擴(kuò)展樣式 
         return cp; 
       } 
     } 

重寫(xiě)窗體的 CreateParams 屬性

API調(diào)用:

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 ));  // 創(chuàng)建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 ; 
} 

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

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

這個(gè)是用OpenGL繪制的

效果是不是很酷呀,通過(guò)以上內(nèi)容的介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

奇台县| 嘉兴市| 建湖县| 宣化县| 云林县| 灯塔市| 囊谦县| 正定县| 扎兰屯市| 五峰| 靖州| 水城县| 德令哈市| 九寨沟县| 皮山县| 大关县| 彭水| 惠东县| 大埔区| 翼城县| 海安县| 松潘县| 泽库县| 阿勒泰市| 松阳县| 保靖县| 长阳| 策勒县| 开阳县| 高雄县| 富民县| 合作市| 正定县| 大方县| 株洲县| 扶沟县| 马鞍山市| 司法| 大方县| 沽源县| 玉溪市|