how can create opacity percent property for my user control?-VBForums
Results 1 to 21 of 21

Thread: how can create opacity percent property for my user control?

  1. #1

    Thread Starter
    Lively Member Black_Storm's Avatar
    Join Date
    Sep 2007
    Posts
    78

    Post how can create opacity percent property for my user control?

    i need create my custom user control to support opacity percent between 0 to 100.
    for example i need create my custom label or my custom text box or my custom picture box with opacity percent?

  2. #2
    Fanatic Member
    Join Date
    Sep 2012
    Posts
    534

    Re: how can create opacity percent property for my user control?

    Maybe the following link is useful to you:

    http://www.vbforums.com/showthread.p...47#post5244547

  3. #3
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    383

    Re: how can create opacity percent property for my user control?

    if i where to do something like this i would "not" use any of the vb own components, instead creating it from scratch, use a image as base and use API to draw it, as you know we have alphablend that can do the opacity for you.
    if you use a usercontrol you could set it to transparent/+invisible (at runtime) and visible in IDE, and draw directly into the parent hdc.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,846

    Re: how can create opacity percent property for my user control?

    Just FYI. GDI+ has methods to draw strings at any opacity you desire. Maybe search the forum for examples of GDI+ and strings.
    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}

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    We covered this in earlier threads. Easy enough on a modern version of Windows, not so much for the dying Windows 7 or any dead OS earlier than that though.

    Example:

    Name:  sshot.png
Views: 97
Size:  13.3 KB

    This demo bounces the opacity up and down on a Timer.

    Windows 8 or later is required. Anything older probably means a lot more work painting the entire UI yourself.


    Note:

    For this to work it requires a manifest, so for most of us the IDE doesn't have the required manifest and only in the compiled program will the UC have alpha transparency.
    Attached Files Attached Files
    Last edited by dilettante; Dec 26th, 2017 at 09:33 PM. Reason: added a note

  6. #6
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    383

    Re: how can create opacity percent property for my user control?

    dying windows 7? my work, my computers all have windows 7. so its not dying at all.
    like windows xp, 7 will be around for many years.
    the only thing that would force people to move to 10 is directx12, nothing else.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    Windows 7 is in Extended Support and Microsoft says even then it cannot be made secure.

    Microsoft warns Windows 7 is dangerously insecure in 2017

    Besides that, Win7 SP1 has several updates that introduced erroneous binary compatibility breaks that will never be fixed. The fixes are in Win8 and later only.

    New features like the one I used above will never be back-ported to Win7. It is a sinking ship in flames. We don't have to like it, but fighting reality has its limits.

  8. #8
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    383

    Re: how can create opacity percent property for my user control?

    we could argue about this a lot, but i will leave it as it is, instead i will go back to the OP's question.
    no matter, people still uses 7 and even xp, if you have 10 and you don't plan to share it with others, i would go with dilettante's suggestion, if not, then check the forum for API examples about GDI/GDI+. its not that hard to make, you only need a few API's and a bit of code to emulate the different controls you plan to have.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    Properly emulating controls from dirt can be a ton of work. If you have to support obsolete OSs then you might be better off with a 3rd party widget library instead... and at that point you may as well be writing in FreeBASIC or C.

    Here is all of the "plumbing" used to get the desired effect on Win8 and later:

    Code:
    Option Explicit
    
    'Requires Windows 8 or later.
    
    Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongW" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long
    
    Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
        ByVal hWnd As Long, _
        ByVal crKey As Long, _
        ByVal bAlpha As Byte, _
        ByVal dwFlags As Long) As Long
    
    Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongW" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long
    
    Private Const GWL_EXSTYLE = -20
    Private Const WS_EX_LAYERED = &H80000
    Private Const LWA_COLORKEY = &H1&
    Private Const LWA_ALPHA = &H2&
    
    Private mAlpha As Byte
    
    Public Property Get Alpha() As Byte
        Alpha = mAlpha
    End Property
    
    Public Property Let Alpha(ByVal RHS As Byte)
        mAlpha = RHS
        If Extender.Visible Then ApplyAlpha
        PropertyChanged "Alpha"
    End Property
    
    Private Sub ApplyAlpha()
        SetLayeredWindowAttributes hWnd, 0, mAlpha, LWA_ALPHA
    End Sub
    
    Private Sub UserControl_InitProperties()
        Alpha = 255 'Default.
    End Sub
    
    Private Sub UserControl_Paint()
        Size 3030, 2280
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        Alpha = PropBag.ReadProperty("Alpha", 255)
    End Sub
    
    Private Sub UserControl_Show()
        If Ambient.UserMode Then
            SetWindowLong hWnd, _
                          GWL_EXSTYLE, _
                          GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
            ApplyAlpha
        End If
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        PropBag.WriteProperty "Alpha", Alpha, 255
    End Sub
    Last edited by dilettante; Dec 26th, 2017 at 09:50 PM.

  10. #10

    Thread Starter
    Lively Member Black_Storm's Avatar
    Join Date
    Sep 2007
    Posts
    78

    Re: how can create opacity percent property for my user control?

    problem not fixed yet,any sample for work on win 7? just a simple user control with opacity property.i found many example usercontrol to hv opacity properties too but was been complicated.i need simple sample.

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,846

    Re: how can create opacity percent property for my user control?

    Any custom control, ultimately is a bitmap. Prior to Windows 8 where we could not make controls semitransparent, there were only a couple solutions and none are 'easy'.

    1. Create a windowless control and draw your control onto the parent DC via UserControl_Paint at whatever opacity you want.

    2. Capture the background behind the usercontrol and blend your usercontrol into the background and paint that. This is problematic if the usercontrol's parent background can change, i.e., background color changes or anything behind the control changes like animation, etc.
    Edited: this option not suitable if the control requires user-input, like a textbox. Why? Because what you have drawn is a snapshot of the control not the control itself. Suitable for pictures, labels. If used for textboxes or other user-input type of controls, becomes much more difficult.

    3. Complicated subclassing routines used to intercept the parent's Paint event to take over the drawing of the area occupied by your usercontrol, then drawing in that area what you want to be drawn. Similar limitations as in #2 above.

    As newer O/S come from Microsoft, sometimes they offer easier ways of doing things. Win8 offered semitransparency for child controls when only the form-level was offered before. Prior to Win2k, form-level semi-transparency wasn't offered. Safer methods of subclassing in the IDE were offered in Win2k and before that, required more complex methods for safety. The list goes on and on. In another 20 years, if VB is still supported, people may look back and say, "Holy crap, you mean you had no easy way to do [fill in the blank] back then?"
    Last edited by LaVolpe; Jan 1st, 2018 at 09:13 AM.
    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}

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    If the UserControl is just a static image things don't get too complex. But once you add a Textbox or even a Label that can display a blinking caret or changing text it becomes another story... not to mention more complex controls.

    Even using layered windows can be problematic. For example a dropdown list for a ComboBox appears solid and opaque even when the textbox part of it is translucent.


    If you are writing games or other funky UIs that break Windows design conventions you are better off just drawing everything manually instead of using controls at all.

    Alpha transparency is more of a stunt than anything else and should be used sparingly when at all.

  13. #13

    Thread Starter
    Lively Member Black_Storm's Avatar
    Join Date
    Sep 2007
    Posts
    78

    Re: how can create opacity percent property for my user control?

    my langauge is not english so i always have problem for understand descriptions but can send a exmplae work on win 7 ? and i want have been a user control and then when i seted opacity to 50% so my user controls opacity change to 50% (with any contains like text box in it or ... )
    Last edited by Black_Storm; Jan 2nd, 2018 at 07:22 AM.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    This isn't perfect or complete, but it is one way that might give you some idea where to begin. Should work even on Windows 95 since you want to support obsolete OSs.

    It relies on MSWLESS.OCX for a TextBox though.


    Name:  sshot.png
Views: 47
Size:  19.9 KB


    It sounds like you need a lot more help though. If you need custom programming services this isn't the place to ask for it.
    Attached Files Attached Files

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    Here is an updated version that corrects a flaw in the transparent OmegaLabel UserControl that caused dynamic changes to Caption to produce muddy overwritten images.

    There are lots of other improvements that could be made to these as well of course, such as additional properties or events if you need them.
    Attached Files Attached Files

  16. #16
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    383

    Re: how can create opacity percent property for my user control?

    mswless.ocx what is that and how to get it?
    im not sure its legal to copy from an early os.

  17. #17
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    It comes with VB6 but must be installed from the CDs separately. Browse your CDs to find it, the location varies with Edition.

    See: "Q184687: INFO: Lightweight Controls in Visual Basic 6.0"

    You can find this on your MSDN CDs by searching for Q184687
    Last edited by dilettante; Jan 3rd, 2018 at 11:05 PM. Reason: link removed

  18. #18
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    383

    Re: how can create opacity percent property for my user control?

    will look into it when i get home, interesting, i never looked into that folder, i always thought that it would be copied when installing.
    very odd that they don't include an option when installing. im sure a lot of people don't even know about it!

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    Makes you wonder where they have been for 20 years.

  20. #20
    Fanatic Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    923

    Re: how can create opacity percent property for my user control?

    There's been a lot of talk about copyright and piracy around here lately, and this is the only reason I bring this up now: that GitHub KB archive is kinda interesting in that it maintains the Microsoft copyright notice, and even going to the top page at https://jeffpar.github.io/kbarchive/ does, but I don't see any permission to republish posted anywhere? Is this pirated material too? Normally it's the kind of stuff I'd just pass over without issue, but due to all the re-examination of copyright law lately, I'm interested as to what the status of stuff like this is.

  21. #21
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,036

    Re: how can create opacity percent property for my user control?

    Hard to say, but I removed the link and replaced it with an alternative source.

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.