无法输出游标位置到调试字符串(WIN32)

Cannot output cursor location to debug string (WIN32)

本文关键字:字符串 WIN32 调试 输出 游标 位置      更新时间:2023-10-16

由于某些原因,当我试图使用以下代码在给定的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 &
0 xff);以这种方式更改代码不会影响最终优化代码的质量。

我甚至尝试将传递给OutputDebugString的变量类型转换为LONG,因为这是POINT类中变量的类型,并且没有差异。

有人知道如何通过GET_X_LPARAMGET_Y_LPARAM将值传递到调试控制台吗?谢谢。

这不是字符串连接,而是将.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                    

字符串连接不适用于整数。尝试使用std::ostringstream:

std::ostringstream out_stream;
out_stream << cursorLocation.x << ", " << cursorLocation.y;
OuputDebugString(out_stream.str().c_str());