Closing a window when you only have the Title


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Closing a window when you only have the Title

Hybrid View

  1. #1
    Brent Guest

    Closing a window when you only have the Title


    Hi all,
    Typically I would not post this question, but I don't have my API Bible today...

    All I have is the Title of window (its a dialog from a Crystal Report).

    When my app shutsdown I need to check if this window is displayed and if
    it is close it. Problem is I don't have the ClassName of the Window, I only
    have the Title.

    So what I need is the API that will take the Title, and return the ClassName
    so I can get the hWnd to close the window.

    Any help would be greatly appreciated.

    Thanks,
    Brent

  2. #2
    Phil Weber Guest

    Re: Closing a window when you only have the Title

    > What I need is the API that will take the Title, and
    > return the ClassName so I can get the hWnd to close
    > the window.


    Brent: You don't need the classname; you can get the hWnd directly from the
    title. Download the FindPart.zip sample from Karl Peterson's Web site
    ( http://www.mvps.org/vb/samples.htm ) to see how to do so.
    ---
    Phil Weber



  3. #3
    Brent Guest

    Re: Closing a window when you only have the Title


    "Phil Weber" <pweber@teleport.com> wrote:
    > > What I need is the API that will take the Title, and
    > > return the ClassName so I can get the hWnd to close
    > > the window.

    >
    >Brent: You don't need the classname; you can get the hWnd
    >directly from the title. Download the FindPart.zip sample from
    >Karl Peterson's Web site ( http://www.mvps.org/vb/samples.htm )
    >to see how to do so.
    >---
    >Phil Weber
    >


    Phil,
    Thanks for the tip. I tried it, but it doesn't seem to work (I'm getting
    an error "Invalid use of AddressOf" or something like that). I actually
    decided to disable the Main MDI form behind the dialog (this achieves what
    I'm looking for - simulates the dialog being Modal). I've also done this
    for simplicity's sake since I won't be on this project much longer (ease
    of maintainability). I hate doing it this way, but I'm not sure how intelligent
    the next developer will be either... I am definitely going to get this to
    work in a smaller app so I know what the problem is...

    If you have and hints as to where I should look I would appreciate it.

    Thanks again,
    Brent

  4. #4
    Joel Ryan Guest

    Re: Closing a window when you only have the Title

    Here's the code I use to find a window. To close it, send it the message WM_CLOSE

    Something like this:

    Private Const WM_CLOSE = &H10
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
    lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd
    As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Sub Command1_Click()
    lWnd = FindWindow(vbNullString, txtCaption.Text)
    Call SendMessage(lWnd, WM_CLOSE, 0, ByVal 0)
    End Sub

    -- Joel



  5. #5
    Paul Little Guest

    Re: Closing a window when you only have the Title

    Brent, I know you've already received other answers (and found your own
    workaround), but I thought I'd add some food-for-thought. If your app is
    creating the Crystal Report window (i.e. Report.Preview or Report.Action =
    1) then there are cleaner ways to close the window (or just wait for the
    user to close the window before proceeding in your app (i.e. a Modal
    preview)). How to do this depends on the mechanism you are using to display
    the Crystal Report (OCX, Automation components, RDC, CRPE API, etc.). If
    you'd like specifics, just post back the method you are using to display the
    report (you can just paste the code that is used to display it if that is
    easier).

    -Paul
    --
    Paul Little, Senior Programmer/Analyst
    SVi Retail Systems
    San Diego, CA

    "Brent" <Brent.Leister@Robot-rx.com> wrote in message
    news:390d93ed@news.devx.com...
    >
    > Hi all,
    > Typically I would not post this question, but I don't have my API Bible

    today...
    >
    > All I have is the Title of window (its a dialog from a Crystal Report).
    >
    > When my app shutsdown I need to check if this window is displayed and if
    > it is close it. Problem is I don't have the ClassName of the Window, I

    only
    > have the Title.
    >
    > So what I need is the API that will take the Title, and return the

    ClassName
    > so I can get the hWnd to close the window.
    >
    > Any help would be greatly appreciated.
    >
    > Thanks,
    > Brent




  6. #6
    Brent Guest

    Re: Closing a window when you only have the Title


    Joel,
    Thanks the code worked perfectly!

    Joel Ryan <jryan@vsi-hq.com> wrote:
    >Here's the code I use to find a window. To close it, send it the message

    WM_CLOSE
    >
    >Something like this:
    >
    >Private Const WM_CLOSE = &H10
    >Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
    >lpClassName As String, ByVal lpWindowName As String) As Long
    >Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal

    hwnd
    >As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    >
    >Sub Command1_Click()
    > lWnd = FindWindow(vbNullString, txtCaption.Text)
    > Call SendMessage(lWnd, WM_CLOSE, 0, ByVal 0)
    >End Sub
    >
    >-- Joel
    >
    >



  7. #7
    Karl E. Peterson Guest

    Re: Closing a window when you only have the Title

    Hi Brent --

    > Thanks for the tip. I tried it, but it doesn't seem to work (I'm getting
    > an error "Invalid use of AddressOf" or something like that).


    You can only use AddressOf to point to routines within a BAS module. Did you suck
    the code directly into one of your forms? That would be a problem, yes.

    That sample also contains a VB4-era, non-callback, solution to the same problem,
    fwiw.

    But, bottom line, if you already know the *full* caption, just call FindWindow --
    that's all you need.

    Later... Karl




  8. #8
    Phil Weber Guest

    Re: Closing a window when you only have the Title

    > What I need is the API that will take the Title, and
    > return the ClassName so I can get the hWnd to close
    > the window.


    Brent: You don't need the classname; you can get the hWnd directly from the
    title. Download the FindPart.zip sample from Karl Peterson's Web site
    ( http://www.mvps.org/vb/samples.htm ) to see how to do so.
    ---
    Phil Weber



  9. #9
    Brent Guest

    Re: Closing a window when you only have the Title


    "Phil Weber" <pweber@teleport.com> wrote:
    > > What I need is the API that will take the Title, and
    > > return the ClassName so I can get the hWnd to close
    > > the window.

    >
    >Brent: You don't need the classname; you can get the hWnd
    >directly from the title. Download the FindPart.zip sample from
    >Karl Peterson's Web site ( http://www.mvps.org/vb/samples.htm )
    >to see how to do so.
    >---
    >Phil Weber
    >


    Phil,
    Thanks for the tip. I tried it, but it doesn't seem to work (I'm getting
    an error "Invalid use of AddressOf" or something like that). I actually
    decided to disable the Main MDI form behind the dialog (this achieves what
    I'm looking for - simulates the dialog being Modal). I've also done this
    for simplicity's sake since I won't be on this project much longer (ease
    of maintainability). I hate doing it this way, but I'm not sure how intelligent
    the next developer will be either... I am definitely going to get this to
    work in a smaller app so I know what the problem is...

    If you have and hints as to where I should look I would appreciate it.

    Thanks again,
    Brent

  10. #10
    Joel Ryan Guest

    Re: Closing a window when you only have the Title

    Here's the code I use to find a window. To close it, send it the message WM_CLOSE

    Something like this:

    Private Const WM_CLOSE = &H10
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
    lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd
    As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Sub Command1_Click()
    lWnd = FindWindow(vbNullString, txtCaption.Text)
    Call SendMessage(lWnd, WM_CLOSE, 0, ByVal 0)
    End Sub

    -- Joel



  11. #11
    Paul Little Guest

    Re: Closing a window when you only have the Title

    Brent, I know you've already received other answers (and found your own
    workaround), but I thought I'd add some food-for-thought. If your app is
    creating the Crystal Report window (i.e. Report.Preview or Report.Action =
    1) then there are cleaner ways to close the window (or just wait for the
    user to close the window before proceeding in your app (i.e. a Modal
    preview)). How to do this depends on the mechanism you are using to display
    the Crystal Report (OCX, Automation components, RDC, CRPE API, etc.). If
    you'd like specifics, just post back the method you are using to display the
    report (you can just paste the code that is used to display it if that is
    easier).

    -Paul
    --
    Paul Little, Senior Programmer/Analyst
    SVi Retail Systems
    San Diego, CA

    "Brent" <Brent.Leister@Robot-rx.com> wrote in message
    news:390d93ed@news.devx.com...
    >
    > Hi all,
    > Typically I would not post this question, but I don't have my API Bible

    today...
    >
    > All I have is the Title of window (its a dialog from a Crystal Report).
    >
    > When my app shutsdown I need to check if this window is displayed and if
    > it is close it. Problem is I don't have the ClassName of the Window, I

    only
    > have the Title.
    >
    > So what I need is the API that will take the Title, and return the

    ClassName
    > so I can get the hWnd to close the window.
    >
    > Any help would be greatly appreciated.
    >
    > Thanks,
    > Brent




  12. #12
    Brent Guest

    Re: Closing a window when you only have the Title


    Joel,
    Thanks the code worked perfectly!

    Joel Ryan <jryan@vsi-hq.com> wrote:
    >Here's the code I use to find a window. To close it, send it the message

    WM_CLOSE
    >
    >Something like this:
    >
    >Private Const WM_CLOSE = &H10
    >Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
    >lpClassName As String, ByVal lpWindowName As String) As Long
    >Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal

    hwnd
    >As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    >
    >Sub Command1_Click()
    > lWnd = FindWindow(vbNullString, txtCaption.Text)
    > Call SendMessage(lWnd, WM_CLOSE, 0, ByVal 0)
    >End Sub
    >
    >-- Joel
    >
    >



  13. #13
    Karl E. Peterson Guest

    Re: Closing a window when you only have the Title

    Hi Brent --

    > Thanks for the tip. I tried it, but it doesn't seem to work (I'm getting
    > an error "Invalid use of AddressOf" or something like that).


    You can only use AddressOf to point to routines within a BAS module. Did you suck
    the code directly into one of your forms? That would be a problem, yes.

    That sample also contains a VB4-era, non-callback, solution to the same problem,
    fwiw.

    But, bottom line, if you already know the *full* caption, just call FindWindow --
    that's all you need.

    Later... Karl




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center