Skip to main content
GameDev.net gamedev.net
🔒 Locked

Strange cursor problem...

Started by Vortez Nov 25, 2011 at 8:24 PM 0 replies 900+ views
Original Post
Vortez
Vortez
I have a strange bug in my engine, that seem to have appeared from nowhere. Even if my code using it(the engine) does nothing but create a window, the cursor is set to hourglass and stay that way. To correct the bug, i was doing this when creating the window:


WNDCLASSEX wc;
...
wc.hCursor = LoadCursor(NULL, IDC_ARROW);


But, this is bad since it cause the following thing to happen

From msdn:
If your application must set the cursor while it is in a window, make sure the class cursor for the specified window's class is set to NULL. If the class cursor is not NULL, the system restores the class cursor each time the mouse is moved.


My game is a chess game, and i need to set the cursor to hourglass when the ai think, then revert it back to an arrow when done. So, if i use LoadCursor as show above, whenever i move the mouse the cursor revert back to an arrow. So i just make it null, and now it work, but as i said, now when i start my engine the cursor become an hourglass all by itself and won't revert back to arrow unless i call SetCursor... Im pretty sure this wasen't the case with previous version of the engine, and i really can't figure out why it's doing that now. I guess i could call SetCursor after creating the window, but that dosen't seem to be the "right" way to correct the situation. I also checked and im not calling setcursor anywhere in my code, execpt when the ai play...

Im pretty confuse about what could cause this bug... If anyone had any idea what's going on that would be great.

Not sure if that would help, but here's the window creating code, it's a bit extensive though

[font="Courier New"]bool CGameWindow::Create(CInitInfo *pInitInfo, WNDPROC MsgProc)
{
m_hInstance = pInitInfo->hInst;

WNDCLASSEX wc; // Window class object.
// This is the Window class.
wc.cbSize = sizeof(WNDCLASSEX); // size of the WNDCLASSEX structure.
wc.style = CS_HREDRAW | CS_VREDRAW; // style of the window.
wc.lpfnWndProc = MsgProc; // Address to the windows procedure.
wc.cbClsExtra = 0; // Extra class information.
wc.cbWndExtra = 0; // Extra window information.
wc.hInstance = m_hInstance; // Handle of application Instance.
wc.hIcon = pInitInfo->IconID == 0 ? NULL : LoadIcon(m_hInstance, MAKEINTRESOURCE(pInitInfo->IconID));
wc.hIconSm = NULL;
wc.hCursor = NULL/*LoadCursor(NULL, IDC_ARROW)*/; // MUST be set to null
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);/*NULL;*/ // Background color.
wc.lpszMenuName = NULL; // Menu.
wc.lpszClassName = pInitInfo->ClassName; // Name of the window class.
wc.hIconSm = pInitInfo->IconSmID == 0 ? NULL : LoadIcon(m_hInstance, MAKEINTRESOURCE(pInitInfo->IconSmID)); // Minimized window icon.

sprintf(&szCaption[0], &pInitInfo->WindowCaption[0]);
sprintf(&szClassName[0], &pInitInfo->ClassName[0]);

// We must register the class in Windows.
if(RegisterClassEx(&wc))
Registered = true;
else
return false;

FullScreen = pInitInfo->FullScreen;
WideScreen = pInitInfo->WideScreen;

DWORD dwStyle;
if(FullScreen){// Check if we wanted full screen mode
// Set the window properties for full screen mode
dwStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

//Change the display mode if not the same has the current one
if(pInitInfo->Width > 0 && pInitInfo->Height > 0){
DEVMODE DisplayMode;
memset(&DisplayMode, 0, sizeof(DisplayMode));
if(!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &DisplayMode)){
MessageBox(NULL, "EnumDisplaySettings Failed ???", "Error!", 0);
return false;
}
DisplayMode.dmPelsWidth = pInitInfo->Width;
DisplayMode.dmPelsHeight = pInitInfo->Height;
//DisplayMode.dmBitsPerPel = 32;
int ChangeDisplayResult = ChangeDisplaySettings(&DisplayMode, CDS_FULLSCREEN);
if(ChangeDisplayResult != DISP_CHANGE_SUCCESSFUL){
MessageBox(NULL,"Error: Failed to change display mode.", "Vortez3DEngine", 0);
return false;
}
}

ClientRect.left = 0;
ClientRect.top = 0;
ClientRect.right = pInitInfo->Width;
ClientRect.bottom = pInitInfo->Height;

memcpy(&WindowRect, &ClientRect, sizeof(RECT));
} else {
dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
//dwStyle &= ~WS_THICKFRAME;

DEVMODE DisplayMode;
memset(&DisplayMode, 0, sizeof(DisplayMode));
if(!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &DisplayMode)){
MessageBox(NULL,"Error: Failed to change display mode.", "Vortez3DEngine", 0);
return false;
}

int ScrW = DisplayMode.dmPelsWidth;
int ScrH = DisplayMode.dmPelsHeight;

ClientRect.left = 0;
ClientRect.top = 0;

float wRatio = (float)pInitInfo->Width / (float)ScrW;
float hRatio = (float)pInitInfo->Height / (float)ScrH;
if(wRatio >= hRatio){
if(wRatio > 1.0f){
ClientRect.right = (long)((float)pInitInfo->Width / wRatio);
ClientRect.bottom = (long)((float)pInitInfo->Height / wRatio);
} else {
ClientRect.right = pInitInfo->Width;
ClientRect.bottom = pInitInfo->Height;
}
} else {
if(hRatio > 1.0f){
ClientRect.right = (long)((float)pInitInfo->Width / hRatio);
ClientRect.bottom = (long)((float)pInitInfo->Height / hRatio);
} else {
ClientRect.right = pInitInfo->Width;
ClientRect.bottom = pInitInfo->Height;
}
}

memcpy(&WindowRect, &ClientRect, sizeof(RECT));

AdjustWindowRectEx(&ClientRect, dwStyle, 0, 0x00000100);
}

// Create the window.
m_hWnd = CreateWindowEx(NULL, // The extended window style.
GetClassName(), // Window class.
GetCaption(), // Window name.
dwStyle, // Window style.
0,
0, // X, Y coords.
GetWndWidth(),
GetWndHeight(), // Window size.
NULL, // Handle to parent window.
NULL, // Menu.
wc.hInstance, // Handle to app instance.
NULL); // Pointer to window.

// If there was an error with creating the window, then close the program.
if(!m_hWnd)
return false;

// Load the icons manually
if(strlen(&pInitInfo->IconName[0]) > 0){
HICON hIcon = (HICON)LoadImage(NULL, pInitInfo->IconName, IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if(hIcon)
SendMessage(m_hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
}
if(strlen(&pInitInfo->IconSmName[0]) > 0){
HICON hIconSm = (HICON)LoadImage(NULL, pInitInfo->IconSmName, IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(hIconSm)
SendMessage(m_hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
}

// Show the window.and update its display
switch(pInitInfo->StartMaximized)
{
case FALSE: ShowWindow(m_hWnd, SW_SHOW); break;
case TRUE: ShowWindow(m_hWnd, SW_MAXIMIZE); break;
}
UpdateWindow(m_hWnd);

//Confine the cursor to our window
if(FullScreen)
ClipCursor(&WindowRect);

// Sets Keyboard Focus To The Window
SetFocus(m_hWnd);

return true;
}[/font]
Vortez
Vortez
Found it, if you pass null to wc.hCursor, you have to manually call setcursor on a mouse move message, and manage it yourself apparently...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.