Results 1 to 6 of 6

Thread: Closing a window via it's hwnd

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Closing a window via it's hwnd

    um how can i close a window by it's hwnd.. the only way i no is 2 use:

    SendMessage TempWinHwnd, WM_CLOSE....

    but this won't close explorer and folder windows..

    Thanks in advance
    Kris

  2. #2
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352
    This code worked fine for me:

    VB Code:
    1. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    2. Const WM_CLOSE = &H10
    3.  
    4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5.  
    6.  
    7. Private Sub Command1_Click()
    8. 'Close Explorer
    9. PostMessage FindWindow("ExploreWClass", vbNullString), WM_CLOSE, 0, 0
    10.  
    11. End Sub
    12.  
    13.  
    14. Private Sub Command2_Click()
    15. 'Close Folder Browsing
    16. PostMessage FindWindow("CabinetWClass", vbNullString), WM_CLOSE, 0, 0
    17.  
    18. End Sub

    Joe

  3. #3
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    But make sure you specify a window title too, otherwise windows will close the first window in the list -- which may not the be the one you wanted to.

  4. #4
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    I think it's

    DestroyWindow Hwnd

    I don't know it's declaration though, not sure what it does, but it seems to work fine.

  5. #5
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    DestroyWindow does not work on an external thread, however I believe WM_DESTROY will.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352
    DestroyWindow is a much harsher way of shutting a program down; it sends the WM_DESTROY and WM_NCDESTROY messages which are almost equivalent to "end tasking" an application. This means time is not allocated for cleanup procedures and may end up in leaks. I would still advise using the code I posted(and as megatron said specify a title; I did not merely to demonstrate the simplest code that works).

    Joe

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