简体   繁体   中英

Is there a way to get the cursor position in a mfc application?

I'm using the OnKeyDown handler to detect for an Enter key press:

void CShortestPathFinderView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
  if( nChar == VK_RETURN)    //Enter key is pressed
{
    CClientDC aDC(this);
    rubberbanding = 0;
            m_pTempElement->vertices[i++]=  /*cursor position??*/;
    mSecondPoint=m_pTempElement->vertices[0];
    m_pTempElement->Draw(&aDC);
}

In the 3rd line of the if statement I need to store the cursor position in an array, but how do I acquire that point? Does the handler provide me with it? or is there a separate function to do so?

To get the current cursor position, you can call GetCursorPos . I don't believe MFC provides a wrapper for this, so it'll just be the Win32 ::GetCursorPos . It returns the point in screen coordinates, so you'll (almost certainly) want to use ScreenToClient to convert that to client area coordinates before storing it.

Note, however, that GetCursorPos will return the position of the cursor when you call it . That may or may not be quite the same as the position the cursor was at when the key was pressed (though it will normally be at least quite close).

Usually, to add a cursor position like this, you'd want to react to the user clicking the mouse button (eg, WM_LBUTTONDOWN ). That message will tell you the position of the mouse when the button was clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM