check current application clicked in taskbar? (C++, Windows API) - c++

I've spent many hours on google and haven't found any relevent results on this particular subject.
I have an application I am wanting to be minimized when the user clicks on it in the taskbar (if it's not already minimized). The problem seems to be related to the fact the window is borderless. When I set it to have a border, it minimizes just fine when clicking it in the taskbar, without any code intervention. But I need the window borderless because I'm making a "custom border" using the client area.
tl;dr how do I check if the current application is being clicked in the taskbar?
Many thanks!
Samuel

There is no need to tinker with the taskbar.
Just make sure you have the WS_MINIMIZEBOX|WS_MAXIMIZEBOX styles set for your window. Otherwise your window won't handle WM_SYSCOMMAND with a wParam of SC_MINIMIZE and SC_RESTORE.
Some resource editors like the one in Visual Studio make it impossible to set WS_MINIMIZEBOX|WS_MAXIMIZEBOX when you remove the standard window border. You may programmatically add the styles back like this:
DWORD style = GetWindowLong( hwnd, GWL_STYLE );
SetWindowLong( hwnd, GWL_STYLE, style | WS_MINIMIZEBOX | WS_MAXIMIZEBOX );

Related

Maximized WS_POPUP window goes in front of the taskbar

I'm creating a window in C++ with that code:
HWnd = CreateWindow(wc.lpszClassName,
"myapp",
WS_POPUP |WS_VISIBLE,
10, 10, 1000, 800, 0, 0, hInst, NULL);
It appears as I want but when I maximize it with like this:
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
then it's like a fullscreen window so that the taskbar is hidden.
I think it's because it is a POPUP window but this is like I want it to appear.
Do I need to create my own maximize function or is there a parameter to avoid that ?
Thanks
You can add WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX style.
If you want your window to be without caption then you will need to adjust window size manually to fit into desktop work area that you can query using SystemParametersInfo specifying SPI_GETWORKAREA flag.
This is a feature where the Taskbar will get out of your full-screen application:
If you want to create a fullscreen window that covers the taskbar, just create a fullscreen window and the taskbar will automatically get out of the way.
I’ve seen people hunt for the taskbar window and then do a ShowWindow(hwndTaskbar, SW_HIDE) on it. This is nuts for many reasons.
Don’t do any of this messing with the taskbar. Just create your fullscreen window and let the taskbar do its thing automatically.
Since this is Stackoverflow, a combination of Wikipedia and Reddit, i want this relevant information saved for the next guy asking this question.

Constraining draggable child windows within parent window?

Please take a look at this screenshot:
As you can see, the "Executable modules" and "Threads" child windows are free to roam about in the sandbox-like "Themida" parent window, and if they get dragged past the edge the overflow simply gets hidden. How can I create this effect?
That is a Multiple Document Interface (MDI) application. The containing window, with the dark grey background is the MDI client window, and the windows inside are the MDI child windows.
The use of MDI has been discouraged by Microsoft for many years so you may wish to think twice about using it in a new application.
Simply set the window style to WS_CHILD, and the window will be confined in the parent client rectangle.
You can do this during window creation, or after using SetWindowLongPtr() and GetWindowLongPtr():
SetWindowLongPtr(hwnd, GWL_STYLE, WS_CHILD | GetWindowLongPtr(hwnd, GWL_STYLE));
P.S. You don't need to create an MDI application to have this behavior.

WS_EX_TRANSPARENT and stylus events

I'm setting WS_EX_TRANSPARENT flag in window with the following code:
SetWindowLong( hwnd, GWL_EXSTYLE,WS_EX_TRANSPARENT | WS_EX_COMPOSITED | WS_EX_LAYERED );
This flag make that mouse events go to windows system instead of my app.
When I set this, I can't get Stylus events in hwnd window. Any way to solve that or that's not possible?
The behavior for the WS_EX_TRANSPARENT extended window style for Layered Windows is documented:
[I]f the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to other windows underneath the layered window.
Note: The rules for input handling are usually the same, for all pointing devices. Whenever you read "mouse events" you can expand that to "input events from a pointing device".
One workaround is given in the documentation for WS_EX_TRANSPARENT:
To achieve transparency without these restrictions, use the SetWindowRgn function.

Creating window frame without maximize button and without resizing options?

I need to create a window that acts like "normal" one, but without maximize button, and sizing border.
Searching through Internet, and studying MSDN, I have learned that natively achieving both is impossible.
There is no window style that does both ( I can disable maximize button, but that is not my aim; as for removing resizing options, I have found suitable window styles in the documentation ).
The closest description would be the dialogbox frame behavior ( no sizing border ), but with extra minimize button.
QUESTION:
Is there a way to achieve my goal some other way?
If yes, can you please provide links to tutorials or code examples? This would be the first time for me to do such a thing and could use all the help I could get?
An important note: I have found this example while searching for a solution, but it will not help me because I target Windows XP onwards.
Creating a window as below will give you a non-sizeable window with a title bar, a minimize button and an exit button.
dwStyle = WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX;
hWnd = CreateWindow(szAppName, szTitle, dwStyle,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL);
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms632679%28v=vs.85%29.aspx
and http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600%28v=vs.85%29.aspx
I suppose you are creating the window using CreateWindowEx. Then, if you omit both WS_MAXIMIZEBOX and WS_MINIMIZEBOX flags (the dwStyle parameter), the window will have only the close button (no minimize/maximize) buttons. If you ommit just WS_MAXIMIZEBOX, Windows draw the maximize box disabled to keep the graphics layout consistent for all windows. There is no way to change this behavior, and it can change in different versions of Windows (Win3.1, for instance, didn't draw the maximize button at all when the flags were set as mentioned.)
Resizable border is disabled by setting other frame than WS_THICKFRAME (ie. WS_BORDER or WS_EX_DLGMODALFRAME in the dwExStyle parameter).
You can also control the user sizing/moving of your window by intercepting messages WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED, WM_ENTERSIZEMOVE, WM_EXITSIZEMOVE, WM_SIZING and WM_MOVING.
In theory, you can also completly change the appearance of the non-client area of the window, but it's hardly worth the effort, and it's questionable whether it's a good idea to fight with the default graphic layout of the operating system when all the developers and user are used to it and content with it. (In other words: if you don't want your window to be maximized, just omit the WS_MAXIMIZEBOX flag and leave it on the operating system how to realize this particular decision.)
I'm pretty sure it is documented on MSDN that the window style you want to OMIT is WS_THICKFRAME, since the Window Styles page says that a thick frame is a sizing frame.

C++ Console Application, hiding the title bar

I have a Windows console application written in C++ and want to hide/remove the complete title bar of the console window, including the close, min/max controls etc. I searched a lot but didn't found anything useful yet.
I inquire the console HWND with GetConsoleWindow and tried to change the console window style with SetWindowLong by removing the WS_CAPTION flag, but this seems to have no effect at all:
HWND hwnd = GetConsoleWindow();
LONG style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_BORDER|WS_CAPTION|WS_THICKFRAME);
SetWindowLong(hwnd, GWL_STYLE, style);
SetWindowPos( hwnd, NULL, 0,0,0,0,
SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE
|SWP_FRAMECHANGED );
I also tried GetSystemMenu/RemoveMenu but this seems only to disable controls like the close button.
You can't. Generally the hWnd of a console window is not guaranteed to be suitable for all window handle operations as, for example, documented here.
You could try a complex solution involving hiding the console window (this is possible), and then setup a window (without the controls) that forwards appropriate events back and forth from the real console window. In particular GDI events to draw the console window contents in your fake console window, and interact with the scrollbar (which in turn adjusts the console...).
This solution is pretty far out, and quite technical.
You can use SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP); , which will remove the caption/titlebar and the borders.
Warning: This does introduce a few glitches that I don't know how to fix (I guess they're cached borders?), but at least it does produce the effect that you want.
I think I would write/use two programs. One console program doing the work and a second program being a controllable console window running the first one. Most probably there are already existing console programs out there and some can be started without title bar? Or find an open source one and modify it.