draw a cursor for a controller using MFC - mfc

I'm writing a MFC program that has a mouse-like controller connected.
The controller itself has callback function that I can get the position of it, but I want to draw a cursor for this controller that I can see where it is in the window.
There is a deviceclass that represents the controller. I new a device object in myCView class.
But I have no idea how can I keep updating the controller's position to the OnDraw() function to show its position when it is moving around.
Does anyone have any ideas about this?
Thanks a lot!

When you get a new cursor position save its x,y position in view member variables and call the view's Invalidate function. This will cause OnDraw to be called. Use the x,y in OnDraw to draw the cursor.

Related

get the index of the ClistBox item on Mouse over

I have a simple CListBox control. I dynamically add texts in it. Is it possible to get the index of the item over which the mouse is currently hovering on the listbox?
Basically, I want to display a tool tip for each item in the listBox based on the mouse position.
Code samples are really appreciated.
The CListBox::ItemFromPoint member returns the item index of the item nearest to a point.
The point needs to be in client coordinates of the CListBox. If you receive them in screen coordinates (e.g. by calling GetCursorPos), you will have to translate them calling CWnd::ScreenToClient on the CListBox.
The second parameter (bOutside) tells you, whether the position is over an item or not. The member function returns FALSE or TRUE, respectively.
I am writing this answer so that others can get help in case some one else is facing this problem.
First things first; you can't directly handle mouse move messages from Clistbox. So, you create a new class based on ClistBox class and then handle OnMouseMove() in that class.Then, inside that OnMouseMove() function you should get the item in the clistbox based on the mouse coordinates (as mentioned by IInspectable).
For code example; see below.
BOOL b;
int n = ItemFromPoint(point,b);
CString str;
GetText( n,str);
AfxMessageBox(str);

Get object clicked by mouseEvent in Qt C++

I have a scene made up of objects, each with a different position. I also have 2–4 pawns I'd like to move to the position of the object clicked, not to the position of the event.
I tried doing something like event->pos() but that gives me the coordinates of the event. Is there any way to do this?
The method you're looking for is QGraphicsView::items().

How to get the visible rect of the node in cocos2d or cocos2d-x?

How to get the visible rect of the node? Sometime i need to hide 50% of the sprite, if the touch point is in the hidden part, just return but not to active the callback function.
There is no default functions, maybe you need to create your own class to handle it.
You can try to create a subclass of CCNode with a changeable CCRect as touchable area, or you can try to read the Image Raw data and detect if the touch position pixel is transparent or not.

Qt C++ Let multiple QGraphicsItem handle one mouse event

I've created an object Chartblock that implements QGraphicsItem. My goal is to create a grid of these objects, and when the mouse button is pressed (and held) and is drug over each block, perform something on each block as the cursor enters it.
Since a QGraphicsItem grabs the mouse events when it is clicked within it, other Items will not fire for the mouseMoveEvent. I then created an object based on the QGraphicsItemGroup to handle all the mouse events, but then I would need some way to pass mousePressEvent/mouseReleaseEvent as well as mouseMoveEvent to each child that the cursor is over.
Am I overthinking how to do this? It seems like such a simple action shouldn't be that difficult to create, but with QGraphicsItems holding onto the mouse events for itself, I'm not sure how to get around it. I've read similar situations, but nothing seems to give a straightforward answer.
Edit: I suppose a way to do this would keep track of the coordinates/sizes of every single QGraphicsItem I create in an array, then get the position of the cursor in the Group mouseMoveEvent, and see if there's a hit..
I was able to pull together a few similar answers to create a solution; I dropped the idea of placing all my QGraphicItem's in a Group, and placed them directly on a scene. With the scene grabbing all mouse events, I have the mouseMoveEvent check to see if the current position is on top of a QGraphicsItem - if so, perform something.
I still need to try and get itemAt() to work for my own classes that implement QGraphicsItem, as itemAt returns only QGraphicsItem's, but I'm sure some cast should get it working.
void ChartScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF mousePosition = event->scenePos();
QGraphicsItem* pItem = this->itemAt(mousePosition.x(), mousePosition.y());
}

how to determine if a POINT is inside the area of a button

how to determine if a POINT is inside the area of a button?
the POINT is in screen coordinates , and I have the handle of the window in which the button is and the handle of the button.
I tried ::ScreenToClient(okBtnH , &tempPosition) where okBtnH is the handle of the button and the tempPosition is a POINT structure in screen coordinates. but it doesn't work.
Is there any way to do this except manual calculation of the screen position of the button and then compare it with the POINT str ?
Are you sure that okBtnH is a handle for the button control, and not just its ID? If ScreenToClient is failing with "Invalid Handle" then your handle is likely invalid.
If you are using MFC then you can just call the member function ScreenToClient. Why do you call the global version and are you sure the handle is the buttons handle?