如何在win32 api中将鼠标坐标写入文本文件?

时间:2011-01-10 03:05:18

标签: winapi file-io type-conversion

我想将鼠标坐标写入文本文件。这是我的代码:

HANDLE hfile;
 DWORD nOut;
 POINT mouseCoords;
 int counter = 10;

 char buffer[10];

 /*CRETAE_ALWAYS - creates a new file OR overwrites existing one*/
 hfile = CreateFile(g_fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 /*Make sure that file is successfully created*/
 if ( hfile == INVALID_HANDLE_VALUE ) {
  MessageBox(NULL, "Cannot create file!", "Error!",
   MB_ICONEXCLAMATION | MB_OK);
  return 0; 
 }

 //while ( counter >= 0 ) {
  GetCursorPos(&mouseCoords);
  sprintf_s(buffer, "%d, %d", mouseCoords.x, mouseCoords.y);

  //buffer[0] = (char)mouseCoords.x;
  //buffer[1] = (char)mouseCoords.y;

  if ( !( WriteFile(hfile, buffer, 2/*strlen(buffer)*/, &nOut, NULL) ) ) {
   MessageBox(NULL, "Cannot write to file!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
   return 0;  
  }

谢谢。

我在windows vista下使用win32 api,Visual Studio 2008。

编辑:
我收到这些警告(他们都指向sprintf_s的行):
警告C4047:'function':'size_t'的间接级别与'char [7]'不同 警告C4024:'sprintf_s':正式和实际参数2的不同类型 警告C4047:'function':'const char *'的间接级别与'LONG'不同 警告C4024:'sprintf_s':正式和实际参数3的不同类型

1 个答案:

答案 0 :(得分:1)

还需要告诉

sprintf_s()缓冲区的大小:

sprintf_s(buffer, sizeof buffer, "%d, %d", mouseCoords.x, mouseCoords.y);