Results 1 to 5 of 5

Thread: Modify CheckBoxes/Radio buttons to show their background

  1. #1
    Guest
    I found this code which allows you to make CheckBoxes & Opton Buttons Show their backgounds, like Label Controls with their Background set to Transparent.

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

    CREATE SEE-THROUGH CONTROLS
    Most standard controls send WM_CTLCOLORXXXXX messages to their parent when they’re about to draw themselves. VB normally handles these messages and responds appropriately with the ForeColor and BackColor properties that you have set for the control. However, it’s possible to override the standard behavior and achieve a transparent background with several basic control types. This example uses the ubiquitous MsgHook for subclassing, but you can use whichever method you prefer. You must ensure that ClipControls is set to False for the form; otherwise, you’ll see whatever is below your form as the background for the controls instead of the background bitmap:

    Option Explicit

    Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long

    Private Const WM_CTLCOLORSTATIC = &H138
    Private Const TRANSPARENT = 1
    Private Const NULL_BRUSH = 5

    Private Sub Form_Load()
    ' Me.ClipControls = False
    ' Must be set at design-time!
    Msghook1.HwndHook = Me.hWnd
    Msghook1.Message(WM_CTLCOLORSTATIC) = True
    End Sub

    Private Sub Msghook1_Message(ByVal msg As Long, ByVal wp As Long, ByVal lp As Long, result As Long)
    Select Case msg
    Case WM_CTLCOLORSTATIC
    ' Call the original windowproc to handle the
    ' foreground colour for the Controls etc
    Call Msghook1.InvokeWindowProc(msg, wp, lp)

    ' Set the background mode to transparent
    Call SetBkMode(wp, TRANSPARENT)

    ' Get the stock null brush and return it
    ' The brush does nothing when the control
    ' paints using it, hence giving the
    ' transparency effect
    result = GetStockObject(NULL_BRUSH)

    Case Else
    ' [Replace this line with your own code
    ' to call the original windowproc]
    result = Msghook1.InvokeWindowProc(msg, wp, lp)
    End Select
    End Sub
    ----------------------------------------------------------

    I Cant get this to work using normal SubClassing without the Mabry MsgHook.ocx. Can somebody help.

    What i want is to display the Picture Behind without any ugly patches. Thanks a lot.

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Hey Hartless!

    This should do it!


    In a module:
    Code:
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    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
    Public Const GWL_WNDPROC = (-4)
    
    Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long 
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long 
    Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long 
    
    Private Const WM_CTLCOLORSTATIC = &H138 
    Private Const TRANSPARENT = 1 
    Private Const NULL_BRUSH = 5 
    
    
    Dim Proc As Long
    
    Public Sub SubClass(hWnd As Long)
        Proc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    
    Public Sub unSubClass(hWnd As Long)
        SetWindowLong hWnd, GWL_WNDPROC, Proc
    End Sub
    
    Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If uMsg = WM_CTLCOLORSTATIC then
    ' Call the original windowproc to handle the 
    ' foreground colour for the Controls etc 
    
        WindowProc = CallWindowProc(Proc, hwnd, uMsg, wParam, lParam)
    
    ' Set the background mode to transparent 
    Call SetBkMode(wp, TRANSPARENT) 
    
    ' Get the stock null brush and return it 
    ' The brush does nothing when the control 
    ' paints using it, hence giving the 
    ' transparency effect 
    result = GetStockObject(NULL_BRUSH) 
    Else
        WindowProc = CallWindowProc(Proc, hwnd, uMsg, wParam, lParam)
    End if
    End Function
    in a form:
    Code:
    Private Sub Form_Load()
    SubClass Me.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    unSubClass Me.hWnd
    End Sub
    Something like that, but that's main way of subclassing and intercepting the messages.
    I their code to redraw the stuff in the WindowProc sub.

    Have fun!

    [Edited by Jop on 12-26-2000 at 05:56 PM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Guest
    Thanks for your you prompt reply, but I'm afraid that doesn't work

    I tried it using similar code but it doesn't work without the Mabry MsgHook Control.

    Thanks anyways.

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hmm.. download & use it then
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Guest
    in the properties window
    you can change the mode of the
    object to a graphic mode
    this should be working

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width