How to Make textbox/RichTextBox transparent

Source: Internet
Author: User

The problem is:TextboxAndRichTextBoxBackcolorAttributeNot acceptableColor. Transparent. So I giveTextboxAndRichTextBoxControl to find the corresponding solution

This is not a perfect solution. It may be hard for beginners to understand it. If youProgramDesign and C # are good, so this articleArticleIt must be a piece of cake for you.

 

RichTextBox Class Transparentcontrol: Control
{
Public Transparentcontrol ()
{
Base . Setstyle (controlstyles. userpaint, True );
Base . Setstyle (controlstyles. doublebuffer, True );
Base . Setstyle (controlstyles. supportstransparentbackcolor, True );
}
}

ClassTransparentrichtextbox: RichTextBox
{
PublicTransparentrichtextbox ()
{
Base. Scrollbars=Richtextboxscrollbars. None;
}

Override   Protected Createparams
{
Get
{
Createparams CP =   Base . Createparams;
CP. exstyle | =   0x20 ;
Return CP;
}
}

Override Protected VoidOnpaintbackground (painteventargs E)
{
}
}

 

 

Textbox

 

Public   Class Transparenttextbox: textbox
{
Picturebox =   New Picturebox ();

Public Transparenttextbox ()
{
Picturebox. Dock = Dockstyle. Fill;
This . Controls. Add (picturebox );
}
Protected   Override   Void Wndproc ( Ref Message m)
{
Base . Wndproc ( Ref M );
Switch (M. msg)
{
Case Win32.wm _ paint:

Bitmap bmpcaptured =  
New Bitmap ( This . Clientrectangle. Width, This . Clientrectangle. Height );
Bitmap BMP result =  
New Bitmap ( This . Clientrectangle. Width, This . Clientrectangle. Height );
Rectangle R =  
New Rectangle ( 0 , 0 , This . Clientrectangle. Width,
This . Clientrectangle. Height );
 
Capturewindow ( This , Ref Bmpcaptured );
This . Setstyle (controlstyles. supportstransparentbackcolor, True );
This . Backcolor = Color. transparent;

Imageattributes imgattrib= NewImageattributes ();

Colormap [] colormap= NewColormap [1];

Colormap [0]= NewColormap ();

Colormap [0]. Oldcolor=Color. White;

Colormap [0]. Newcolor=Color. transparent;

Imgattrib. setremaptable (colormap );

Graphics g=Graphics. fromimage (BMP result );

G. drawimage (bmpcaptured, R,0,0,This. Clientrectangle. Width,
This. Clientrectangle. Height, graphicsunit. pixel, imgattrib );

G. Dispose ();

Picturebox. Image=(Image) BMP result. Clone ();
Break;

CaseWin32.wm _ hscroll:

CaseWin32.wm _ vscroll:

This. Invalidate ();//Repaint

//If you use scrolling then add these two case statements

Break;
}
}

Private   Static   Void Capturewindow (Control, Ref Bitmap bitmap)
{
Graphics g = Graphics. fromimage (Bitmap );
Int I = ( Int ) (Win32.prf _ Client | Win32.prf _ erasebkgnd );
Intptr iptr =   New Intptr ( 14 );
Intptr HDC = G. gethdc ();
Win32.sendmessage (control. Handle, win32.wm _ print, HDC, iptr );
G. releasehdc (HDC );
G. Dispose ();
}

 

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.