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?
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.
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:
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.
Last edited by dilettante; Dec 26th, 2017 at 09:33 PM.
Reason: added a note
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.
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.
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.
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.
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.
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"
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.
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.
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.
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.
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.
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!
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.