devxlogo

Make the Background of Your RichTextBox Controls Transparent

Make the Background of Your RichTextBox Controls Transparent

If you plan to require Windows 2000 for your application, you can make your standard VB RichTextBox control 100-percent transparent with a few simple API calls. To try this tip, create a new project (or use an existing one), add a RichTextBox control, and add this code and these declarations in a standard module:

 Option Explicit' Win32 APIs.Private Declare Function GetWindowLong _	Lib "user32" Alias "GetWindowLongA" _	(ByVal hWnd As Long, _	ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong _	Lib "user32" Alias "SetWindowLongA" _	(ByVal hWnd As Long, ByVal nIndex As Long, _	ByVal dwNewLong As Long) As LongPrivate Declare Function SetWindowPos Lib "user32" _	(ByVal hWnd As Long, ByVal hWndInsertAfter As _	Long, ByVal X As Long, ByVal Y As Long, _	ByVal cx As Long, ByVal cy As Long, _	ByVal wFlags As Long) As Long		' Style bits.Private Const GWL_EXSTYLE As Long = (-20)Private Const WS_EX_TRANSPARENT As Long = &H20' Force total redraw that shows new styles.Private Const SWP_FRAMECHANGED = &H20Private Const SWP_NOMOVE = &H2Private Const SWP_NOZORDER = &H4Private Const SWP_NOSIZE = &H1Public Function Transparent(ByVal hWnd As Long, _	Optional ByVal Value As Boolean = True) As _	Boolean	Dim nStyle As Long	Const swpFlags As Long = _		SWP_FRAMECHANGED Or SWP_NOMOVE Or _		SWP_NOZORDER Or SWP_NOSIZE	' Get current style bits.	nStyle = GetWindowLong(hWnd, GWL_EXSTYLE)	' Set new bits as desired.	If Value Then		nStyle = nStyle Or WS_EX_TRANSPARENT	Else		nStyle = nStyle And Not WS_EX_TRANSPARENT	End If	Call SetWindowLong(hWnd, GWL_EXSTYLE, nStyle)	' Force redraw using new bits.	SetWindowPos hWnd, 0, 0, 0, 0, 0, swpFlags	' Make sure new style took.	Transparent = _		(GetWindowLong(hWnd, GWL_EXSTYLE) = nStyle)End FunctionYou can use this function to toggle the transparency of your RichTextBox controls at will:Private Sub Check1_Click()	Call Transparent(RichTextBox1.hWnd, _		(Check1.Value = vbChecked))End Sub

To be on the safe side, check the OS version before making these calls, as the effects can be rather unpleasant in the wrong environment.

That

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist