patterncppMinor
LifeSaver: Hides and manipulates windows
Viewed 0 times
manipulateshideslifesaverwindowsand
Problem
[ This app is now on github !! ]
This is a small program I wrote to hide/show different windows on my Windows PC.
This is named
Its structure is like a stack. You activate a window, and then press LCtrl+F7. The window will be captured and pushed into the stack. I call these the slots. There are 10 empty slots in the beginning. Slots are numbered from 0 to 9. So every captured window goes into the next available slot. When captured, the slot number is briefly flashed on the window. Popping from the stack is currently not allowed. However, clearing the whole stack is possible. Just press LCtrl+F8 to clear the stack, and start over again.
To flash the slot number, I used GDI (not GDI+). Painting the digit is easy, but erasing is impossible. I did what I could do to make the window repaint itself and hence erase the digit. I hope it is okay-ish.
To interact with the program, the LCtrl button must be pressed. Then there are variety of controls to manipulate the windows. They can be toggled between hidden/shown individually by pressing LCtrl+N, where N is the slot number the window was captured into and was flashed briefly on the screen. N must be pressed from the numeric pad.
By pressing LCtrl+LWin, all the visible captured windows can be toggled to hidden simultaneously. This is referred to as "Special Hide" in the source code. It is special because when LCtrl+LWin is pressed again, it toggles the windows back to visible, but only specifically the ones which were hidden with "Special Hide". This is referred to as, appropriately enough, "Special Show" in the source code.
If in emergency, by pressing LCtrl+RCtrl keys together, you can to kill all captured windows (whether currently visible or hidden) and followed by this program killing itself. To kill the windows, I send them
This is a small program I wrote to hide/show different windows on my Windows PC.
This is named
LifeSaver because it did save my life several times, whenever someone stormed into my room unexpectedly. It does have some cool uses and some naughty uses, for all people.Its structure is like a stack. You activate a window, and then press LCtrl+F7. The window will be captured and pushed into the stack. I call these the slots. There are 10 empty slots in the beginning. Slots are numbered from 0 to 9. So every captured window goes into the next available slot. When captured, the slot number is briefly flashed on the window. Popping from the stack is currently not allowed. However, clearing the whole stack is possible. Just press LCtrl+F8 to clear the stack, and start over again.
To flash the slot number, I used GDI (not GDI+). Painting the digit is easy, but erasing is impossible. I did what I could do to make the window repaint itself and hence erase the digit. I hope it is okay-ish.
To interact with the program, the LCtrl button must be pressed. Then there are variety of controls to manipulate the windows. They can be toggled between hidden/shown individually by pressing LCtrl+N, where N is the slot number the window was captured into and was flashed briefly on the screen. N must be pressed from the numeric pad.
By pressing LCtrl+LWin, all the visible captured windows can be toggled to hidden simultaneously. This is referred to as "Special Hide" in the source code. It is special because when LCtrl+LWin is pressed again, it toggles the windows back to visible, but only specifically the ones which were hidden with "Special Hide". This is referred to as, appropriately enough, "Special Show" in the source code.
If in emergency, by pressing LCtrl+RCtrl keys together, you can to kill all captured windows (whether currently visible or hidden) and followed by this program killing itself. To kill the windows, I send them
WM_QUIT and `WM_Solution
So I spotted this bit...
If all you need to do is "flip the flag" at the end, you can do so by negating the current value:
Then you have this:
Now, I don't know if
So do it like this:
I think you should also look into splitting the message handling cases in
You've also got a typo in the comments at the top:
should be "And so on, up to a maximum (...)"
Lastly, as said by others already in the comments, this
If you need a visual indicator where your functions begin, there's a few options:
Now, that's regarding all the slashes for your functions.
In other places, they are down right dangerous:
I skipped those as comments.
That's what they are, aren't they? I just saw big blocks like this:
Which weren't that int
if (flag_ShowSpecialHidden) // Check whether to perform ShowSpecial or HideSpecial operation
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
ShowWindowSpecial(&winObjects[i]);
flag_ShowSpecialHidden = false; // Flip the Flag
}
else
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
HideWindowSpecial(&winObjects[i]);
flag_ShowSpecialHidden = true; // Flip the Flag
}If all you need to do is "flip the flag" at the end, you can do so by negating the current value:
if (flag_ShowSpecialHidden) // Check whether to perform ShowSpecial or HideSpecial operation
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
ShowWindowSpecial(&winObjects[i]);
}
else
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
HideWindowSpecial(&winObjects[i]);
}
flag_ShowSpecialHidden = !flag_ShowSpecialHidden;Then you have this:
// Initially set the flag to True...
flag_OperationAllowed = true;
// But set it to false if any control keys are pressed...
if ( CAPTURE_WINDOW || UNCAPTURE_ALL || TOGGLE_SPECIAL )
flag_OperationAllowed = false;
for (unsigned int numpadkey = 0; numpadkey <= 9; ++numpadkey)
if ( GetAsyncKeyState(numpadkey + VK_NUMPAD0) )
{
flag_OperationAllowed = false;
break;
}Now, I don't know if
GetAsyncKeyState has any sideeffects like clearing input buffers, but if all you're gonna do is set flag_OperationAllowed, then clearly we don't need to enter this loop if flag_OperationAllowed is already false. And setting it to true only it set it to false some time later in the same section of code is a waste of cycles.So do it like this:
// Initially set the flag to True...
flag_OperationAllowed = !(CAPTURE_WINDOW || UNCAPTURE_ALL || TOGGLE_SPECIAL )
if ( flag_OperationAllowed ) { //check numpadkeys
for (unsigned int numpadkey = 0; numpadkey <= 9; ++numpadkey)
if ( GetAsyncKeyState(numpadkey + VK_NUMPAD0) )
{
flag_OperationAllowed = false;
break;
}
}I think you should also look into splitting the message handling cases in
WindowProcedure up into separate functions - it's a pretty big function with a lot of things happening, and if you could reduce it to a switch that calls other functions, it'd be easier to see what it does at a glance.You've also got a typo in the comments at the top:
Ando so on upto a maximum of Slot 9. (Total 10 windows)should be "And so on, up to a maximum (...)"
Lastly, as said by others already in the comments, this
/////// style of yours is hard to maintain. It's also pretty annoying to read, in my opinion. Editing of text prefixed with a lot of slashes like that is harder than it would be without the slashes, and if you're in the business of habitually revising your comments due to changes made, the slashes lead to a lot of busy work.If you need a visual indicator where your functions begin, there's a few options:
- Document the functions, so the comment block at the start highlights a new function
- Configure your IDE to give function declarations a different color
- Make use of your IDE's ability to jump between function declarations
- Split up your code into different files that provide a better overview. (Such as a header file and an implementation).
Now, that's regarding all the slashes for your functions.
In other places, they are down right dangerous:
////////////////////////////////////////////////
//// Some defines for choosing Windows version
////////////////////////////////////////////////
#define Windows7 0x601
#define _WIN32_WINNT Windows7
#define WIN32_LEAN_AND_MEAN
////////////////////////////////////////////////
///////////////////////
//// Headers needed
///////////////////////
#include
#include
///////////////////////
//////////////////////////////////////////////////
//// This may be moved to a separate resource.h
//////////////////////////////////////////////////
#define ID_MY_TIMER 7777
//////////////////////////////////////////////////I skipped those as comments.
That's what they are, aren't they? I just saw big blocks like this:
//////////////////////////////////////////////////////////////////
//// Programmer Info
//////////////////////////////////////////////////////////////////
// Name: Anees Ahmed
// eMail: aneesahmed777@gmail.com
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//// Application Info
//////////////////////////////////////////////////////////////////////
// Name: LifeSaver
// Version: 3.0
// Completed: 4 June 2015
// Purpose: Hide specific windows from the screen whenever wanted
//////////////////////////////////////////////////////////////////////Which weren't that int
Code Snippets
if (flag_ShowSpecialHidden) // Check whether to perform ShowSpecial or HideSpecial operation
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
ShowWindowSpecial(&winObjects[i]);
flag_ShowSpecialHidden = false; // Flip the Flag
}
else
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
HideWindowSpecial(&winObjects[i]);
flag_ShowSpecialHidden = true; // Flip the Flag
}if (flag_ShowSpecialHidden) // Check whether to perform ShowSpecial or HideSpecial operation
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
ShowWindowSpecial(&winObjects[i]);
}
else
{
for (unsigned int i = 0, N = winObjects.size(); i < N; ++i)
HideWindowSpecial(&winObjects[i]);
}
flag_ShowSpecialHidden = !flag_ShowSpecialHidden;// Initially set the flag to True...
flag_OperationAllowed = true;
// But set it to false if any control keys are pressed...
if ( CAPTURE_WINDOW || UNCAPTURE_ALL || TOGGLE_SPECIAL )
flag_OperationAllowed = false;
for (unsigned int numpadkey = 0; numpadkey <= 9; ++numpadkey)
if ( GetAsyncKeyState(numpadkey + VK_NUMPAD0) )
{
flag_OperationAllowed = false;
break;
}// Initially set the flag to True...
flag_OperationAllowed = !(CAPTURE_WINDOW || UNCAPTURE_ALL || TOGGLE_SPECIAL )
if ( flag_OperationAllowed ) { //check numpadkeys
for (unsigned int numpadkey = 0; numpadkey <= 9; ++numpadkey)
if ( GetAsyncKeyState(numpadkey + VK_NUMPAD0) )
{
flag_OperationAllowed = false;
break;
}
}Ando so on upto a maximum of Slot 9. (Total 10 windows)Context
StackExchange Code Review Q#118747, answer score: 2
Revisions (0)
No revisions yet.