1

When I do a get GetWindowPlacement, the WINDOWPLACEMENT::showCmd seems to be always 1, which is SW_SHOWNORMAL.

Does anyone know why is this so and if it is updated? Does anyone know if this variable is maintained by the application itself or by the operating system?

I am running this on Windows 7.


I am using this to achieve the same purpose as mentioned in this thread: I am trying to undo hidden windows that were previously shown without storing the hidden windows in memory (hide/show will be called in different run sessions) or on disk.

void hide(const unsigned int pid){
  std::list<HWND> windowList = getWindowbyPID(pid);
  for(std::list<HWND>::iterator it = windowList.begin(); it != windowList.end(); it++){
    if(IsWindowVisible(*it)){ std::cout << "Hid WIN#" << *it << std::endl; ShowWindow(*it,SW_HIDE); }
  }
}

void show(const unsigned int pid){
  std::list<HWND> windowList = getWindowbyPID(pid);
  for(std::list<HWND>::iterator it = windowList.begin(); it != windowList.end(); it++){
    //if(IsWindowVisible(*it)){ ShowWindow(*it,SW_SHOW); }
    WINDOWPLACEMENT wp;
    wp.length = sizeof(wp);
    wp.showCmd = 0; // Just to clear showCmd before reading.
    std::cout << *it << std::endl;
    std::cout << "BEFORE: " << wp.showCmd << std::endl;
    GetWindowPlacement(*it,&wp);
    std::cout << "AFTER: " << wp.showCmd << std::endl;
  }
}

Output of one example that I did (pid of notepad.exe) after hiding hwnd#00060CD0:

003D0642
BEFORE: 0
AFTER: 1
000B0682
BEFORE: 0
AFTER: 1
00060CD0
BEFORE: 0
AFTER: 1

I am trying to use GetWindowPlacement to differentiate the windows that were always hidden and the windows that were previously shown. It never seems to be 0 even for windows that were always hidden.

Community
  • 1
  • 1
swtdrgn
  • 1,154
  • 4
  • 17
  • 49

1 Answers1

5

There are only three possible values of the showCmd after calling GetWindowPlacement.

From the MSDN documentation on GetWindowPlacement (emphasis mine):

The flags member of WINDOWPLACEMENT retrieved by this function is always zero. If the window identified by the hWnd parameter is maximized, the showCmd member is SW_SHOWMAXIMIZED. If the window is minimized, showCmd is SW_SHOWMINIMIZED. Otherwise, it is SW_SHOWNORMAL.

Therefore, it appears that the window you're asking for placement info on is in a state other than maximized or minimized when you're calling GetWindowPlacement.

I'd suspect what you're actually looking for is IsWindowVisible.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Yes, you are right. I missed that. `IsWindowVisible` will not do because I do not want to show a window that was always hidden. – swtdrgn Jun 05 '13 at 17:47
  • 2
    How are you supposed to determine if a window "was always hidden". The only way to create a non-visible window is to not include `WS_VISIBLE` in the flags passed to `CreateWindow`, and any subsequent calls to `ShowWindow` or `SetWindowPos` that change visibility will toggle this flag on or off; it doesn't retain the original state from when the window was created. – Ken White Jun 05 '13 at 17:55
  • I was afraid to hear that, but thank you for sharing your expertise with me. – swtdrgn Jun 05 '13 at 18:04
  • @Ken White, not "only": include WS_VISIBLE with HWND_MESSAGE :) – kero Jun 06 '13 at 00:29
  • @kero: I'm not sure what you're saying. The only way to *create a window* is to use `CreateWindow`, and to create it hidden you just don't set the `WS_VISIBLE` style bit. Anything that affects visibility after that changes that style bit from its original state so that you have no idea what that original state was when it was created, so you can't tell that the window was always hidden and never shown. What part of that are you not in agreement with? – Ken White Jun 06 '13 at 00:47
  • @Ken: an exact quote - "The only way to create a non-visible window is to not include WS_VISIBLE in the flags passed to CreateWindow", and I just said that it does not take into account all, didn't mean to offend you :) – kero Jun 07 '13 at 11:18