-
May 7th, 2022, 05:37 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Too many controls using subclassing
Well, I want to make a usercontrol, and inside it there will be more than 100 controls. The problem is that each of these 100 controls will use subclassing.
I wanted to know, from the more experienced ones, if I implement this, it will be heavy.
The events are three: MouseEnter, MouseExit and MouseClick.
The control will be a RibbonBar ..........
-
May 7th, 2022, 06:15 PM
#2
Re: Too many controls using subclassing
I'd say "no". But a control like this, I'd definitely consider compiling it separately and making an OCX out of it.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 7th, 2022, 07:35 PM
#3
Re: Too many controls using subclassing
Originally Posted by Episcopal
if I implement this, it will be heavy.
Only depends on what your new window procedure is doing. I mean if this window procedure performs brain surgery and you have a 100 controls all trying to do it, it will hog the system.
However, I get the sense that you're wondering of subclassing itself has some kind of performance cost. The answer is no because subclassing doesn't really add anything new to a window. It just adds one more level to the window procedure's callstack and given that even without your own added window procedure, the callstack will still be nested very deeply into the OS itself, it's practically a drop in a bucket adding a new window procedure via subclassing.
-
May 7th, 2022, 11:12 PM
#4
Re: Too many controls using subclassing
Yes, the subclassing per-se should not add much overhead.
-
May 8th, 2022, 01:28 AM
#5
Re: Too many controls using subclassing
Originally Posted by Niya
Only depends on what your new window procedure is doing. I mean if this window procedure performs brain surgery and you have a 100 controls all trying to do it, it will hog the system.
However, I get the sense that you're wondering of subclassing itself has some kind of performance cost. The answer is no because subclassing doesn't really add anything new to a window. It just adds one more level to the window procedure's callstack and given that even without your own added window procedure, the callstack will still be nested very deeply into the OS itself, it's practically a drop in a bucket adding a new window procedure via subclassing.
Maybe OP means that Subclassing makes the code very heavy, or the windows messages are processed repeatedly, and affects the performance of the software
-
May 8th, 2022, 02:10 AM
#6
Re: Too many controls using subclassing
Originally Posted by Episcopal
Well, I want to make a usercontrol, and inside it there will be more than 100 controls. The problem is that each of these 100 controls will use subclassing.
I wanted to know, from the more experienced ones, if I implement this, it will be heavy.
The events are three: MouseEnter, MouseExit and MouseClick.
The control will be a RibbonBar ..........
Many times, in order to avoid subclassing, I use Timer, HitTest to handle (simulate) MouseEnter and MouseExit. Also, I don't know if TrackMouseEvent-API helps, I've forgotten its usage.
-
May 8th, 2022, 07:32 AM
#7
Thread Starter
Fanatic Member
Re: Too many controls using subclassing
Originally Posted by Elroy
But a control like this, I'd definitely consider compiling it separately and making an OCX out of it.
Yes, I thought of doing it separately. In fact, it only has about 20 controls inside the usercontrol ..... but in the ribbonbar several types of controls are created, such as buttons and combobox.
-
May 8th, 2022, 07:36 AM
#8
Thread Starter
Fanatic Member
Re: Too many controls using subclassing
Originally Posted by Niya
Only depends on what your new window procedure is doing. I mean if this window procedure performs brain surgery and you have a 100 controls all trying to do it, it will hog the system.
The MouseEnter and MouseExit events will only change the color of the controls.
Originally Posted by Niya
However, I get the sense that you're wondering of subclassing itself has some kind of performance cost.
That's exactly what I wanted to know, as I don't know what goes on behind the scenes.
-
May 8th, 2022, 07:38 AM
#9
Thread Starter
Fanatic Member
Re: Too many controls using subclassing
Originally Posted by SearchingDataOnly
Many times, in order to avoid subclassing, I use Timer, HitTest to handle (simulate) MouseEnter and MouseExit. Also, I don't know if TrackMouseEvent-API helps, I've forgotten its usage.
This I don't know how to do....
Edit: I mean the part of "I use Timer, HitTest to handle (simulate) MouseEnter and MouseExit."
-
May 8th, 2022, 08:35 AM
#10
Re: Too many controls using subclassing
Personally, I'd say a timer that then checks whether certain things are happening (mouse down, keys down, mouse at a certain spot, whatever) would cost far more than a subclass that simply monitors the message pump looking for certain messages. Also, a timer (even a fast one) could miss things (such as the keydown to a keyup), whereas subclassing won't miss anything.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 8th, 2022, 11:23 AM
#11
Re: Too many controls using subclassing
I've tried using mouse tracking via timers and vb mousemove events, it's incredibly unreliable. Especially with a higher interval, but then a lower one is going to have serious performance costs.
The most subclassed controls I've had on a form is probably about 20-30... My shell browser control creates a bunch of subclassed edit controls when you expand the details bar on top of a bunch of subclassed always present controls. Never had an issue with it.
Really a VB6 app is so lean and mean compared to the gargantuan frameworks where everything is abstracted 5 times over and even hello world requires loading dozens of MB of libraries typical of a lot of modern apps, it's very difficult to get a pure VB6 app to have performance issues on modern hardware.
It's like when you play quake 1 and get thousands of fps.
-
May 9th, 2022, 03:49 PM
#12
Thread Starter
Fanatic Member
Re: Too many controls using subclassing
When I was younger I always liked to ask my elders things, because of the experience.
I swear in my ignorance I figured that subclassing generated overhead.
I'll mark the thread as resolved...
-
May 9th, 2022, 03:51 PM
#13
Thread Starter
Fanatic Member
Re: Too many controls using subclassing
-
May 9th, 2022, 06:28 PM
#14
Re: [RESOLVED] Too many controls using subclassing
Sorry, I couldn't log in to vbForums yesterday, so I didn't reply in time.
If subclassing rarely crashes your IDE, then you should continue to use subclassing.
In my opinion, whether subclassing will incur a lot of overhead depends on your code, if there are messages being sent repeatedly, then there will be overhead, which seems to have nothing to do with subclassing. The reason I don't like subclassing is because it often causes my VB6-IDE to crash.
Regarding the use of timer to simulate MouseLeve, ColinE66 shared a control RAD-TreeView, in which cScrollbar.cls contains the usage and skills of mMouseLeaveTimer and HitTest.
In addition, many years ago, I implemented the MouseLeave and MouseEnter operations through the Drag operation of the Label and Image controls in my Button control, and I can't find the code now. I remember that there are similar codes in vbForums and PlanetSourceCode, which is to achieve the highlight (linked -label) effect of the Label through the Drag operation of the Label-control.
-
May 10th, 2022, 12:37 AM
#15
Re: Too many controls using subclassing
Originally Posted by Episcopal
When I was younger I always liked to ask my elders things, because of the experience.
I swear in my ignorance I figured that subclassing generated overhead.
I'll mark the thread as resolved...
Well bear in mind that every time you move your mouse, messages are generated and captured by window procedures for each discrete point it moves. Just think about that for a second. Just imagine all that code executing for each point your mouse pointer moves and we aren't even aware of it. This is give you an idea of how fast the Windows messaging system is.
-
May 10th, 2022, 01:24 AM
#16
Re: [RESOLVED] Too many controls using subclassing
You could subclass a class to affect all the windows of specified class using SetClassLong.
-
May 10th, 2022, 05:17 AM
#17
Re: Too many controls using subclassing
Originally Posted by fafalone
I've tried using mouse tracking via timers and vb mousemove events, it's incredibly unreliable.
Not when you do it right...
(and BTW... IIRC, the WinAPI-SubClassing for MouseHover is using a timer under the covers as well).
One can even make it extremely resource-friendly
(the timer-instance only existing as long as the Control is in hovered state via Controls.Add/Remove)...
Please check it yourself, when you implement a little UserControl, named ucTest (in a virginal VB6-Project):
Code:
Option Explicit
Event MouseEnter(Sender As Object)
Event MouseLeave(Sender As Object)
Private Declare Function GetCursorPos Lib "user32" (pPoint As Any) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal x&, ByVal y&) As Long
Private WithEvents tH As VB.Timer
Public Property Get BackColor() As Long 'just a BackColor-Prop, to demonstrate changes from outside
BackColor = UserControl.BackColor
End Property
Public Property Let BackColor(ByVal RHS As Long)
UserControl.BackColor = RHS
End Property
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
'the next line (placed inside Mouse_Move) will ensure MouseEnter (using tH as a boolean Flag)
If tH Is Nothing Then Set tH = Controls.Add("VB.Timer", "tH"): tH.Interval = 20: RaiseEvent MouseEnter(Me)
End Sub
Private Sub tH_Timer() 'the timer cleans up its own instance when the MouseLeave-condition is true
Dim P&(0 To 1): GetCursorPos P(0)
If hWnd <> WindowFromPoint(P(0), P(1)) Then Controls.Remove "tH": Set tH = Nothing: RaiseEvent MouseLeave(Me)
End Sub
And this into the Test-Form (with two instances of ucTest -> ucTest1 and ucTest2):
Code:
Option Explicit
Private Sub ucTest1_MouseEnter(Sender As Object)
Sender.BackColor = vbGreen
End Sub
Private Sub ucTest2_MouseEnter(Sender As Object)
Sender.BackColor = vbGreen
End Sub
Private Sub ucTest1_MouseLeave(Sender As Object)
Sender.BackColor = &H8000000F
End Sub
Private Sub ucTest2_MouseLeave(Sender As Object)
Sender.BackColor = &H8000000F
End Sub
HTH
Olaf
-
May 11th, 2022, 07:20 PM
#18
Thread Starter
Fanatic Member
Re: [RESOLVED] Too many controls using subclassing
I found this on the internet a long time ago...
Code:
Option Explicit
Private bEnter As Boolean
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32.dll" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
Public Event MouseEnter()
Public Event MouseLeave()
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lpPoint As POINTAPI
GetCursorPos lpPoint
If (Not WindowFromPoint(lpPoint.X, lpPoint.Y) = UserControl.hwnd) Then
ReleaseCapture
If bEnter Then
RaiseEvent MouseLeave
bEnter = False
UserControl.BackColor = vbGreen
End If
ElseIf (WindowFromPoint(lpPoint.X, lpPoint.Y) = UserControl.hwnd) Then
SetCapture UserControl.hwnd
If Not bEnter Then
RaiseEvent MouseEnter
bEnter = True
UserControl.BackColor = vbBlue
End If
End If
End Sub
Olaf ..... is this better than creating vb.timer???
I usually use Paul Caton's autosubclass ... Which alternative would be better?
Last edited by Episcopal; May 11th, 2022 at 07:24 PM.
-
May 11th, 2022, 11:45 PM
#19
Re: [RESOLVED] Too many controls using subclassing
(and BTW... IIRC, the WinAPI-SubClassing for MouseHover is using a timer under the covers as well).
It sets one on top of the main input processing loop, so tends to be a bit better at tracking position. (i.e. the main input loop is always running, and always tracks which window gets the input, TrackMouseEvent flags the main input loop to put a timer after a mouse move, then when the timer fires, checks if it's the same window as when set).
Last edited by fafalone; May 12th, 2022 at 12:00 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|