Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

What is the relationship of closewindow() and WM_CLOSE?

Home/ Questions/Q 359
Next
Answered
What is the relationship of closewindow() and WM_CLOSE?
braevanov
braevanov Teacher

I am a new programmer working on a Windows application using C++. I’ve been reading through the Microsoft documentation and I stumbled upon some conflicting information regarding the `CloseWindow` function and the `WM_CLOSE` message. I’m confused about how these two are related and when I should use one over the other.
From what I understand, `CloseWindow` is a function that can be used to minimize, maximize, or close a window. On the other hand, `WM_CLOSE` is a message that can be sent to a window to instruct it to close. I’m not sure how these two functions/messages are connected or whether they’re interchangeable.
Here’s an example of the code I’m working with:
“`
HWND hWnd = FindWindow(NULL, “My Window Title”);
if (hWnd != NULL) {
CloseWindow(hWnd);
}
“`
In this code snippet, I’m using the `FindWindow` function to locate a specific window by its title and then attempting to close it using `CloseWindow`. However, I’m not sure if I should be sending a `WM_CLOSE` message instead or if `CloseWindow` is the correct function to be using at all. Can someone explain the difference between these two and when I should use each one?

closewindowmessage-handlingwinapiwindowswm-close
  • 632
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

4 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    infinity.e.i.m Teacher
    2021-12-04T16:46:20+00:00Added an answer about 1 year ago

    Hi there!
    It seems that you’re having trouble with the relationship between `CloseWindow` and `WM_CLOSE`. I understand that this can be a bit tricky to wrap your head around, so let me see if I can clear things up for you.
    In short, `CloseWindow` is a function that sends a `WM_CLOSE` message to the specified window, asking it to close. The difference between using `CloseWindow` and sending a `WM_CLOSE` message directly is that `CloseWindow` is safer to use when you don’t have direct access to the window handle, for example, if you’re working with a higher-level programming language that doesn’t offer direct access to the native window handle.
    Now, let’s dive a bit deeper into the specifics of `CloseWindow` and `WM_CLOSE`. `CloseWindow` is a user-defined function that sends a `WM_CLOSE` message to the specified window. This message asks the window to close, just like if the user clicks the close button on the window. If the window is a child window, the `WM_CLOSE` message is sent to the parent window instead.
    On the other hand, `WM_CLOSE` is a message that’s sent to a window to request that it close. When a window receives a `WM_CLOSE` message, it’s up to the window how to handle the message. Typically, the window will display a message box asking the user if they want to save any unsaved changes before closing.
    So, in essence, `CloseWindow` is just a safer way to send a `WM_CLOSE` message. If you have direct access to the window handle, it’s perfectly fine to send the `WM_CLOSE` message directly. But if you don’t have direct access, or if you’re working with a higher-level programming language, using `CloseWindow` can save you some headache.
    I hope this clears things up for you! If you have any more questions, feel free to ask.

    • 52
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. nicostradamus
    2021-12-22T12:01:34+00:00Added an answer about 1 year ago

    `CloseWindow` API function, which is called in Windows Form applications is responsible for closing a specified window. On the other hand, WM_CLOSE is a message sent to any window whose close button is clicked or whose title bar close button is clicked. So, `CloseWindow` bounds to the button and response to the click, but `WM_CLOSE` is independently bound to the close button.

    Whenever a close button is clicked, WM_CLOSE message is sent to the window on which the close button is clicked. When it receives the WM_CLOSE message, a window usually responds by calling `DestroyWindow`. In some cases, however, the lowest-level message loop for a thread can directly call `DestroyWindow`.

    `CloseWindow` and `WM_CLOSE`, both perform the same function ie; closing the window. But while using them, there is a difference in their behavior. You can use both of them in your application, but it’s recommended to use `WM_CLOSE` message while working with a window handler.

    • 38
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. viktorija_35
    2021-12-31T22:56:23+00:00Added an answer about 1 year ago

    The `WM_CLOSE` message is used to ask a window to close itself. It sends a message to the window’s message queue, giving the window the opportunity to clean up resources or prompt the user to save changes before closing.

    In the case of the `CloseWindow()` function, this function sends the `WM_SYSCOMMAND` message with the `SC_CLOSE` parameter. This message is handled differently by different types of windows but in general, it tells the window to close.

    It’s important to note that `CloseWindow()` does not immediately close the window. It simply sends a message to the window’s message queue, and it’s up to the window to decide how to handle it. For example, if the window has unsaved changes, it might ask the user if they want to save their work before closing.

    • 29
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  4. miamushi09 Teacher
    2021-12-22T21:47:26+00:00Added an answer about 1 year ago

    When we send a `WM_CLOSE` message, Windows dispatches it to the window specified by the `hwnd` parameter of the `SendMessage()` function. This can initiate the window’s close procedure, which is usually implemented by the `DestroyWindow()` function. The `CloseWindow()` function, on the other hand, removes the maximized or minimized style from a window instead of closing it.
    It is important to keep in mind that these two functions have completely different purposes, and using them interchangeably could lead to unintended consequences. If you want to close a window, you should always use `SendMessage()` with the `WM_CLOSE` message, and if you want to minimize or maximize a window, use `ShowWindow()`.

    In short, `CloseWindow()` only affects the size and position of a window, while `WM_CLOSE` message is responsible for initiating the close procedure of a window which includes removing the window from the screen and freeing up the resources it was using.

    • 23
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.