dev c ++. Create a Windows Application Project. Get the coordinates of the cursor every 1 second and write all the results into a text file.

    1 answer 1

    To get coordinates, use the GetCursorPos function.

    POINT p; if (GetCursorPos(&p)) { //позиция курсора: px py } 

    You also need ScreenToClient for mapping coordinates in win coordinates.

     if (ScreenToClient(hwnd, &p)) { //px и py относительны вашего окна } 

    To receive coordinates periodically, use for example a timer:

     #include <iostream> #include <chrono> #include <thread> #include <functional> void timer_start(std::function<void(void)> func, unsigned int interval) { std::thread([func, interval]() { while (true) { func(); std::this_thread::sleep_for(std::chrono::milliseconds(interval)); } }).detach(); } void do_something() { std::cout << "I am doing something" << std::endl; } int main() { timer_start(do_something, 1000); while(true); } 

    You can write data to the file as follows:

     #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("тест.txt"); myfile << "Данные для записи.\n"; myfile.close(); return 0; } 
    • ScreenToClient on the condition of the problem is hardly needed. Well, if WinAPI, then why not SetTimer or even Sleep in a loop? - Vladimir Martyanov
    • Yes, and the timer with a slip :) Nobody forbids. - Mstislav Pavlov