Results 1 to 9 of 9

Thread: changing the property of an object in WndProc

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    changing the property of an object in WndProc

    How do change the caption(or any other property) of a button(or any other object) when the window is loaded or painted, etc,. (like in WndProc function)

    The problem is that in WndProc function, I dont have the hwnd property of my button or any other child window. I just a the hwnd of main window
    Baaaaaaaaah

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Well when you create the window (using create windows) the returned value is the hwnd of the window (button), and the second parameter is the window text. This is if you are not using a dialogue. if you are just change the caption like in VB.

    In runtume use the SetWindowText API.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    that is not what I meant!

    OK, I know that I can set the text of a window using "SetWindowText" but if you look at this function:

    SetWindowText(hwnd, "MyText");

    Now the hwnd which I am going to provide is in this call

    RSESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM, WPARAM)

    That call only contains the hwnd of my main window not any child window like "button", "ComboBox", "ListView",....

    I want to change the property OR text of a child window but I dont get hwnd in any of the parameters in the above "WndProc" Call.

    So how do I get the hwnd and then set the property of a child window?
    Baaaaaaaaah

  4. #4
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Cool ..

    Then you create a variable to hold the handle to your control. In WinMain() you create the control and store the returned handle into the varialbe you created. Then you can use this handle(the variable) to modify the caption anywhere in the code
    Does that help?Let me show you a rough example:

    Code:
    HWND button;
    
    WinMain()
    {
         button = CreateWindow(WS_BORDER,.......);
    }
    Now you can use the handle button anywhere
    Amon Ra
    The Power of Learning.

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    I did not get that

    I think that you are talking about messing up with the property in "WinMain()" function. I want to change the property in "WndProc" Call like this:

    [code]

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, LPSTR icmdline, int nShowCmd)

    {


    /* Now it the function parameters, I am just given the hwnd of my main window. I Create the hwnd of my button in "WinMain()" function so I dont have any way of accessing that(that is what i dont know ) Let us that I get a WM_CREATE message like this:*/

    WM_CREATE:


    //Now I want to use the hwnd of my button here but I cannot use
    //it because I have not declared that in this(WinMain()) function
    //so How do I access it and do something like this:


    SetWindowText(hwndofmybutton, "theText");
    Baaaaaaaaah

  6. #6
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Talking exactly...

    Exactly, you have to store the handle you got from using the CreateWindow for your control. Declare a public varialbe and store the control's handle in there. After that you can use the control's handle anywhere.
    AmonRa
    Amon Ra
    The Power of Learning.

  7. #7
    denniswrenn
    Guest
    Here is a working example of how you would do something like that:

    Code:
    #include <windows.h>
    
    char szClassName[ ] = "MyWindow";
    HWND hwndbutton; //global
    HWND hwndwindow; //global
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
    
        MSG messages;  
        WNDCLASSEX wincl;
    
        wincl.hInstance = hInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof(WNDCLASSEX);
    
        wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL; 
        wincl.cbClsExtra = 0;   
        wincl.cbWndExtra = 0;   
        
        wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        
        if(!RegisterClassEx(&wincl)) return 0;
    
        
        hwndwindow = CreateWindowEx(0,szClassName,
               "whatever",
               WS_OVERLAPPEDWINDOW - WS_MAXIMIZEBOX - WS_SIZEBOX | WS_VISIBLE, 
               CW_USEDEFAULT,CW_USEDEFAULT,145,170,HWND_DESKTOP,
               NULL,hInstance,NULL);
    
    	hwndbutton = CreateWindowEx(0, "BUTTON",
    		   "My button", WS_CHILD | WS_VISIBLE,
    		    0, 0, 100, 100, hwndwindow, NULL,
    			hInstance, NULL);
    
    
        while(GetMessage(&messages, NULL, 0, 0))
        {
               TranslateMessage(&messages);
               DispatchMessage(&messages);
        }
    
        return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
               case WM_COMMAND:
    		        if((HWND)lParam == hwndbutton)
                    {
    					SetWindowText(hwndbutton, "Changed");
                    }
    			    break;
               case WM_DESTROY:
    			   PostQuitMessage(0);
                   break;
               default:
               return DefWindowProc(hwnd, message, wParam, lParam);
        }
    
        return 0;
    }

  8. #8

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    thanks a lot!

    That should work now

    So its mean that if you want to use the hwnd anywhere in your program then you declare it as global or public (like outside you functions)
    Baaaaaaaaah

  9. #9
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Cool hrmmm...

    Isn't that what I told you? You have to store the returned ahndle into a public variable, accessable anywhere in your program
    Amon Ra
    Amon Ra
    The Power of Learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width