Thread: Custom control information

  1. #1

    Custom control information

    What's the best way for me to keep 'information' with an instance of my custom control? Like at the moment, my controls have a class created on the WM_CREATE message and then I use SetWindowLong() to have a pointer to it. However, this makes the GWL_USERDATA useless for anything else ... and you can set it on the standard windows controls so that got me thinking that windows does it differently!

    Now, the idea I'm working on now is to have an ever expanding array of a struct which uses the id and hwnd of a control as a unique id to store the class instance.. This way I can just search the array for the id and hwnd that match the control I'm painting and then retrieve the class pointer ...

    typedef struct {
       HWND hwnd;
       int id;
       LONG *ptr;
    That would be the struct, or at least something similar to that. And then on the WM_PAINT message I'd just search the array looking for the matching struct members and then typecast the long pointer to the class type.

    And this would be entered into the dynamic array after the class instance is instatiated.

    Is this feasable??

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Window memory:

    SetWindowLong documentation:

    Reserve extra window memory by specifying a nonzero value in the cbWndExtra member of the WNDCLASSEX structure used with the RegisterClassEx function.

    nIndex - Specifies the zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer.
    See example:

    Use SetWindowLongPtr for pointers.

    You can also use SetProp() but window memory is far safer and more efficient.

  3. #3
    So SetProp allows me to say set the property "WindowClass" or whatever I want to a value??

    If this is true I am forever in your debt!! You will have increased my productivity and reduced my headaches exponentially!!

  4. #4
    It works!! You legend!! I have so many problems I can solve now because of you!! No more pointer related errors and crashes!

    I can't thank you enough lol, ...


  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    I wouldn't use SetProp()! Window memory, as used in the example, is far better.

  6. #6
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    My preferred way is to fill up a whole new WNDCLASS structure and register it with the system. I increase the size of the cbWndExtra member and store the pointer to my wrapper class in there.

    If you want to make use of an existing class (e.g. BUTTON), use the GetClassInfo[Ex]() function to fill up the WNDCLASS structure.

    Then set the size of the cbWndExtra member to the sum of the default cbWndExtra size and the size of your pointer.

    Good article on custom controls.
    Last edited by Dante Shamest; 03-02-2004 at 06:43 AM.

  7. #7
    What is actually wrong with setprop??

    Ok, well althoug SetProp and GetProp are much cleaner and easier ... That catch22 article actually showed me a better way to get around my problem.

    My problem being that I didn't want to use the GWL_USERDATA because other people who use my control might want to use that space!
    Last edited by Mithoric; 03-02-2004 at 07:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom rightclick menu on a control?
    By Viper187 in forum Windows Programming
    Replies: 18
    Last Post: 10-10-2008, 08:55 AM
  2. Spy++ view messages posted/sent to a control, or from it?
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 06-24-2007, 11:07 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Superclassed edit control; custom notifications
    By Boksha in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2006, 03:21 PM
  5. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM