Tracking clipboard changing data in win32 console application? - c++

I am currently making a win32 console application in c++.
Now, I have to react on the user changing the clipboard content.
Currently I am just checking for the user pressing ctrl+c, but obviously that's not enough since it won't track rightclick->copy, etc.
Sadly i cannot just use the winmessage, since i am developing a console application, hence my application does not have its own hwnd.
And i really do not want to copy the clipboard data 10 times a second or so.
This is bound to cause problems with other programs since i have to lock and unlock the clipboard every time.
Any suggestions?

To monitor clipboard changes, you can use the AddClipboardFormatListener API:
When a window has been added to the clipboard format listener list, it is posted a WM_CLIPBOARDUPDATE message whenever the contents of the clipboard have changed.
An application that doesn't have a GUI can create a message-only window to receive change notifications:
A message-only window enables you to send and receive messages. It is not visible [...]. The window simply dispatches messages.

Related

MFC: Best place to prevent a window from re-appearing from a Restore operation

When my application is minimized, and the application programmatically closes a child window, the state of the child window between my framework and MFC goes out of sync because MFC will not send a WM_SHOWWINDOW message when the application is minimized. I noticed that Qt had the same problem: https://codereview.qt-project.org/#/c/93410/
Things that I have tried:
Override OnShowWindow() -- if the states are out of sync, then I alter the BOOL parameter before passing it to the CDialog::OnShowWindow. But doing so does nothing. It is as if the BOOL parameter given to the override is read-only.
Handle WM_SHOWWINDOW in PreTranslateMessage -- this does not work because WM_SHOWWINDOW does not appear here.
I know I can check SW_PARENTOPENING to know when to look for out-of-sync problems and handle it, but I just don't know where is the best place to do it.
My current solution is to override OnShowWindow, check for SW_PARENTOPENING, then post a SW_HIDE. It works, but it feels like a waste because I should be able to prevent it from restoring entirely rather than defer it.
Summary:
Basically, I am just programmatically closing a window, say from a timer call, or user's remote command, or whatever, while the main application is minimized. The dialog will be temporarily hidden when minimized (the MFC framework will automatically call ShowWindow(SW_HIDE) but with an internal flag to re-open when the app is restored). If my program sends ShowWindow(SW_HIDE) now, this call will not be registered, and the window will be re-opened by MFC when the app is maximized. To my user, he/she has closed the window remotely and does not expect the window to re-appear, so I need to re-call my ignored ShowWindow(SW_HIDE) somehow when restoring the main app.

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.

Stop playing sound and volume when application is minimized

I was using the PlaySound function in one C++ application under Windows.
When this application is running a local var command PlaySound and it starts to play a WAV file.
But when you click on the minimize dialog box of my application, the sound continues playing, when the logical rule must be silenced or muted until you maximize the application again.
The question is:
Is there application-state within Windows to detect when application is minimized?
In order to set the waveOutSetVolume function with 0x00 = mute.
Have your app's window catch the WM_SYSCOMMAND message and check it for the SC_MINIMIZE, SC_MAXIMIZE, and SC_RESTORE notifications.
Your application will receive a WM_WINDOWPOSCHANGED message when the application window has been minimized or maximized. Check the WINDOWPOS struct in lParam to determine the new state. You can then turn the sound off or on, respectively. (Note: this message is also sent when the window is moved, sent to back, etc. So check the lParam.)
If the application is currently minimized can be checked with IsIconic.
And I don't find it logical that the sound has to stop playing when the application is minimized. It's a multitasking OS after all. I want it to do things in parallel.

How to send mouse click event to a game application?

I try to send a mouse click event to a game application. First, i use Spy++ to find what message the application receive. I see something like : WM_MOUSEACTIVATE, WM_WINDOWPOSCHANGING, WM_ACTIVATEAPP, WM_ACTIVATE, WM_SETFOCUS, ...
i try to send the same as i see on Spy++ but it doesn't work. How to send mouse click to a game application without give it focus? . it's run in window mode. Thanks in advance.
You want WM_LMOUSEDOWN. You can always check MSDN for the documentation on which messages mean what.
The best way to automate applications and games is via SendInput. While in theory it should be possible to drive an application via WM_LUBTTONDOWN etc, many applications read the key state directly via lower level APIs (such as GetAsyncKeyState) which don't change their state to reflect the messages processed from the message queue.
Using SendInput requires actually setting the game to the foreground as the input events are synthesized at a low level and are thus delivered to the active/focused window.

Simulate input in own application without focus?

I was wondering if it is possible to simulate input in your own application without the window being focused or the window minimized, kind of like with PostMessage or SendMessage but without those APIs specifically? Thanks!
you can do that with global hooks.
so basically windows is sending messages to the application which is focused, but apps with global hooks will get all the input events for which they registered for inputs.