Thread: Reset static control's text ?

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    Reset static control's text ?

    Hey, i'd like to know how to reset a static control's text to nothing. when i declare the control, i have nothing in it. when a menu item is clicked, text gets sent to the controls. then when a different menu item is clicked, if the previous text is longer then the new text, you see the new text + the remainder of the previous text. thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Would the following not serve your purpose?

    Code:
    // hwndStatic is your static control
    SetWindowText( hwndStatic, "" );

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    thanks but I don't think so.. that just makes the title bar of my window go to nothing. :-\ lol
    this is one of my control's:
    Code:
                 CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_SIMPLE, 
                 160, 8, 150, 23, hwnd, (HMENU)ID_LABEL1, g_hInst, NULL);
    then when you click on a menu item:
    Code:
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)fahr_to_cels);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempcel);
    gets sent to the control.
    but then when something else gets sent to the same control, it still shows text from ^ that as its the longest string.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Quote Originally Posted by willc0de4food
    thanks but I don't think so.. that just makes the title bar of my window go to nothing.
    Make sure you pass the handle of your static control, not the handle of your main window.

    Alternatively, try SetDlgItemText.

    Code:
    SetDlgItemText( hwnd, ID_LABEL1, "");

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    no dice. i have no clue why.. here's more code.
    Code:
    ...
    
    void UncheckMenu(HWND hwnd)
    {
         int menu = 0, submenu = 0;
         char holder[3] = {0};
         
         /* check to see if a menu item is already checked. if it is, uncheck it. */
         if (menucheck[0] == 49)
         {
             holder[0] = menucheck[1];
             menu = atoi(holder);
             holder[0] = menucheck[2];
             submenu = atoi(holder);
             
             if (menu == 1)
             {  
                 if (submenu == 1)
                 {   CheckMenuItem(GetMenu(hwnd), ID_UNITS_FTOC, MF_UNCHECKED);   }
                 else if (submenu == 2)
                 {    CheckMenuItem(GetMenu(hwnd), ID_UNITS_FTOK, MF_UNCHECKED);  }
                 else
                 {    MessageBox(hwnd, "An error occured in the menu.\nPlease restart the application.", "Error", MB_OK | MB_ICONERROR);   }
             }
             else if (menu == 2)
             {
                  if (submenu == 1)
                 {   CheckMenuItem(GetMenu(hwnd), ID_UNITS_CTOF, MF_UNCHECKED);   }
                 else if (submenu == 2)
                 {    CheckMenuItem(GetMenu(hwnd), ID_UNITS_CTOK, MF_UNCHECKED);  }
                 else
                 {    MessageBox(hwnd, "An error occured in the menu.\nPlease restart the application.", "Error", MB_OK | MB_ICONERROR);   }
             }
             else if (menu == 3)
             {
                  if (submenu == 1)
                 {   CheckMenuItem(GetMenu(hwnd), ID_UNITS_KTOF, MF_UNCHECKED);   }
                 else if (submenu == 2)
                 {    CheckMenuItem(GetMenu(hwnd), ID_UNITS_KTOC, MF_UNCHECKED);  }
                 else
                 {    MessageBox(hwnd, "An error occured in the menu.\nPlease restart the application.", "Error", MB_OK | MB_ICONERROR);   }
             }
             else
             {    MessageBox(hwnd, "An error occured in the menu.\nPlease restart the application.", "Error", MB_OK | MB_ICONERROR);   }
         }
    }
    
    /* This function sets the labels so the user knows whats going on */
    void SetLabel(HWND hwnd, int from, int to)
    {
         TCHAR fahr_to_cels[22] = "Fahrenheit to Celsius", cels_to_fahr[22] = "Celsius to Fahrenheit", fahr_to_kelv[22] = "Fahrenheit to Kelvin", kelv_to_fahr[22] = "Kelvin to Fahrenheit", cels_to_kelv[24] = "Celsius to Kelvin", kelv_to_cels[24] = "Kelvin to Celsius";
         TCHAR tempfahr[19] = "Temp in Fahrenheit", tempcel[20] = "Temp in Celsius", tempkel[20] = "Temp in Kelvin";
         
         UncheckMenu(hwnd);
         
         /*SetWindowText((HWND)ID_LABEL1, "");
         SetWindowText((HWND)ID_LABEL2, "");
         SetWindowText((HWND)ID_LABEL4, "");*/
         SetDlgItemText(hwnd, ID_LABEL1, "");
         SetDlgItemText(hwnd, ID_LABEL2, "");
         SetDlgItemText(hwnd, ID_LABEL4, "");
         
         /* 1 = fahrenheit, 2 = celsius, 3 = kelvin. */
         if (from == 1)
         {
             SendDlgItemMessage(hwnd, ID_LABEL2, WM_SETTEXT, 0, (LPARAM)tempfahr);
             if (to == 1)
             {
                 CheckMenuItem(GetMenu(hwnd), ID_UNITS_FTOC, MF_CHECKED);
                 menucheck[0] = 49;
                 menucheck[1] = 49;
                 menucheck[2] = 49;
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)fahr_to_cels);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempcel);
             }
             else if (to == 2)
             {
                 CheckMenuItem(GetMenu(hwnd), ID_UNITS_FTOK, MF_CHECKED);
                 menucheck[0] = 49;
                 menucheck[1] = 49;
                 menucheck[2] = 50;
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)fahr_to_kelv);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempkel);
             }
             else { MessageBox(hwnd, "There was an error receiving your request.\nPlease restart the application.", "Error!", MB_OK | MB_ICONERROR); }
         }
         else if (from == 2)
         {
             SendDlgItemMessage(hwnd, ID_LABEL2, WM_SETTEXT, 0, (LPARAM)tempcel);
             if (to == 1)
             {
                 CheckMenuItem(GetMenu(hwnd), ID_UNITS_CTOF, MF_CHECKED);
                 menucheck[0] = 49;
                 menucheck[1] = 50;
                 menucheck[2] = 49;
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)cels_to_fahr);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempfahr);
             }
             else if (to == 2)
             {
                 CheckMenuItem(GetMenu(hwnd), ID_UNITS_CTOK, MF_CHECKED);
                 menucheck[0] = 49;
                 menucheck[1] = 50;
                 menucheck[2] = 50;
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)cels_to_kelv);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempkel);
             }
             else { MessageBox(hwnd, "There was an error receiving your request.\nPlease restart the application.", "Error!", MB_OK | MB_ICONERROR); }
         }
         else if (from == 3)
         {
             SendDlgItemMessage(hwnd, ID_LABEL2, WM_SETTEXT, 0, (LPARAM)tempkel);
             if (to == 1)
             {
                 CheckMenuItem(GetMenu(hwnd), ID_UNITS_KTOF, MF_CHECKED);
                 menucheck[0] = 49;
                 menucheck[1] = 51;
                 menucheck[2] = 49;
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)kelv_to_fahr);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempfahr);
             }
             else if (to == 2)
             {
                 CheckMenuItem(GetMenu(hwnd), ID_UNITS_KTOC, MF_CHECKED);
                 menucheck[0] = 49;
                 menucheck[1] = 51;
                 menucheck[2] = 50;
                 SendDlgItemMessage(hwnd, ID_LABEL1, WM_SETTEXT, 0, (LPARAM)kelv_to_cels);
                 SendDlgItemMessage(hwnd, ID_LABEL4, WM_SETTEXT, 0, (LPARAM)tempcel);
             }
             else { MessageBox(hwnd, "There was an error receiving your request.\nPlease restart the application.", "Error!", MB_OK | MB_ICONERROR); }
         }
         else
         {
             MessageBox(hwnd, "There was an error receiving your request.\nPlease restart the application.", "Error!", MB_OK | MB_ICONERROR);
         }
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                 CreateWindowEx(0, TEXT("STATIC"), TEXT("Currently converting: "), WS_CHILD | WS_VISIBLE | SS_SIMPLE, 
                 23, 8, 134, 23, hwnd, (HMENU)ID_TEXT1, g_hInst, NULL);
                 
                 CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_SIMPLE, 
                 160, 8, 150, 23, hwnd, (HMENU)ID_LABEL1, g_hInst, NULL);
                 
                 CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_SIMPLE, 
                 0, 104, 130, 23, hwnd, (HMENU)ID_LABEL2, g_hInst, NULL);
                 
                 CreateWindowEx (WS_EX_CLIENTEDGE,                    /* more or 'extended' styles */
                                 TEXT("EDIT"),                        /* 'class' of control to create */
                                 TEXT(""),                            /* text displayed in the control */
                                 WS_CHILD | WS_VISIBLE | WS_BORDER,   /* how it looks */
                                 131,                                 /* control position: left */
                                 104,                                 /* control position: top */
                                 100,                                 /* control width */
                                 20,                                  /* control height */
                                 hwnd,                                /* parent window handle */
                                 (HMENU)ID_LABEL3,                                /* control's ID */
                                 g_hInst,                           /* application instance */
                                 NULL);
                                 
                 CreateWindowEx (0, TEXT("BUTTON"), TEXT("Convert"), 
                 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 130, 144, 104, 23, hwnd, (HMENU)ID_TEXT2, g_hInst, NULL);
                 
                 CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_SIMPLE, 
                 0, 208, 130, 23, hwnd, (HMENU)ID_LABEL4, g_hInst, NULL);
                 
                 CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER, 
                 131, 208, 100, 20, hwnd, (HMENU)ID_LABEL5, g_hInst, NULL);
                 
                 CreateWindowEx(0, TEXT("BUTTON"), TEXT("Clear"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
                 130, 248, 104, 23, hwnd, (HMENU)ID_TEXT3, g_hInst, NULL);
                 
                 CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | SS_SIMPLE, 272, 8, 0, 16, 
                 hwnd, (HMENU)ID_LABEL6, g_hInst, NULL);
                 
                 return 0;
            break;
            case WM_COMMAND:
                 switch(LOWORD(wParam))
                 {
                     case ID_FILE_CLOSE:
                          PostMessage(hwnd, WM_CLOSE, 0, 0);
                     break;
                     case ID_UNITS_FTOC:
                          SetLabel(hwnd, one, one);
                     break;
                     case ID_UNITS_FTOK:
                          SetLabel(hwnd, one, two);
                     break;
                     case ID_UNITS_CTOF:
                          SetLabel(hwnd, two, one);
                     break;
                     case ID_UNITS_CTOK:
                          SetLabel(hwnd, two, two);
                     break;
                     case ID_UNITS_KTOF:
                          SetLabel(hwnd, three, one);
                     break;
                     case ID_UNITS_KTOC:
                          SetLabel(hwnd, three, two);
                     break;
                     case ID_HELP_ABOUT:
                          MessageBox(hwnd, "Temperature Converter 1.0a; Shibby Inc. © 2005", 
                          "About...", MB_OK | MB_ICONINFORMATION);
                     break;
                     case ID_TEXT2:
                          MessageBox(hwnd, "booyah", "8-)", MB_OK);
                     break;
                     case ID_TEXT3:
                          UncheckMenu(hwnd);
                          SetDlgItemText(hwnd, ID_LABEL3, "");
                          SetDlgItemText(hwnd, ID_LABEL5, "");
                     break;            
                     }
            break;
    
    ...
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    what you need to do is call InvalidateRect(hwnd,0,TRUE) after your call to set the text.

    You can play around with the 2nd parameter using only the rect of the static control to make it a bit more effiencient, but the above should work to refresh the entire window

    edit: change true to TRUE, forgot we are dealing with windows here, compiler will complain about the true to TRUE conversion
    Last edited by Darryl; 08-10-2005 at 01:18 PM.

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    you...are freaking awesome.

    i would almost tell you i love you...but i thatd just be awkward.
    lol

    that works!
    Registered Linux User #380033. Be counted: http://counter.li.org

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    mmm...so, now how would i make my controls start off being disabled until an option in the menu is clicked? then once the option in the menu is clicked, it enables the controls so that the user can enter data and click the buttons?

    thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  9. #9
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Nice one, Darryl.
    I would never have guessed InvalidateRect was the problem.

    so, now how would i make my controls start off being disabled until an option in the menu is clicked? then once the option in the menu is clicked, it enables the controls so that the user can enter data and click the buttons?
    I think EnableWindow should do the trick.

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Dante Shamest
    Nice one, Darryl.
    I would never have guessed InvalidateRect was the problem.



    I think EnableWindow should do the trick.
    The clue was that when he wrote new text, he was still seeing part of the old text.

  11. #11
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    EnableWindow isn't doing it for me, unless i'm using it wrong. i've tried passing hwnd for the first parameter, and (HWND)ID_TEXT2 and the first one completely freezes the window. no alt+f4, no right clicking on it in the taskbar to close it...u must alt+ctrl+del. the second method - does nothing. lol so do am i supposed to add it in a specific area? or am i doing something wrong? thanks for the help.
    Registered Linux User #380033. Be counted: http://counter.li.org

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by willc0de4food
    EnableWindow isn't doing it for me, unless i'm using it wrong. i've tried passing hwnd for the first parameter, and (HWND)ID_TEXT2 and the first one completely freezes the window. no alt+f4, no right clicking on it in the taskbar to close it...u must alt+ctrl+del. the second method - does nothing. lol so do am i supposed to add it in a specific area? or am i doing something wrong? thanks for the help.
    You need to pass the handle to the control, not the handle to the window. (HWND)ID_TEXT2 is just plain wrong, you can't cast the resourceid to be the handle to the control. There's 2 ways you can go about this, first when you call CreateWindowEx you could save the return value, that is the HWND you need:

    Code:
    HWND myControl = CreateWindowEx(...)
    second you can call GetDlgItem with the resource_id and get the handle

    Code:
    HWND myControl = GetDlgItem(hwnd, ID_TEXT2)
    then call enablewindow

    Code:
    EnableWindow( myControl, FALSE);
    EDIT: I guess you avoid the myControl variable by nesting:
    Code:
    EnableWindow( GetDlgItem(hwnd, ID_TEXT2), FALSE);
    Last edited by Darryl; 08-11-2005 at 07:56 AM.

  13. #13
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    freakin sweet

    i built a new computer because i sold my previous one, but in the mean time i haven't been able to work on my program but i'm going at it now and that works like a charm thanks.
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  3. static text won't change
    By lambs4 in forum Windows Programming
    Replies: 4
    Last Post: 03-18-2003, 07:50 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. Problem with static text in dialog boxes
    By Clyde in forum Windows Programming
    Replies: 11
    Last Post: 05-28-2002, 12:51 PM