开发者

how to write mouse coordinates to a text file in win32 api?

I would like to write mouse coordinates to a text file. Here is my code:

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;  
  }

Thank you.

i am using win32 api, Visual Studio 2008, under windows vis开发者_开发问答ta.

EDIT:

I am getting these warnings (They all point to the line with sprintf_s):

warning C4047: 'function' : 'size_t' differs in levels of indirection from 'char [7]'

warning C4024: 'sprintf_s' : different types for formal and actual parameter 2

warning C4047: 'function' : 'const char *' differs in levels of indirection from 'LONG'

warning C4024: 'sprintf_s' : different types for formal and actual parameter 3


sprintf_s() also needs to be told the size of the buffer:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜