A message box that never loses focus - c++

A message box created with
MessageBox (NULL, "Text", "Title", MB_ICONINFORMATION | MB_SYSTEMMODAL);
stays on top of other windows, but it loses keyboard focus when the user clicks another window.
How could I create a message box (or an Edit box or a dialog box) that never loses keyboard focus, so if I try to switch to another window using [ALT-TAB] or mouse or any other method, or if another application running in a background opens its own Edit box, the keyboard focus would jump back to my message / Edit box?
The standard MessageBox function doesn't have such an option, so I tried a custom Edit box. I experimented with WM_SETFOCUS, WM_KILLFOCUS, WM_NCACTIVATE, SetForegroundWindow, SetFocus, but had no luck so far. When I open another window, the keyboard focus stubbornly goes to that window.

RegisterHotKey(0,1,MOD_ALT,VK_TAB); //disables alt+tab until message box returns
SetTimer(hwnd,1,100,0); //0.1 sec is enough for message box window creation
MessageBox(0,"Text","Title",MB_ICONINFORMATION|MB_SYSTEMMODAL); //since it's system modal, main message loop will wait for it to return
ClipCursor(0); //frees cursor
UnregisterHotKey(0,1); //enables alt+tab again
Here we use a timer to clip cursor so that user won't be able to click outside of the message box. I left title part of the messagebox outside of the clip area because clicking there negates cursor clipping.
case WM_TIMER:
{
KillTimer(hwnd,1);
RECT rc;
GetWindowRect(FindWindow(0,"Title"),&rc);
rc.top+=22; //title area left out of clip area
ClipCursor(&rc); //user cannot move their mouse outside of the message box
break;
}
This however can't block Ctrl + Alt + Del but does what you ask.

Related

Get HWND of a window i clicked on

I'm making a program, with c++ and winapi, where you can click a button, then select a window and it changes the transparency of that window.
I have no problems of making the window transparent, but i can't get the hwnd of a window.
When I use this function the program just crashes (No errors are being printed to the console).
HWND getHWND(HWND self)
{
HWND found = GetForegroundWindow();
if(found == self || found == NULL)
return getHWND(self);
return found;
}
I tried using this, but I don't like it, because the user has only 2 seconds to click on another window. (I'know I could use a bigger delay, but then it might be to much.. so this isn't optimal)
HWND getHWND()
{
Sleep(2000);
return GetForegroundWindow();
}
Don't poll for the active window, especially not with a recursive function. Just let the OS notify you when a window is clicked.
When the user clicks on your button, you can use SetCapture(), or a mouse hook via SetWindowsHookEx(), to direct subsequent mouse clicks to your app even if they are clicking on someone else's window. When you detect a click, release the capture/hook, and then use WindowFromPoint() to get the HWND at the location of the click.

How to check mouse click position is on required application?

I know the mouse click position using API GetCursorPos and the handle of application (HWND).
How can I check mouse click position is on this application?
My view:
Get the bounding box of application from its handle. (GetWindowRect(hWnd, &rect);)
Check cursor position lies in this bounding box. (PtInRect(&rect, p))
This is not working if windows are overlapping.
As we know the handle of targeted screen handle and click cursor position:
// hWnd : Already known windows handle
GetCursorPos(&p);
HWND hWndFromPoint = WindowFromPoint(p);
// If the handle got from click point is child of the desire window means it is clicked on the desire window itself.
if (IsChild(hWnd, hWndFromPoint))
{
// Do something on Mouse click
}

Dragging the console window with ReadConsoleInput()

As you can see from the code snippet below, I've implemented a way to drag the Windows command prompt by its client area.
The problem with this code is that if the user follows these steps:
Unfocus console window
Focus console window by clicking and dragging it (without releasing)
Drag the window so that the cursor escapes window area (this can mean moving the cursor too quickly, or out of the set bounds (second monitor), or over the taskbar/other always-on-top window)
The console window will stop following the cursor, until it's moved to be inside the window again.
The fact that this does not happen when the console window is already in-focus at step 1, is what's really weird to me. I've tried debugging this for so many hours now, I just can't do it anymore. I'd appreciate any help regarding this.
// Continuously read input
while(ReadConsoleInput(hIn, &ir, 1, &nr))
{
switch(ir.EventType)
{
// Left mouse button down that either focuses or unfocuses console window
case FOCUS_EVENT:
// Left mouse button down that focuses console window
if(ir.Event.FocusEvent.bSetFocus)
{
GetCursorPos(&firstPos);
ScreenToClient(hWnd, &firstPos);
}
break;
case MOUSE_EVENT:
// Mouse did something inside console window
switch(ir.Event.MouseEvent.dwButtonState)
{
// Left mouse button down or up
case FROM_LEFT_1ST_BUTTON_PRESSED:
// Left mouse down or up, no drag
if(!ir.Event.MouseEvent.dwEventFlags)
{
GetCursorPos(&firstPos);
ScreenToClient(hWnd, &firstPos);
}
// Left button down, and mouse move. -> drag
if(ir.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
{
GetCursorPos(&currentRelativeToScreen);
// Calculate window position while dragging
// |
// v
if(currentRelativeToScreen.x - firstPos.x > scrnSz.right - ca.right)
wndPos.X = scrnSz.right - ca.right;
else if(currentRelativeToScreen.x - firstPos.x < 0)
wndPos.X = 0;
else
wndPos.X = currentRelativeToScreen.x - firstPos.x;
if(currentRelativeToScreen.y - firstPos.y > scrnSz.bottom - ca.bottom)
wndPos.Y = scrnSz.bottom - ca.bottom;
else if(currentRelativeToScreen.y - firstPos.y < 0)
wndPos.Y = 0;
else
wndPos.Y = currentRelativeToScreen.y - firstPos.y;
// End window position calculations
SetWindowPos(hWnd, 0, wndPos.X, wndPos.Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
break;
default:
break;
}
break;
default:
break;
}
Drag the window so that the cursor escapes window area [...] The console window will stop following the cursor, until it's moved to be inside the window again.
This is the expected behavior, as documented at https://msdn.microsoft.com/en-us/library/windows/desktop/ms684239.aspx (emphasis added).
Mouse events are generated whenever the user moves the mouse, or presses or releases one of the mouse buttons. Mouse events are placed in a console's input buffer only when the console group has the keyboard focus and the cursor is within the borders of the console's window.
[ EDIT] To answer the followup comment about this.
this does not happen when the console window is already in-focus at step 1
Though not obvious from the official documentation, the console window appears to capture the mouse when clicked (and thus track it even when moved outside the console window) only if it already had the focus at the time of the click. For a console window out of focus, the 1st click gives it the focus (without globally capturing the mouse, so it only receives MOUSE_MOVE notifications while the cursor is within its client area), while the 2nd click enters exclusive capture mode (and receives all MOUSE_MOVE notifications regardless of cursor position).
This can be verified with a standard console window set to quick-edit mode. If the window has the input focus, then click-and-drag selects text in the console, even if the mouse is moved outside the window. However, if the window does not have the focus. the 1st click just gives it the focus, but does not enter any capture mode and dragging does not select any text.
on mouse move do:
if(button==leftButton){
ReleaseCapture();
SenMessage(hWnd,WM_NCLBUTTON_DOWN,HTCAPTION,lParam);
}

How to keep window inactive on simulated clicks?

I made a program in C++ that simulates clicks on an inactive window using:
PostMessage (z, WM_LBUTTONDOWN, 0,MAKELONG(t.left+x,t.top+y));
But whenever it makes a click it activates the window and the window moves to the top.
Is there a way I can make the window stay inactive or another way to click it?
I used SetWindowPos(z , HWND_BOTTOM,....) to make that window be at the bottom of the z-order list but it still activates.
EDIT: the window is a game console
Try switching from PostMessage to SendInput and see if you get the same effect.

MFC Unclickable Button (Running away from cursor on MouseMove)

How would i make a button that will change it's position on MouseMove Event if the cursor is close enough to the center of the button in MFC ?
WM_MOUSEMOVE is not delivered to the button if the cursor is not over it (and is not captured, but you don't want that). So you have to process WM_MOUSEMOVE in the parent dialog. If you want your button to be a self-contained control, you have to subclass the parent window upon button creation.
Subclassing, in this context, means:
- you retrieve and store the parent's window proc address with GetParent()->GetWindowLong(GWL_WNDPROC)
- you set it to your procedure with SetWindowLong()
- in the procedure, you call the parent's previous window proc, after handling WM_MOUSEMOVE the way you want.
The WM_MOUSEMOVE coordinates will be relative to the screen, but you'll probably want to track the button position relative to the window that contains it. Use the ScreenToClient method on the parent window to convert, then you can compare the coordinates to see if it's close. Then use MoveWindow to move the button.
If you track the mouse cursor position you can determine when the cursor gets close to or enters the button window rect. You can then use the SetWindowPos() function to reposition the button window in the parent window client area.