2

I've just recently learned how to get my mouse position but if I move my window it is having a problem. Example, I want to draw a dot in the position of the mouse coordinate (x = 100, y = 100) so the system will draw at that coordinate in the window and that's the problem because, the mouse position is read according to the position of the SCREEN instead of the WINDOW. If I can somehow get the mouse coordinate according to the window instead of the screen that would fix the problem.

photo

#include<graphics.h>
#include<iostream>
#include<windows.h>
using namespace std;

int main()
{
    initwindow(800,600);
    POINT CursorPosition;

    while(1)
    {
        GetCursorPos(&CursorPosition);

        cout << CursorPosition.x << endl;
        cout << CursorPosition.y << endl;

        if(GetAsyncKeyState(VK_LBUTTON)) {
        bar(CursorPosition.x, CursorPosition.y, CursorPosition.x+50, 
        CursorPosition.y+50);
        }

        delay(5);
        Sleep(5);
        system("cls");
    }
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Vincent
  • 31
  • 5
  • 6
    ***I move my window it is having a problem. Please help thank you!*** We don't know the problem. Although please note that `graphics.h` is not modern code. It was meant to run on DOS in the 1990s not a windows machine in 2020. Also you don't seem to be using `graphics.h` at all (other than including the header) in the code that is shown. – drescherjm Mar 12 '20 at 12:43
  • What sort of "problem", Vincent? – Asteroids With Wings Mar 12 '20 at 12:51
  • I apologise for not clearly explaining what problem I m having. I've edited my question please try the new code and you might be able to figure it out. ty – Vincent Mar 12 '20 at 12:56
  • 1
    @Vincent you still didn't explain what the "problem" is? And there is a missing `}` somewhere in your code, and your code probably does not compile. Is this the problem? Please [edit] your question and clarify. If there as compilation errors, post the full error log. – Jabberwocky Mar 12 '20 at 13:16
  • @Jabberwocky No there is no compilation error I just forgot to put the "}" please try and run the code again. Try clicking somewhere inside the window and move the window and you will see big difference . – Vincent Mar 12 '20 at 13:27
  • 1
    We would prefer that you describe the problem in your question, to save time - we get thousands of questions every day so you are competing for our time! Help us to help you. :) – Asteroids With Wings Mar 12 '20 at 14:09
  • 2
    I believe (my memory on this subject is fading) you need a handle to the window that you are drawing on to calculate the position in window coordinates. With that said I really don't think you should be mixing the windows api and the 25 year old bgi. I don't believe the port exposes the windows api directly. – drescherjm Mar 12 '20 at 14:24

1 Answers1

1

Here is a simple solution which shows you how to convert cursor position to window position. This Proof of Concept utilizes GetForeGroundWindow(), GetCursorPosition() and ScreenToClient(). Hold right mouse button and move your mouse around the console window to see the output

#include <iostream>
#include <windows.h>

int main()
{
    while (1)
    {
        if (GetAsyncKeyState(VK_RBUTTON))
        {
            POINT pnt;
            GetCursorPos(&pnt);
            ScreenToClient(GetForegroundWindow(), &pnt);

            std::cout << "x: " << pnt.x << " y: " << pnt.y << std::endl;

            Sleep(300);
        }
    }

    return 0;
}
GuidedHacking
  • 3,628
  • 1
  • 9
  • 59