I'm working now with playground SDK and need to get WNDCLASS of my game window. I haven't found anything in SDK, thats why I'm trying to do this with hWnd of game window. So is there any way to get WNDCLASS from HWND? I need this to change system cursor in game window
-
1Just curious, but why can't you keep track of the data you passed into `RegisterClass`? – Adam Rosenfield Nov 13 '12 at 21:57
-
@AdamRosenfield playground is a cross platform SDK for casual games and it creates the window. – IronMensan Nov 13 '12 at 22:03
-
@ElDorado, Just wondering, as there's not often a need to access the class, what are you doing with it? – chris Nov 13 '12 at 22:04
-
@chris I have no idea what the OP intends to do with the class. – IronMensan Nov 13 '12 at 22:05
-
@IronMensan, Oops, wrong person :p – chris Nov 13 '12 at 22:05
-
@chris Well, lets say, I just NEED this class ) – ElDorado Nov 13 '12 at 22:06
-
similar question: http://stackoverflow.com/questions/41252574/can-wndclassexlpszclassname-be-set-in-an-mfc-app-cmainframe – user2457260 Dec 21 '16 at 11:15
-
similar question: http://stackoverflow.com/questions/41252574/can-wndclassexlpszclassname-be-set-in-an-mfc-app-cmainframe – user2457260 Dec 21 '16 at 11:16
1 Answers
I don't know about the SDK in question, but as long as it provides access to the native HWND
type, you can use native calls.
To change the cursor for all windows of that class:
Use the SetClassLongPtr
function:
SetClassLongPtr(hwnd, GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(newCursorHandle));
To change the cursor for just the game window:
First of all, there is a WM_SETCURSOR
message that you can handle to take control of what cursor is shown in the window. You can read more about that in Adam Rosenfield's comment below.
That aside, there is an alternative: As per the SetCursor
documentation, first make sure the class's cursor is set to nothing (NULL
). Then you can use the SetCursor
function when the mouse enters and leaves the client area. To not interfere with other windows in the class, be sure to set the class cursor to NULL on mouse entry and set it back to what it was on mouse exit.
otherCursor = SetCursor(otherCursor);
To get the read-only WNDCLASSEX associated with a window:
First, use GetClassName
to get the name of the class associated with the window:
std::array<TCHAR, 256> className; //256 is max classname length
GetClassName(hwnd, className.data(), className.size());
Then, use that in a call to GetClassInfoEx
:
WNDCLASSEX wce;
GetClassInfoEx(GetModuleHandle(nullptr), className.data(), &wce);
Now you can access wce
to read the contents of the class structure. If you need to, you can replace std::array
with std::vector
and .data()
with &className[0]
, as well as nullptr
with NULL
. GetClassInfo
will return a WNDCLASS
if you need that instead of WNDCLASSEX
.
-
if I've changed some attributes of WNDCLASS, how must I save it? Something like RegisterClassEx? I've tryed only to change them but it doesn't seems to work right (at least this attribute doesn't saved) – ElDorado Nov 13 '12 at 22:31
-
-
@ElDorado, I've updated my answer to reflect the different possibilities of what one could need. – chris Nov 13 '12 at 23:03
-
Doesn't seem to work. Looks like Playground is changing cursor back to system. I've tryed to set .hCursor to NULL due to [this](http://msdn.microsoft.com/en-us/library/ms648393(v=vs.85).aspx) info. Looks like it worth new post, because this way doesn't work. Anyway thanks a lot, I've learned something out of Your replies – ElDorado Nov 13 '12 at 23:10
-
@ElDorado, It looks to me like this question/answer might benefit some people in the future who come wanting the `WNDCLASS` for whatever reason. That's always a plus. – chris Nov 13 '12 at 23:18
-
2@ElDorado: If you to change a window's cursor, you'd be better off overriding its window procedure's [`WM_SETCURSOR`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms648382%28v=vs.85%29.aspx) message handler. If you can't change the window procedure, you can [subclass it](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633570%28v=vs.85%29.aspx#subclassing_window) instead. – Adam Rosenfield Nov 14 '12 at 18:29
-
@AdamRosenfield, I forgot about that option. Thank you very much for bringing it up. – chris Nov 14 '12 at 20:27
-
@Adam Rosenfield Thanks a lot! That was the only working wolution for me! – ElDorado Nov 16 '12 at 22:08