Click here to Skip to main content
16,005,290 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Common Events Pin
jboss16-Dec-03 14:53
jboss16-Dec-03 14:53 
QuestionHow to read the audio volume meter in VB.NET Pin
nordta15-Dec-03 11:26
nordta15-Dec-03 11:26 
QuestionHow to make shortcut/hotkey Pin
cometz15-Dec-03 6:08
cometz15-Dec-03 6:08 
QuestionHow to execute Dos command line in VB? Pin
Thunder200415-Dec-03 3:07
Thunder200415-Dec-03 3:07 
AnswerRe: How to execute Dos command line in VB? Pin
Thunder200415-Dec-03 4:04
Thunder200415-Dec-03 4:04 
GeneralNeed a huge favor and an honest oppinion Pin
cnurse15-Dec-03 0:27
cnurse15-Dec-03 0:27 
Questionhow to brush color into a textbox ? Pin
dungti14-Dec-03 20:02
dungti14-Dec-03 20:02 
AnswerRe: how to brush color into a textbox ? Pin
cnurse15-Dec-03 0:30
cnurse15-Dec-03 0:30 
Dungti,

If you are using VB.NET then just set the background color. If you are using VB 6 then you will have to work your way through this lot...It's on MSDN.

PSS ID Number: Q174301

Article Last Modified on 01-11-2001


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual Basic Learning Edition for Windows 6.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Windows 6.0
Microsoft Visual Basic Control Creation Edition for Windows 5.0
Microsoft Visual Basic Learning Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0

--------------------------------------------------------------------------------


Summary
When a dithered color is selected as the background of a TextBox, the background around the text is displayed as a different, solid color when the display is set for 256 colors or less. To work around this limitation, you must hook the WM_CTLCOLOREDIT message and set the text background color to TRANSPARENT. This is not a trivial alternative, and using a solid background color is recommended if at all possible.



More Information
WARNING: ANY USE BY YOU OF THE SAMPLE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this sample code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

WARNING: Failure to unhook a window before its imminent destruction will result in application errors, Invalid Page Faults, and data loss. This is due to the fact that the new WindowProc function being pointed to no longer exists, but the window has not been notified of the change. Always unhook the sub-classed window upon unloading the sub-classed form or exiting the application. This is especially important while debugging an application that uses this technique within the Microsoft Visual Basic 5.0 Development Environment. Pressing the END button or selecting End from the Run menu without unhooking will cause an Invalid Page Fault and close Microsoft Visual Basic.

A TextBox control cannot draw text with a dithered background color, but it can draw text with a transparent background color. If the background of the control is dithered, this achieves the same visual effect. (Note the distinction between the background of the text and the background of the control. It is simple to get the control's background dithered; the problem is getting the text background dithered as well.)

From the SDK perspective, the answer is simple; handle the WM_CTLCOLOREDIT message in the parent and call SetBkMode() to set the background mode to TRANSPARENT. This is less simple in Visual Basic.

The following application consists of a form with a single Textbox control and a code module:


Create a new Visual Basic "Standard EXE" project.


Add a Textbox to Form1.


Paste the following code into Form1's code module:
Private Sub Form_Load()
gHW = Me.hwnd
Text1.BackColor = &HC0E0FF
Hook
End Sub

Private Sub Form_Unload(Cancel As Integer)
Unhook
End Sub




Add a module.


Paste the following code into this module:
Declare Function CallWindowProc Lib "user32" Alias _
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As _
Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As _
Long) As Long

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Declare Function SetBkMode Lib "gdi32" _
(ByVal hdc As Long, ByVal nBkMode As Long) As Long

Public Const WM_CTLCOLOREDIT = &H133
Public Const TRANSPARENT = 1
Public Const GWL_WNDPROC = -4

Global lpPrevWndProc As Long
Global gHW As Long

Public Sub Hook()
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub

Public Sub Unhook()
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As _
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_CTLCOLOREDIT Then
i = SetBkMode(wParam, TRANSPARENT)
End If
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, _
lParam)
End Function




Save the project, and then run it.

RESULT: When the form is displayed, you will see a TextBox with a dithered background. Type into the TextBox and the dithered background is maintained.


To terminate the application, use Alt-F4, the control box menu, or click the close button on the form. If you click the stop button in the Visual Basic Design environment, the Unload event is not triggered to unhook the window and an IPF will occur.





References
For additional information, please see the following article in the Microsoft Knowledge Base:


Q168795 : HOWTO: Hook Into a Window's Messages Using AddressOf

Additional query words: kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbControl

Keywords: kbGrpDSVB
Issue Type: kbhowto
Technology: kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB500Search kbVB600Search kbVBA500Search kbVBA500 kbVBA600 kbVB500 kbVB600 kbZNotKeyword3


GeneralRe: how to brush color into a textbox ? Pin
dungti15-Dec-03 15:19
dungti15-Dec-03 15:19 
GeneralAutomatically loading object properties from SQL data sources Pin
cnurse13-Dec-03 22:39
cnurse13-Dec-03 22:39 
GeneralRe: Automatically loading object properties from SQL data sources Pin
cnurse13-Dec-03 23:03
cnurse13-Dec-03 23:03 
GeneralRe: Automatically loading object properties from SQL data sources Pin
cnurse13-Dec-03 23:13
cnurse13-Dec-03 23:13 
QuestionHow Can i Use Comm in .NET???? Pin
13-Dec-03 21:30
suss13-Dec-03 21:30 
AnswerRe: How Can i Use Comm in .NET???? Pin
elRaptor26-Dec-03 10:37
elRaptor26-Dec-03 10:37 
AnswerRe: How Can i Use Comm in .NET???? Pin
elRaptor31-Dec-03 8:20
elRaptor31-Dec-03 8:20 
GeneralScaling and moving graphics paths Pin
cnurse13-Dec-03 21:16
cnurse13-Dec-03 21:16 
GeneralVB Help Transfering Info from Word to Access Database Pin
^Kyr13-Dec-03 14:51
suss^Kyr13-Dec-03 14:51 
GeneralRe: VB Help Transfering Info from Word to Access Database Pin
^Kyr15-Dec-03 13:03
suss^Kyr15-Dec-03 13:03 
GeneralRe: VB Help Transfering Info from Word to Access Database Pin
Vipin Bokariya15-Dec-03 20:38
Vipin Bokariya15-Dec-03 20:38 
GeneralEdit source files while debugging Pin
Charlie Williams13-Dec-03 8:55
Charlie Williams13-Dec-03 8:55 
GeneralRe: Edit source files while debugging Pin
Dave Kreskowiak14-Dec-03 4:56
mveDave Kreskowiak14-Dec-03 4:56 
GeneralRe: Edit source files while debugging Pin
Charlie Williams14-Dec-03 8:11
Charlie Williams14-Dec-03 8:11 
GeneralTransparencyKey usage consuming memory Pin
gregcost13-Dec-03 6:56
gregcost13-Dec-03 6:56 
QuestionHow to declare a public variable that can be edit by forms?VB.Net Pin
MJay13-Dec-03 5:10
MJay13-Dec-03 5:10 
QuestionHow to Draw 3D charts Pin
xprtguro13-Dec-03 2:40
xprtguro13-Dec-03 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.