CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2005
    Posts
    336

    SendMessage/PostMessage

    Hi
    This code closes the window with PostMessage.
    But if i write SendMesssage instead of Post message it doesn't close the window.I don't understand why it is so.
    Code:
       HWND h=::FindWindow(NULL,L"Tutorial: A Simple Window - Mozilla Firefox");
       PostMessage(h,WM_QUIT,0,0);
    What are the differences so postmessage can close the window but postmessage can not.
    Thanks

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: SendMessage/PostMessage

    Never post/send a WM_QUIT. Always post/send a WM_CLOSE message instead.
    The reason it doesn't work is probably answered here.. which is , WM_QUIT message has to be processed by GetMessage, however, SendMessage bypasses this.

    Anyways, refrain from send/posting WM_QUIT. WM_CLOSE is the recommended approach to close a window. If not, you might bypass other stuff the application would typically do when closing a window.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SendMessage/PostMessage

    What are the differences so postmessage can close the window but postmessage can not
    Quote Originally Posted by MSDN
    The SendMessage function sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.
    Quote Originally Posted by MSDN
    The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.
    Now, you should undestand better what Kiran said (as specified in MSDN)
    Quote Originally Posted by MSDN
    The WM_QUIT message is not associated with a window and therefore will never be received through a window's window procedure. It is retrieved only by the GetMessage or PeekMessage functions.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Sep 2005
    Posts
    336

    Re: SendMessage/PostMessage

    ok.
    Thanks friends for help...

  5. #5
    Join Date
    Oct 2006
    Posts
    19

    Re: SendMessage/PostMessage

    Hi All

    I want to close another programm from my programm:
    Code:
       SendMessage(hMw,WM_CLOSE,0,0);
    And it works ...

    But if another programm is Minimized, it's not closed. Only after the second start of my program it's closed.

    Why...?
    I don't understand.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SendMessage/PostMessage

    I don't understand.
    I don't undestand this either:
    after the second start of my program it's closed.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    164

    Re: SendMessage/PostMessage

    I think he means... if the program he wants to close is minimized, running his program once will not close it. But, running his program a second time will close it.

  8. #8
    Join Date
    Oct 2006
    Posts
    19

    Re: SendMessage/PostMessage

    Yes I meant it.

    I badly know English. Sorry...

  9. #9
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: SendMessage/PostMessage

    Why SendMessage ? Why not PostMessage ? ( SendMessage will tie up the sender if , for some reason, the target process is hung ). Instead, use PostMessage, or SendMessageTimeout.

    Also, how are you obtaining the hWnd ? Are you sure you are getting the right hWnd ?

  10. #10
    Join Date
    Oct 2006
    Posts
    19

    Re: SendMessage/PostMessage

    PostMessage does not work too.

    SendMessageTimeout did't try. I do not know parameter - fuFlags.

    Yes Window handle is right. First time it's working.

  11. #11
    Join Date
    Oct 2006
    Posts
    19

    Re: SendMessage/PostMessage

    SendMessageTimeout does not work too.

    Maybe i send wrong parameters !?

    Code:
      hMw = FindWindowEx (NULL,NULL,"WindowsForms10.Window.8.app17",NULL);
      SendMessageTimeout(hMw,WM_CLOSE,0,0,0,200,0);

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

Featured