Results 1 to 14 of 14

Thread: Window subclassing problem (or something)

  1. #1

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275

    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

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    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

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  5. #5

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    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

  6. #6
    New Member
    Join Date
    Mar 2001
    Posts
    12

    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

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  8. #8

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    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

  9. #9
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322
    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 ***

  10. #10

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    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

  11. #11
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  12. #12

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    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

  13. #13
    Tygur
    Guest
    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

  14. #14

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    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
  •  



Click Here to Expand Forum to Full Width