How to detect mouse cursor is outside a windows? - c++

I've written a code that dynamically creates a POPUP style window when the user clicks inside my main app window. Now I'd like the POPUP window to be automatically destroyed when the mouse cursor goes out of the POPUP wnd region. I know that i have probably handle the WM_MOUSEMOVE message but how to do that? Please provide a simple code for that if You can...

Use the WM_MOUSELEAVE message instead. However, note that this message has to be explicitly requested via TrackMouseEvent(), which your window can call when it receives its first WM_MOUSEMOVE message.

As #Remy Lebeau said, the following is the code implementation.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL Tracing = FALSE;
switch (message)
{
case WM_MOUSELEAVE:
{
DestroyWindow(hWnd);
break;
}
case WM_MOUSEMOVE:
{
if (!Tracing)
{
Tracing = TRUE;
TRACKMOUSEEVENT tme{};
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
TrackMouseEvent(&tme);
}
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

you can compare the event.target and event.currentTarget, if both are same then you are out side of popup window else in side the popup window.

Related

WM_PAINT based on button click

I am trying to write a windowprocedure that would call the animation of a rectangle in the window only when the start button is clicked and stop when the stop button is clicked.
I tried doing this like this:
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (wParam)
{
case BUTTON_START:
stopClicked = false;
DestroyWindow(hStartButton);
CreateStopButton(hWnd);
Animate(hWnd);
return 0;
case BUTTON_STOP:
stopClicked = true;
DestroyWindow(hStopButton);
CreateStartButton(hWnd);
return 0;
}
case WM_CREATE:
AddMenus(hWnd);
CreateStartButton(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProcW(hWnd, msg, wParam, lParam);
}
return 0;
}
the Animate function:
void Animate(HWND hWnd)
{
HDC hdcWnd = GetDC(hWnd);
while(!stopClicked)
{
//drawing code
}
ReleaseDC(hWnd, hdcWnd);
DeleteDC(hdcWnd);
}
The program crashes as it never exist the while(!stopClicked) loop.
My question is how to make that possible that the animation would stop on a button click?
Your application hanged, bucause you are waiting for a flag to change and there is no way it will change.
WindowProcedure is called on an event, and until you leave it, any other event won't be processed.
What you need to do is to perform steps of animation on timer.
You need to setup a timer which will send you an event which you have to handle and there you can draw next frame of your animation.

Winapi detect button hovering

I've got a C++ project in which I'm using the Winapi to develop a window with a button and I want to change the text of the button when it's being hovered. For example, changing "Click me" to "Click me NOW!", when hovered. I've tried searching but I've not found any good ways to do this.
I noticed that when user hovers, the WM_NOTIFY message is received, but I don't know how to ensure that it has been called by the mouse hover. I've found that I can use TrackMouseEvent to detect hovering, but it's limited to a period of time and I want to execute an action every time the user hovers the button.
Here is how I create a button:
HWND Button = CreateWindow("BUTTON", "Click me",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_NOTIFY,
20, 240, 120, 20,
hwnd, (HMENU)101, NULL, NULL);
And this my window procedure:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_NOTIFY:
{
//??? Here is where I get a message everytime I hover the button, But I don't know any proper way to see if it has been executed by the button.
}
case WM_CREATE: //On Window Create
{
//...
}
case WM_COMMAND: //Command execution
{
//...
break;
}
case WM_DESTROY: //Form Destroyed
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Assuming you're using the common controls there is the BCN_HOTITEMCHANGE notification code for the WM_NOTIFY message. The message includes the NMBCHOTITEM structure, which includes information for whether the mouse is entering or leaving the hover area.
Here's an example:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_NOTIFY:
{
LPNMHDR header = *reinterpret_cast<LPNMHDR>(lParam);
switch (header->code)
{
case BCN_HOTITEMCHANGE:
{
NMBCHOTITEM* hot_item = reinterpret_cast<NMBCHOTITEM*>(lParam);
// Handle to the button
HWND button_handle = header->hwndFrom;
// ID of the button, if you're using resources
UINT_PTR button_id = header->idFrom;
// You can check if the mouse is entering or leaving the hover area
bool entering = hot_item->dwFlags & HICF_ENTERING;
return 0;
}
}
return 0;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
You can check the code of the WM_NOTIFY message to see if it is a NM_HOVER message.
switch(msg)
{
case WM_NOTIFY:
if(((LPNMHDR)lParam)->code == NM_HOVER)
{
// Process the hover message
}
else if (...) // any other WM_NOTIFY messages you care about
{}
}
You can use simply SFML to do this.
Code:
RectangleShape button;
button.setPosition(Vector2f(50, 50));
button.setSize(Vector2f(100, 40));
button.setFillColor(Color::Green);
if(button.getGlobalBounds().contains(static_cast<Vector2f>(Mouse::getPosition(/*your
window name*/window)
{
button.setFillColor(Color::Red);
}

c++ win32 api notify mouse inside button area

I've a Button made using Win32Api that I want to be able to notify whenever the user put the mouse inside the button rectangle.
I noticed that when user does that the WM_NOTIFY is called but I don't know which flag to use for ensure that the user has the mouse inside it's area.
Here is my button:
HWND Button = CreateWindow("BUTTON", "Test",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_NOTIFY,
20, 240, 120, 20,
hwnd, (HMENU)101, NULL, NULL);
And my WndProc:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_NOTIFY:
{
//??? Here is where I want to do it
}
case WM_CREATE: //On Window Create
{
wHWND = hwnd;
if (onCreate != NULL)
onCreate(hwnd);
break;
}
case WM_COMMAND: //Command execution
{
//...
break;
}
case WM_DESTROY: //Form Destroyed
{
if (onDestroy != NULL)
onDestroy(hwnd);
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
I don't know if I'm in the right path using WM_NOTIFY or not, maybe there is an easier way to do that. Thank you!
Try catching the WM_SETFOCUS message instead if you want a generic way of tracking focus.
BN_SETFOCUS is sent as WM_COMMAND in the upper 16 bits of WPARAM (HIWORD):
The parent window of the button receives this notification code through the WM_COMMAND message
If you are not talking about focus but just the mouse position then you can use ChildWindowFromPointEx or RealChildWindowFromPoint and a timer.

Why is this code running?

I wrote the following WndProc. It is used by a Notify Icon. I removed the unimportant parts (like default labels) for an better overview.
When I click the Nofify Icon with the right mouse button, the context menu appears. When I click an item, I get the corresponding return value of TrackPopupMenu and print it out. However, TrackPopupMenu is a blocking call, but the WndProc is just working fine while the context menu is opened. Why?
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, ID_TRAY_EXIT_CONTEXT_MENU_ITEM, displayString);
}
break;
case WM_TRAYICON:
switch (lParam)
{
case WM_RBUTTONUP:
{
POINT curPoint;
GetCursorPos(&curPoint);
UINT clicked = TrackPopupMenu(
hMenu,
TPM_RETURNCMD | TPM_NONOTIFY,
curPoint.x,
curPoint.y,
0,
hWnd,
NULL
);
std::cout << std::to_string(clicked) << std::endl;
}
break;
}
std::cout << lParam << std::endl;
break;
}
return 0;
}
Because TrackPopupMenu is pumping messages while it is executing. That is, it has a message processing loop that calls DispatchMessage for any new messages posted to the thread's message queue, and DispatchMessage in turn calls your window procedures with the messages intended for each window.

WM_MBUTTONDOWN doesn't work

I use WM_MBUTTONDOWN to keep track of middle mouse button when it's pressed. So, when I click the middle mouse button at the mouse wheel, it will display the message on the console. I wonder why it doesn't work. Is WM_MBUTTONDOWN for another usage?
LRESULT CALLBACK UI::WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
// The message is post when we destroy the window.
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MBUTTONDOWN:
cout << "Middle button clicked" << endl;
break;
// Default handling for other messages.
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Output:
Mouse used: Logitech
This looks like a driver issue. On some mice WM_MBUTTONDOWN message is not received. Make sure you use a mouse driver that supports middle button.
Check if the middle button works in other applications - if not, it is a driver issue.