How to maximize a window after minimizing it - c++

How does one maximize a window after minimizing it? I use nCmdShow = 3 to maximize it and nCmdShow = 6 to minimise it using ShowWindow(hwnd, nCmdShow). However, once I minimize the window, I cannot restore or maximize it.
This is because I cannot store the handler for the window that is minimized so that the same window can be maximized on certain condition? How do I achieve the same thing?

You want to use SW_RESTORE to redisplay your minimized window, to quote MSDN:
Activates and displays the window. If the window is minimized or
maximized, the system restores it to its original size and position.
An application should specify this flag when restoring a minimized
window.

You need to call ShowWindow(Hwnd, SW_SHOWMAXIMIZED);
If you really "cannot store the handle" (is that what you meant by handler?) then you could consider using FindWindow. Sounds to me like you need to just store the window handle and then it'll be OK!

You can minimise the window by Alt+Enter
and maximise the window by the same keycombination.

Related

Question about set window full screen with SetWindowPos

In my app, I want to keep my window full screen. So I added resizing window function when received WM_DISPLAYCHANGE event.
In the resizing window function, I use EnumDisplayMonitors to get current size of monitor, the size is correct. Then I use SetWindowPos function to set my window size equal to the monitor. But after SetWindowPos called, I found my window is still a little smaller than the monitor size some time. It seems that the desktop is not ready enough. I also set SWP_NOSENDCHANGING flag in the SetWindowPos function but still not work.
Is there any way to solve this problem?
The usual approach to make a window full screen is not by reacting dynamically to WM_DISPLAYCHANGE or query the display dimensions. The canonical way to make a window full screen is to set its style to WS_MAXIMIZE | WS_POPUP | WS_VISIBLE its extended style to WS_EX_TOPMOST using SetWindowLong, followed by a call to SetWindowPos with flag SWP_FRAMECHANGED, then maximize it with *ShowWindowwith flagsSW_SHOWMAXIMIZED`.

How to check if my current window is the top most visible window

I have a window which is set to AlwaysOnTop using the WS_EX_TOPMOST flag. Now, it is possible that some other application might also have a window which has WS_EX_TOPMOST set and override the topmost flag for my window.
How should I check if my window is indeed the window that is the top most window and nothing is being painted over it (the nothing is being painted over my window is the important part). If something is painting over my window, I want to hide my window and show it again when I can make it the top most window (but that's probably the second step)
Call GetWindow passing your topmost window's handle and the GW_HWNDFIRST flag. The window returned will be the topmost window that is highest in the Z-order. You can then use the GW_HWNDNEXT flag to walk through the topmost windows in order of decreasing Z-order until you find yours. If any of the windows overlap your window, then your window is underneath.
The old standard way was to call WindowFromPoint for a point on your supposedly visible window and compare the returned handle against your own window handle. There is a better way using the clipping system. I discuss this here.

Create a window using the WS_EX_NOACTIVATE flag ,but it can't be dragged until I release the mouse

I have created a window using the WS_EX_NOACTIVATE flag and it works great as far as not taking focus when you click the window. However when I drag the window or try to resize it, it doesn't redraw the window as your are moving and resizing it, only at the very end once you release the mouse button. Is there a way around this? I would like to see the window as I am resizing it. I have searched many documents, but I still don't find the resolution...
If you need a window that doesn't take focus when clicked, but can still be interacted with, you need to handle the WM_MOUSEACTIVATE, and return MA_NOACTIVATE.
Additional information and fully working sample code has been published by Raymond Chen. See How can I have a window that rejects activation but still receives pointer input?
It's a Windows bug. You need to call SetWindowPos(hwnd, 0, x, y, width, height, 0) on WM_MOVING. The coordinates to set are given to you in lParam which is a RECT*.
Note that doing this will activate the owned window and deactivate the owner, which is not what you want (and SWP_NOACTIVATE has no effect either).
To avoid that, you need to set WS_CHILD on the owned window. But set it after you created the window, via SetWindowLong(), otherwise your owned window will get clipped, like any child window.
And, as you probably already figured out, this only works for windows with WS_EX_TOOLWINDOW ex style. I wasn't able to make the owned window stay deactivated with any other style combination that doesn't include WS_EX_TOOLWINDOW.
That's winapi for you :(

How to prevent window resizing temporarily?

I have a window which can be resized, but there are some situations when resizing is not possible because of the application state. Is there a way to prevent resizing the window temporarily?
I want to disable resizing by all means available to the users, which include window menu, dragging edges by mouse, user initiated window tiling performed by OS - and perhaps some other I am not aware of?
To retain the look of the window border and still prevent re-size (and cursor change), catch WM_NCHITTEST, pass it to DefWindowProc, if the returned code is one of the size constants, change the real return to something else, HTCLIENT for example
One way is to use GetWindowLong() with GWL_STYLE flag to get the window style and
reset/remove any styles you need, ie the WS_THICKFRAME style so that the window can't be resized.
You apply the new style with SetWindowLong.
Another possibility is to handle the WM_GETMINMAXINFO message and set the MINMAXINFO struct so that both min and max size of the window is the current size. Then the user can't resize the window either.
Following code in the window procedure seems to handle the case of user dragging the window edge/corner:
case WM_SIZING:
RECT &rc = *(LPRECT) lParam;
RECT windowRect;
GetWindowRect(hwnd, &windowRect);
rc = windowRect;
return 0;
I did not find anything yet to prevent the system from resizing the window when tiling/cascading windows. I hoped following might do the trick, but it seems it does not:
case WM_SIZE:
return TRUE;
I guess I can find similar measure for other cases, but at least I would need to know the exhaustive list of messages which can result in a window changing its size.
Also, while this really prevents the window from resizing, I would rather prevent the user from even initiating the resize, than apparently letting him to resize and then refusing to do so.

Win32, C++: Creating a popup window without stealing focus

I am creating a program that displays a popup at certain times (just like some chat clients for example) on which the user can click. However, I do not want to take away the focus from the current application.
The way I'm doing it now is by using a HWND with WS_POPUPWINDOW and minimizing and then restoring the window. However, this steals the focus from the current application. Setting foreground or hiding and showing a window did not make it appear on the foreground. I would like to be able to keep using a HWND so I can use other elements in this window, but I have no idea how to give it foreground without stealing focus.
I use win32 and c++.
To show without activating:
ShowWindow(hwnd, SW_SHOWNOACTIVATE);
To raise without activating:
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
Unfortunately this is not working for me. The window is created with CreateWindowExA and is showed using ShowWindow(hwnd, SW_SHOWNOACTIVATE) however the keyboard focus is still stolen from the window which has the focus at the moment of the creation. (The window is created with layered and trasparent attributes by using SetWindowLong() and SetLayeredWindowAttributes() ).
PS: The window which has the focus is not parent of the new created window.
Solved: It worked when I removed the SetForegroundWindow call. This function cause the window passed as parameter to be activated.