|
-
Apr 11th, 2001, 06:32 PM
#1
Thread Starter
Hyperactive Member
Window subclassing (does this scare everyone off :)
This is kinda funny... I got this code from someone a while ago that subclasses the form and helps me determine if the form gets the focus or not, but each time I quit my application, VB crashes, without ANY error message, the window just disappears and I'm left wondering what just happened...
Has this happened to any of you and do you know how to stop this?
-JR-
Last edited by Jareware; Apr 11th, 2001 at 06:42 PM.
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
-
Apr 11th, 2001, 06:43 PM
#2
don't know much about subclassing, but you hooked the form on the Form Load, right? Did you remember to unhook it in the Form Unload??
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 11th, 2001, 06:50 PM
#3
Thread Starter
Hyperactive Member
Well actually when I took the code OUT of the unload event, it actually didn't crash anymore @ program end. (I hear a big but coming...) But now if a normal unhandeled error occurs inside the program, vb cathes it and when I say END (in the dialog box) it crahses again. Wonder why the guy who gave me the code said that "subclassing is crash-prone"... 
Well actually this one is kinda holding my project back. You don't happen to know any other way of doing this do you? Like, for example change contents of Label1.Caption when the form gets or loses the focus?
-JR-
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
-
Apr 11th, 2001, 06:57 PM
#4
Code:
'interval of say 1
Private Sub Timer1_Timer()
If Screen.ActiveForm.Name = Me.Name Then
Label1.Caption = "Active"
Else
Label1.Caption = "Inactive"
End If
End Sub
does that do it?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 11th, 2001, 07:04 PM
#5
Thread Starter
Hyperactive Member
Says:
Object variable or With block variable not set (runtime error 91)
Do you know what could be wrong (because I'm getting pretty hopeful that something like this could abolish the need for the hundred lines of code that I have now that only crashes my vb )?
-JR-
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
-
Apr 11th, 2001, 07:15 PM
#6
New Member
answer to your question
Hey,
This is a problem with VB and subclassing. You can not End the program with the Stop button or the "End" button when an error occurs otherwise VB will terminate.
You must do it by closing your app using the "X" button and make sure that you Unhook your sub class in the form's unload method.
Hope that helps.
Nick
-
Apr 11th, 2001, 08:10 PM
#7
Originally posted by Jareware
Says:
Object variable or With block variable not set (runtime error 91)
Do you know what could be wrong (because I'm getting pretty hopeful that something like this could abolish the need for the hundred lines of code that I have now that only crashes my vb )?
-JR-
make sure all your controls are named right, and be sure to have a timer on the form your using named Timer1...
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 11th, 2001, 10:08 PM
#8
Thread Starter
Hyperactive Member
Actually I took this kind of an approach:
Code:
Private Sub Form_Resize()
Do
If Screen.ActiveForm.Name = Me.Name Then
Label1.Caption = "Active"
Else
Label1.Caption = "Inactive"
End If
DoEvents
Loop
End Sub
Unfortunately, it only says INACTIVE and then stays the same no matter what I do with the form...
Any ideas on what might be wrong?
(So close I'd hate to give up )
-JR-
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
-
Apr 11th, 2001, 10:23 PM
#9
Hyperactive Member
JareWare,
I remember you as the guy who wanted to show if a form is active...And I recommended Subclassing. Megatron gave you the codes for subclassing.
One thing to remember is to Unhook the Window once you unload or terminate the form.
Therefore close your app by clicking on the X button. NOT by pressing the STOP button on the VB IDE itself as that will just END your app abruptly without firing any events at all. Therefore your form_unload is bypassed. and the Subclassing is still Hooked.
Subclassing is a method that is very powerful. It encompasses one of the most important C++ function to be used in VB. So powerful that MS actually forbid anybody to hook a windows procedure that doesnt belong to you since the onset of VB4.
William T
Software Architect / Chief Software Developer
Softwaremaker.Net Pte Ltd
http://www.Softwaremaker.net
*** Things are always the darkest before they go pitch black ***
-
Apr 11th, 2001, 10:56 PM
#10
Thread Starter
Hyperactive Member
Oh, hi. The code actually worked fine with the example I did back then, and thanks for helping me out then. But in that code, the way to unhook it (supposedly) was the same that was used to activate it in the first place:
Code:
StartSubclassing Me.hWnd
Is this correct?
Actually (I'm so sorry), I have to leave to Hawaii tomorrow ( ) for spring break, so would it be OK with you guys if we would kindofa put this thread in "stasis" for a week or so, I'll post a new message when I'm back so you will get the emails. I mean if that's ok, it would be great.
And I'm going to see about the subclassing stuff then, thanks for all the help.
Catch you later then, plane is waiting (well, tomorrow anyway, now I have about n^10 times too much homework to finish before I go to bed )...
-JR-
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
-
Apr 20th, 2001, 05:54 PM
#11
did you try it in a Timer, because it worked for me when I did it
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 20th, 2001, 09:36 PM
#12
Thread Starter
Hyperactive Member
If it's a really simple form can you attach it to this thread since I can't seem to get it working... maybe it has something to do with me running windows 2000, although I doubt it...
-JR-
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
-
Apr 20th, 2001, 10:03 PM
#13
I don't see why that "screen.activeform.name thingy" should work. I think Jareware wants to detect whether the focus goes to another window outside of vb. That screen.activeform.name code will not do that. It will only detect if you go to another form in the same program.
My recommendation if you don't want to subclass is to use the GetForegroundWindow API.
Just put this code in the timer:
Code:
If Me.hWnd = GetForegroundWindow Then
Label1.Caption = "Active"
Else
Label1.Caption = "Inactive"
End If
And put this in the General Declarations area at the top of the form code:
Code:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
-
Apr 21st, 2001, 02:10 PM
#14
Thread Starter
Hyperactive Member
Oh tygur thanks I love you! It works! x 100
And thanks you all for helping!
-JR-
"Blessed are we who can laugh at ourselves for we shall never cease to be amused"
- Unknown
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
|