Table of contents
Creating a Custom Icon and Mouse Cursor for an Ogre App MS Windows
Note: Both methods listed below work only on Microsoft Windows. However, if you are targeting the windows platform for a release of your application, they are quite useful. Also, both of the below code fragments should be called only after Ogre has created the default window for your application via mRoot->initialise( ... ).
Icon
Here is a much cleaner and simpler way of setting or changing an icon for a window on the fly:
Note: Make sure you have created an icon resource in Visual Studio, and that the ID is IDI_ICON1 (or just change IDI_ICON1 to whatever you'd like in the below code).
#ifdef WIN32 HWND hwnd; mRenderWindow->getCustomAttribute("WINDOW", &hwnd); HINSTANCE hInst = (HINSTANCE)GetModuleHandle(NULL); SetClassLong (hwnd, GCL_HICON, (LONG)LoadIcon (hInst, MAKEINTRESOURCE (IDI_ICON1))); #endif
Cursor
Very similar code can also be used for changing the mouse cursor for a window:
Note: Make sure you have created a cursor resource in Visual Studio, and that the ID is IDC_POINTER (or just change IDC_POINTER to whatever you'd like in the below code).
#ifdef WIN32 HWND hwnd; mRenderWindow->getCustomAttribute("WINDOW", &hwnd); HINSTANCE hInst = (HINSTANCE)GetModuleHandle(NULL); SetClassLong (hwnd, GCL_HCURSOR, (LONG)LoadCursor (hInst, MAKEINTRESOURCE (IDC_POINTER))); #endif
Note: If you're using OIS, by default your mouse is in exclusive mode, so the cursor is hidden. If you want to show your cursor (which will make your cursor dependent upon Windows), use the following parameters when initialising your OIS parameter list:
paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" ))); paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
Icon & Cursor
Both of the above can also be combined to set both an icon and a cursor with minimal code:
#ifdef WIN32 HWND hwnd; mRenderWindow->getCustomAttribute("WINDOW", &hwnd); HINSTANCE hInst = (HINSTANCE)GetModuleHandle(NULL); SetClassLong (hwnd, GCL_HICON, (LONG)LoadIcon (hInst, MAKEINTRESOURCE (IDI_ICON1))); SetClassLong (hwnd, GCL_HCURSOR, (LONG)LoadCursor (hInst, MAKEINTRESOURCE (IDC_POINTER))); #endif