How could I understand that user pressed X button on cmd window? - c++

I have written a program in C++ and before exit I store some data in files. However, if the user clicks on the close box (X) on top right corner I lose those data. Is there a way to detect if the user clicks on the close box, so that I call some functions before exit?

Use SetConsoleCtrlHandler() to installer a handler that looks for the CTRL_CLOSE_EVENT event:
A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager).

Related

C++: Execute function when program terminates

Some programs pop "Save before exit?" message when terminating.
And I wonder if I can implement this with C++ console application.
So I tried some standard functions like signal and atexit.
But they only work when:
main() returns (atexit)
sending interrupt through Ctrl+C (on Windows, SIGINT)
an error occurs (SIGABRT)
So yeah, how? Is it only possible with GUI application?
In comments, you said:
I want exit events to happen when that 'X' button is pressed(On windows).
That's part of GUI I guess.
Than what kind of request is sent to program when the exit button of the console is pressed?
You can use SetConsoleCtrlHandler() to register a user defined callback function that receives a CTRL_CLOSE_EVENT notification when the console window is closed:
A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager).

Need to know windows API to get a callback when my application is activated

I have a wxWidgets windows application, I am launching annother application upon click a certain button on
my appication, This new launched application behaves like to modal window and my application is sent back, But
when user use Alt+Tab or click my appliction icon, My application comes to front, whereas child application
which is already opened should have been shown
I figured how to bring an application to front, Now i would like to know if i can set a callback to parent application
which will be called whenever application is activated (either through Alt+Tab or task bar icon or any other way),
So i can bring my child application to front at this time.Is there a windwos API for this?
WM_ACTIVATE
Sent to both the window being activated and the window being
deactivated. If the windows use the same input queue, the message is
sent synchronously, first to the window procedure of the top-level
window being deactivated, then to the window procedure of the
top-level window being activated. If the windows use different input
queues, the message is sent asynchronously, so the window is activated
immediately.
case WM_ACTIVATE:
{
// test if window is being activated
if(LOWORD(wParam)!=WA_INACTIVE)
{
// application is being activated
}
else
{
// application is being deactivated
}
}
break;
EDIT:
If you want to use a hook to monitor whether the window is switched, you can refer to this link.
Capture switch window event (window focus) (Alt+TAB)

Programmatically clicking toolbar button in parent of modal window

I have an application that hooks into another application via an API. My application launches a modal window which prevents keypresses to reach the parent as one would expect.
However due to limitations in the API I need to click one of the parents toolbar buttons from time to time (yes it's a kludge).
I wonder if this is possible while still having the modal window of my application active? Is it perhaps possible to send the required command directly into the parent command queue?
Clicking the button programmatically with no modal window should not be a problem, one could go by this link for example: http://forums.codeguru.com/showthread.php?307633-How-to-run-a-very-long-SQL-statement. But I would prefer not having to close my window each time I have to click the button.
Although the fifth answer is what I find interesting as I'm thinking this could make it possible to send the command without having to close my modal window first. Also it feels an ever so small bit less ugly.
First of all, when a modal dialog is shown, it runs its own message pump. So any attempt to fake input messages will land in the modal dialog message pump. Which is no good to you. So, you'd have to send a message rather than fake input.
However, when a modal dialog is shown, its owning windows are disabled. Which means that these windows will not respond to any messages you send. So I guess that means that you could:
Enable the owning top-level window that you hosts the toolbar in question.
Send the message to the toolbar button.
Disable the owning window again.
Not the prettiest way to go about things, but you did ask!

How to make C++ to execute a function when I close the console

I want a C++ program to execute a function when I close manually the console.
I made a C++ program that test a password and if it isn't correct make the windows to log off.
But if I close the console from the "X" button nothing happens and I want to make windows to log off too if the console is closed from "X" button?
I tried _onexit_t oe() function but it doesn't help me.
So there is a method to do that or to hide the bar which contains the "Minimize", "Maximize" and "Close" buttons?
Assuming you mean the normal textual console window, you can register your own event handler via SetConsoleCtrlHandler and watch for the events CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT, etc.

How to get notified of textbox focus?

Using a Windows 7 touch device Windows shows this little touch-keyboard indicator (tabing this will bring up the touch on screen keyboard) when you tab/focus a textbox or kind of input field (Notepad etc.).
I want to write an application that gets notified when exactly that happens, a textbox (etc.) gets focused (no matter which application).
Are applications informed about focusing in other applications, do I need to hook something?
Is there a way in doing so in c++?
I believe the SetWinEventHook function and specifically the EVENT_OBJECT_FOCUS event is what you are looking for.
From the MSDN description:
An object has received the keyboard focus. The system sends this event for the following user interface elements: list-view control, menu bar, pop-up menu, switch window, tab control, tree view control, and window object. Server applications send this event for their accessible objects.
The hwnd parameter of the WinEventProc callback function identifies the window that receives the keyboard focus.