Is it necessary to invoke the "ShowWindow" and "UpdateWindow" functions?

2

Recently I did an introduction to the Windows API and I'm already in the part of creating windows. In one of the first applications I created I had use of ShowWindow and UpdateWindow after creating a window, but even without them the window continued to show. I even created more windows to see if your invocation would be needed and these additional windows would also pop up.

So here's my question, is it really necessary to invoke them? Will it have any impact not to do? Or is it just a prevention measure?

Q. I put the C ++ tag because it is an API also used in this language

    
asked by anonymous 27.06.2015 / 00:10

2 answers

3

I do not know how you wrote the application, but we can say that some functions already call these functions "automatically" what would actually make this redundant.

ShowWindow

But note that the use of ShowWindow is not only to display the window when starting a program, it has as "utility" to change the state of the window that is referring, for example, this would force to minimize a window already opened:

 ShowWindow(hwnd, SW_FORCEMINIMIZE);
  • SW_FORCEMINIMIZE minimizes a window even though the Thread owner is not responding

  • SW_HIDE - Hides the window and activates another window

  • SW_MAXIMIZE - minimizes the window

  • SW_MINIMIZE - minimizes the window and activates the next one that is at the highest level by the order Z

  • SW_RESTORE - activates and displays the window. If minimized or maximized it "restores" to the original position and sizes.

  • SW_SHOW - active and shows the window at the original position and size.

  • SW_SHOWDEFAULT - Sets the state of the window based on the SW_ specified in the STARTUPINFO structure passed to CreateProcess
  • SW_SHOWMAXIMIZED - Activates and displays the maximized window

  • SW_SHOWMINIMIZED - Activates and displays the minimized window

  • SW_SHOWMINNOACTIVE - shows the minimized window, similar to SW_SHOWMINIMIZED except that the window is not activated

  • SW_SHOWNA - Shows the window at the current position and size, it is similar to SW_SHOW except that the window is not activated

  • SW_SHOWNOACTIVATE - Displays the window at the most recent position and size, similar to SW_SHOWNORMAL , except it does not activate the window.
  • SW_SHOWNORMAL - Activates and displays the window. If the window is minimized or maximized this restores to the original size and position. An application should specify this "flag" when displaying the window for the first time.

UpdateWindow

UpdateWindow refreshes the client area of the window by sending a WM_PAINT message to the window if the update region is not empty. This may be useful for animations perhaps, so you will use it when necessary to refresh the rendering.

I do not have much knowledge, but to what seems to me these are the goals, if you have any more doubts maybe new answers will appear, I hope that for now this will help.

Documentation:

27.06.2015 / 01:36
3

Just to complement what Guilherme said, if you realize, most of the time at the moment of calling the ShowWindow inside WinMain you send the argument nCmdShow, this argument is the way Windows wants your window to be displayed ( since you can change the view of shortcuts), so basically when calling ShowWindow sending nCmdShow, you're not exactly asking to show your window, but rather to show it in the way that Windows wants.

But the window is not shown on the screen by itself, it is only shown on the screen automatically if you create the window by sending the WS_VISIBLE flag, see this example:

CreateWindowW(wc.lpszClassName, L"Janela", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 150, NULL, NULL, hInstance, NULL);  

In the above case it is shown automatically, because you have sent WS_VISIBLE in the function, ie even without calling the ShowWindow it will be shown.

Now look at this case:

HWND hwnd = CreateWindowW(wc.lpszClassName, L"Janela", WS_OVERLAPPEDWINDOW, 100, 100, 250, 150, NULL, NULL, hInstance, NULL);    
ShowWindow(hwnd, SW_SHOW);

In this case the window will not appear without the ShowWindow call, because the WS_VISIBLE flag was not sent.

    
27.06.2015 / 02:42