C # implement winform custom translucent Mask Layer

Source: Internet
Author: User

It is not difficult to achieve the translucent effect through Div + CSS on the webpage. Today we will look at a method implemented in winfrom:

As follows:

When the mask layer is displayed:

The source code of the custom Mask Layer Control is as follows:

View code

 Using  System;  Using  System. drawing;  Using  System. Windows. forms;  Using  System. componentmodel;  Namespace  Myopaquelayer {  ///  <Summary>      ///  Custom Control: Translucent Control  ///   </Summary>      /*  * [Toolboxbitmap (typeof (myopaquelayer)] * used to specify the icons displayed on the toolbar when a custom control is added to the toolbar. * The correct syntax is * [toolboxbitmap (typeof (xxxxcontrol), "xxx.bmp")] * Where xxxxcontrol is your custom control and "xxx.bmp" is the name of the icon you want to use.  */  [Toolboxbitmap (  Typeof  (Myopaquelayer)]  Public   Class Myopaquelayer: system. Windows. Forms. Control {  Private   Bool _ Transparentbg = True ; //  Transparent or not          Private   Int _ Alpha = 125 ; //  Set transparency                  Private System. componentmodel. Container components = New System. componentmodel. Container ();  Public  Myopaquelayer ():  This ( 125 , True  ){}  Public Myopaquelayer ( Int Alpha, Bool  Isshowloadingimage) {setstyle (system. Windows. Forms. controlstyles. opaque,  True  ); Base  . Createcontrol ();  This . _ Alpha = Alpha;  If  (Isshowloadingimage) {picturebox picturebox_loading = New  Picturebox (); picturebox_loading.backcolor = System. Drawing. color. White; picturebox_loading.image = Loading. properties. Resources. loading; picturebox_loading.name = " Picturebox_loading  "  ; Picturebox_loading.size = New System. Drawing. Size ( 48 , 48  ); Picturebox_loading.sizemode = System. Windows. Forms. pictureboxsizemode. autosize; Point Location = New Point ( This . Location. x + ( This . Width-picturebox_loading.width )/2 , This . Location. Y + ( This . Height-picturebox_loading.height )/ 2 ); //  Center Picturebox_loading.location = Location; picturebox_loading.anchor = Anchorstyles. None;  This  . Controls. Add (picturebox_loading );}}  Protected   Override  Void Dispose ( Bool  Disposing ){  If  (Disposing ){  If (! (Components = Null  ) {Components. Dispose ();}}  Base  . Dispose (disposing );}  ///   <Summary>          /// Custom drawing form  ///   </Summary>          ///   <Param name = "E"> </param>          Protected   Override   Void  Onpaint (system. Windows. Forms. painteventargs e ){  Float  Vlblcontrolwidth;  Float  Vlblcontrolheight; pen labelborderpen; solidbrush labelbackcolorbrush; If  (_ Transparentbg) {color drawcolor = Color. fromargb ( This . _ Alpha, This  . Backcolor); labelborderpen = New Pen (drawcolor, 0  ); Labelbackcolorbrush = New  Solidbrush (drawcolor );}  Else  {Labelborderpen = New Pen ( This . Backcolor, 0  ); Labelbackcolorbrush = New Solidbrush ( This  . Backcolor );}  Base  . Onpaint (E); vlblcontrolwidth = This  . Size. width; vlblcontrolheight = This . Size. height; E. Graphics. drawrectangle (labelborderpen,  0 , 0  , Vlblcontrolwidth, vlblcontrolheight); E. Graphics. fillrectangle (labelbackcolorbrush,  0 , 0  , Vlblcontrolwidth, vlblcontrolheight );}  Protected   Override Createparams //  V1.10  { Get  {Createparams CP = Base  . Createparams; CP. exstyle | = Zero X 00000020 ; //  0x20;  //  Enable ws_ex_transparent to make the control transparent                  Return  CP ;}}  /* * [Category ("myopaquelayer"), description ("whether transparent, true by default")] * It is generally used to describe the properties of your custom control ). * Category is used to indicate the category of the attribute. description is the meaning of the attribute.  */  [Category (  "  Myopaquelayer  " ), Description ( "  Whether to use transparency. The default value is true.  "  )]  Public   Bool  Transparentbg { Get  {  Return  _ Transparentbg ;}  Set  {_ Transparentbg = Value;  This  . Invalidate () ;}} [category (  "  Myopaquelayer  " ), Description ( "  Set transparency "  )]  Public   Int  Alpha {  Get  {  Return  _ Alpha ;}  Set  {_ Alpha = Value;  This  . Invalidate ();}}}} 

Opaquecommand method: showopaquelayer (display mask layer) and hideopaquelayer (hide mask layer)

View code

 Using  System;  Using  System. Windows. forms;  Namespace  Loading {  Class  Opaquecommand {  Private Myopaquelayer. myopaquelayer m_opaquelayer = Null ; //  Translucent masked layer         ///   <Summary>          ///  Show Mask Layer  ///   </Summary>          ///   <Param name = "control">  Widget  </Param>          ///   <Param name = "Alpha">  Transparency  </Param>          ///  <Param name = "isshowloadingimage">  Show icons  </Param>          Public   Void Showopaquelayer (Control, Int Alpha, Bool  Isshowloadingimage ){  Try  {  If ( This . M_opaquelayer = Null ){  This . M_opaquelayer = New  Myopaquelayer. myopaquelayer (alpha, isshowloadingimage); control. Controls. Add (  This  . M_opaquelayer );  This . M_opaquelayer.dock = Dockstyle. Fill;  This  . M_opaquelayer.bringtofront ();}  This . M_opaquelayer.enabled =True  ;  This . M_opaquelayer.visible = True  ;}  Catch  {}}  ///   <Summary>          ///  Hide Mask Layer  ///   </Summary>          Public   Void Hideopaquelayer (){  Try  {  If ( This . M_opaquelayer! = Null  ){  This . M_opaquelayer.visible = False  ;  This . M_opaquelayer.enabled = False  ;}} Catch  (Exception ex ){  //  MessageBox. Show (ex. Message );  }}}} 

Source code download: self-defined translucent shadow-source code .rar

 

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.