How to force the cursor to the center of a window.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    How to force the cursor to the center of a window.

    Hi guys.

    I'm writing a game in C++ and DirectX and I'm looking for a way to force the mouse to stay in the center of the window.

    I tried this, but it doesn't seem to work. It doesn't give any errors, it simply does nothing.
    [code=cpp]
    RECT wRect;
    GetWindowRect(h Wnd, &wRect);
    COORD wCoord = {wRect.left + (CLIENT_WIDTH / 2), wRect.top + (CLIENT_HEIGHT / 2)};
    SetConsoleCurso rPosition(
    GetStdHandle ( STD_OUTPUT_HAND LE ),
    wCoord
    );
    [/code]

    I also tried to use the IDirect3DDevice 9::SetCursorPos ition method but that also did nothing.
    [code=cpp]
    RECT wRect;
    GetWindowRect(h Wnd, &wRect);
    d3dDevice->SetCursorPosit ion(
    wRect.left + (CLIENT_WIDTH / 2),
    wRect.top + (CLIENT_HEIGHT / 2),
    D3DCURSOR_IMMED IATE_UPDATE
    );
    [/code]

    Anybody know how I can do this?

    I should add that I'm using DirectInput to capture the mouse. Don't think it matters, but then again, what do I know :P
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Atli
    Hi guys.

    I'm writing a game in C++ and DirectX and I'm looking for a way to force the mouse to stay in the center of the window.

    I tried this, but it doesn't seem to work. It doesn't give any errors, it simply does nothing.
    [code=cpp]
    RECT wRect;
    GetWindowRect(h Wnd, &wRect);
    COORD wCoord = {wRect.left + (CLIENT_WIDTH / 2), wRect.top + (CLIENT_HEIGHT / 2)};
    SetConsoleCurso rPosition(
    GetStdHandle ( STD_OUTPUT_HAND LE ),
    wCoord
    );
    [/code]

    I also tried to use the IDirect3DDevice 9::SetCursorPos ition method but that also did nothing.
    [code=cpp]
    RECT wRect;
    GetWindowRect(h Wnd, &wRect);
    d3dDevice->SetCursorPosit ion(
    wRect.left + (CLIENT_WIDTH / 2),
    wRect.top + (CLIENT_HEIGHT / 2),
    D3DCURSOR_IMMED IATE_UPDATE
    );
    [/code]

    Anybody know how I can do this?

    I should add that I'm using DirectInput to capture the mouse. Don't think it matters, but then again, what do I know :P
    I'm not sure if this might work but try the SetCusorPos function:
    [code=cpp]
    SetCursorPos(CL IENT_WIDTH / 2, CLIENT_HEIGHT / 2);
    [/CODE]

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Originally posted by ilikepython
      I'm not sure if this might work but try the SetCusorPos function:
      [code=cpp]
      SetCursorPos(CL IENT_WIDTH / 2, CLIENT_HEIGHT / 2);
      [/CODE]
      That works perfectly!

      Thanks very much.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Atli
        That works perfectly!

        Thanks very much.
        Glad to help .

        Comment

        Working...