简体   繁体   English

有没有办法在mfc应用程序中获取光标位置?

[英]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: 我正在使用OnKeyDown处理程序来检测Enter键是否按下:

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? 在if语句的第3行我需要将光标位置存储在数组中,但是如何获取该点呢? 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 . 要获取当前光标位置,可以调用GetCursorPos I don't believe MFC provides a wrapper for this, so it'll just be the Win32 ::GetCursorPos . 我不相信MFC为此提供了一个包装器,所以它只是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. 它返回屏幕坐标中的点,因此您(几乎可以肯定)希望在存储之前使用ScreenToClient将其转换为客户区坐标。

Note, however, that GetCursorPos will return the position of the cursor when you call it . 但请注意, GetCursorPos在您调用时返回光标的位置。 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 ). 通常,要添加这样的光标位置,您需要对用户单击鼠标按钮做出反应(例如, WM_LBUTTONDOWN )。 That message will tell you the position of the mouse when the button was clicked. 该消息将告诉您单击按钮时鼠标的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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