Thread: put vars in separate function, but need reference

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    put vars in separate function, but need reference

    here is what i'm trying to do. what i want to do, so i dont clutter up the WM_CREATE message in the main window's procedure, is call a function so that it creates and modifies (font, size, placement, etc.) the controls for this main window. see, this function is a one-time call sort of thing, and i'm doing it just for neatness and readability, but i dont know how to logically go about this.

    okay, here is my wndproc:
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static HWND     hwndControls[NUM_OF_CTRLS];
    
        switch(message)
        {
            case WM_CREATE:
                hwndControls = Setup_Controls();
                return 0;
    
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
    
            default:
                break;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    and i want someway to modify (and create) the controls in another function but have the same scope and maneuverability for WnProc. how should i do this?

    thanks!

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    send pointer to the array into the Setup_Controls() function rather than as a return. Send in this pointer in response to a msg for this window.

    You will also have to send in the HWND of the window or dialog, could set this up as part of the array.

    If you edit the resource.h file (or define them youself) and find the controls #define (ie all the edits). You can change the int ID numbers to be consecutive and then use a loop to create, fill and read them.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    alright, i'll give that a try and then get back to you with results.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    i'm having a little trouble getting this to work. i took your advice and just used a pointer to the struct as a param. and i'm writing the function right now, and i just have a couple of questions about some things.

    for instance, in the Setup_Controls() function, it takes the pointer to the struct, and then i have to loop through each CTRLSET struct item to create each control. but, how do i know when the last item has been reached? for instance, i loop with an incrementing variable, i, but how do i test for the last element of the CTRLSET array?

    here is my function so far:
    Code:
    int Setup_Controls(CTRLSET *csCtrls, HWND hParent, HINSTANCE hInstance)
    {
        int     i = 0;
    
        while(csCtrls[i])
        {
            csCtrls[i].hCtrl = CreateWindow(csCtrls[i].szClass,
                                    csCtrls[i].szText, csCtrls[i].iStyle,
                                    csCtrls[i].pPos.x, csCtrls[i].pPos.y,
                                    csCtrls[i].pSize.x, csCtrls[i].pSize.y,
                                    hParent, (HMENU)csCtrls[i].iID, hInstance, NULL);
            i++;
        }
    
        return 0;
    }
    and here's the struct:
    Code:
    typedef struct
    {
        HWND    hCtrl;
        int     iID;
        TCHAR   szClass[33];
        TCHAR   szText[255];
        int     iStyle;
        POINT   pPos;
        POINT   pSize;
    }CTRLSET;
    any idea how to test for the last element in the CTRLSET array? how would i know when i'm done with the loop?

    thanks.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Don't you know how many controls you have? Pass that in as well.

    Test the ID as you should have filled it in and have an idea of the range.


    Find the size of the block of memory with GlobalSize() [may need to GlobalHandle() the block first] first and divide by the sizeof(STRUCTURE)

    iNumRecords=GlobalSize(GlobalHandle(pStruct))/sizeof(STRUCT);

    then test that it is a multiple of the STRUCT

    //there sould be no remainder in this division or the size is wrong
    if( GlobalSize(GlobalHandle(pStruct)) % sizeof(STRUCT) )



    PS Test that the contol has been created. ie CreateWindow() did not return NULL.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM