Results 1 to 40 of 41

Thread: [vb6] Transparent PictureBox

Threaded View

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    [vb6] Transparent PictureBox

    Side Note. As of Windows 8, controls can now be made semi-transparent by applying the WS_EX_LAYERED extended window style via SetWindowLong or CreateWindowEx. This option previously existed only for the main-level window/form. However, VB picturebox still won't work since it has a class style of CS_OWNDC. That style along with the CS_CLASSDC prevents WS_EX_LAYERED from taking effect. To test a child control for compatibility:
    Code:
    Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Const GCL_STYLE = -26&
    Const CS_CLASSDC = &H40
    Const CS_OWNDC = &H20
    
    If (GetClassLong(childHwnd, GCL_STYLE) And (CS_CLASSDC Or CS_OWNDC)) = 0 Then ' can use WS_EX_LAYERED
    Note: In order to use layered child windows, the application has to declare itself Windows 8-aware in the manifest

    Updated: 27 Feb 2011
    Added OpacityPercent property to control. Allows picturebox to appear between 0% to 100% opaque.
    --------------------------------------------------------------------------------------------------------------

    The attached usercontrol and module will allow a picturebox to fake transparency very well. It can also fake transparency for any option buttons and checkboxes contained within the picturebox. Labels are your responsibility, simply set their backstyle property to transparent.

    How to use the usercontrol and module. The module is used for subclassing only.
    Subclassing will only be used if checkboxes or option buttons are to be made to fake transparency.
    1) Simply add them to your project. It was not designed as a stand-alone ocx
    2) Add one usercontrol for each picturebox you want to fake transparency
    3) Picturebox borders are up to you. Control will work with/without them
    4) On form_load, assign the usercontrol its picturebox via the AttachBuddy function
    5) Read the short comments at the top of the control's code for more details.

    Sample with themed controls
    Name:  screenshot.PNG
Views: 32042
Size:  121.8 KB

    Some notes. As already mentioned, subclassing is only in play if you opt to make checkboxes or option buttons fake transparency.
    Using gradient backgrounds might be a good example, but using textured backgrounds and placing text over them can be hard on your users' eyes. I, for example, am very color blind. Captchas over textured backgrounds drive me nuts. So would your app if it had similar appearance.
    Also, you should test your app with and without a manifest (themed controls). For example, you may lose your text forecolor with themed controls. So placing a checkbox with white text over a very dark background looks good un-themed, but text may become invisible when themed. Keep this in mind, you may want 2 different color schemes. You can call the ValidateThemeEmployed function.

    Limitations for the ucPicBuddy
    1) One ucPicBuddy per picturebox. It must exist on the same form as the picturebox. The ucPicBuddy control will move itself appropriately.
    2) Do not place any non-windowless controls between the picturebox and its container (i.e., form, frame, other picturebox)
    -- picturebox looks transparent but is not. Placing controls behind it will not allow you to click on them & they won't show through
    3) You can place any windowless controls between the picturebox and its container. They will show through & are not clickable.
    4) You should ensure the picturebox and its ucPicBuddy control have synchronized .Visible property settings to avoid unnecessary paints
    5) Option buttons/checkboxes with .Style property = Graphical are not supported
    6) Some actions at runtime require you to refresh what changed: Form's BackColor, Option Button/CheckBox caption at runtime
    7) Changing option button/checkbox .Alignment property at runtime requires you to call the AttachChildControl to re-add it to the ucPicBuddy control.
    -- This is because VB destroys that checkbox/option button & recreates a new one with the new Alignment property
    .
    Attached Files Attached Files
    Last edited by LaVolpe; Mar 4th, 2015 at 10:23 AM. Reason: added the Side Note
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Tags for this Thread

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