2011-01-10 37 views
0

我想將鼠標座標寫入文本文件。這裏是我的代碼:如何在win32 api中將鼠標座標寫入文本文件?

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

謝謝。

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

編輯:
我收到這些警告(它們都指向同sprintf_s行):
警告C4047:「功能」:「爲size_t」在間接的水平不同於「燒焦[7]」
警告C4024:'sprintf_s':不同類型的正式和實際參數2
警告C4047:'函數':'const char *'與LONG'間接級別不同
警告C4024:'sprintf_s':不同類型正式和實際參數3

回答

1

sprintf_s()也需要告訴緩衝區的大小:

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

尼斯。我怎麼會想念這個? – infinitloop 2011-01-10 03:22:49