3

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

ElDorado
  • 448
  • 6
  • 19

1 Answers1

6

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.

James
  • 1,651
  • 2
  • 18
  • 24
chris
  • 60,560
  • 13
  • 143
  • 205
  • 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, What exactly are you changing? – chris Nov 13 '12 at 22:50
  • @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