由于某些原因,当我试图使用以下代码输出鼠标光标在给定的WIN32窗口中的位置时:
//Global Variable
POINT cursorLocation;
// Win32 Windowing subsystem code redacted
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
cursorLocation.x = GET_X_LPARAM(lParam);
cursorLocation.y = GET_Y_LPARAM(lParam);
switch(message){
case WM_MOUSEMOVE:
// mouse movement handle
OutputDebugString(cursorLocation.x + "," + cursorLocation.y);
OutputDebugString("\n");
// WM_MOUSEMOVE break
break;
}
}现在,当我运行程序并移动鼠标时,以下文本将被记录到控制台:
较小的数据类型导致数据丢失。 如果这是有意的,则应使用适当的位掩码掩蔽强制转换的源。 例如: char c= (i & 0xFF); 以这种方式更改代码不会影响结果优化代码的质量。
我甚至尝试将传递给OutputDebugString的变量类型转换为LONG,因为这是POINT类中变量的类型,没有区别。
有人知道如何将值通过GET_X_LPARAM和GET_Y_LPARAM传递到调试控制台吗?谢谢。
发布于 2016-11-17 20:18:55
这不是字符串连接,而是将.x和.y添加到指向","的指针中。
cursorLocation.x + "," + cursorLocation.y相反,试着例如:
char s[256];
sprintf_s(s, "%d,%d", cursorLocation.x, cursorLocation.y);
OutputDebugStringA(s); // added 'A' after @IInspectable's comment, but
// using UNICODE and wchar_t might be better indeed 发布于 2016-11-17 20:25:33
字符串级联不适用于整数。尝试使用std::ostringstream
std::ostringstream out_stream;
out_stream << cursorLocation.x << ", " << cursorLocation.y;
OuputDebugString(out_stream.str().c_str());https://stackoverflow.com/questions/40664225
复制相似问题