By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

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

Atli
Expert 5K+
P: 5,058
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.
Expand|Select|Wrap|Line Numbers
  1. RECT wRect;
  2. GetWindowRect(hWnd, &wRect);
  3. COORD wCoord = {wRect.left + (CLIENT_WIDTH / 2), wRect.top + (CLIENT_HEIGHT / 2)};
  4. SetConsoleCursorPosition(
  5.     GetStdHandle ( STD_OUTPUT_HANDLE ),
  6.     wCoord
  7. );
  8.  
I also tried to use the IDirect3DDevice9::SetCursorPosition method but that also did nothing.
Expand|Select|Wrap|Line Numbers
  1. RECT wRect;
  2. GetWindowRect(hWnd, &wRect);
  3. d3dDevice->SetCursorPosition(
  4.     wRect.left + (CLIENT_WIDTH / 2), 
  5.     wRect.top + (CLIENT_HEIGHT / 2), 
  6.     D3DCURSOR_IMMEDIATE_UPDATE
  7. );
  8.  
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
Jun 16 '07 #1
Share this Question
Share on Google+
3 Replies


ilikepython
Expert 100+
P: 844
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.
Expand|Select|Wrap|Line Numbers
  1. RECT wRect;
  2. GetWindowRect(hWnd, &wRect);
  3. COORD wCoord = {wRect.left + (CLIENT_WIDTH / 2), wRect.top + (CLIENT_HEIGHT / 2)};
  4. SetConsoleCursorPosition(
  5.     GetStdHandle ( STD_OUTPUT_HANDLE ),
  6.     wCoord
  7. );
  8.  
I also tried to use the IDirect3DDevice9::SetCursorPosition method but that also did nothing.
Expand|Select|Wrap|Line Numbers
  1. RECT wRect;
  2. GetWindowRect(hWnd, &wRect);
  3. d3dDevice->SetCursorPosition(
  4.     wRect.left + (CLIENT_WIDTH / 2), 
  5.     wRect.top + (CLIENT_HEIGHT / 2), 
  6.     D3DCURSOR_IMMEDIATE_UPDATE
  7. );
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1. SetCursorPos(CLIENT_WIDTH / 2, CLIENT_HEIGHT / 2);
  2.  
Jun 16 '07 #2

Atli
Expert 5K+
P: 5,058
I'm not sure if this might work but try the SetCusorPos function:
Expand|Select|Wrap|Line Numbers
  1. SetCursorPos(CLIENT_WIDTH / 2, CLIENT_HEIGHT / 2);
  2.  
That works perfectly!

Thanks very much.
Jun 16 '07 #3

ilikepython
Expert 100+
P: 844
That works perfectly!

Thanks very much.
Glad to help .
Jun 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.