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.


Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job