Help with setting text into LTEXT using Window Handle

This is a discussion on Help with setting text into LTEXT using Window Handle within the Windows Programming forums, part of the Platform Specific Boards category; Hi, need help again: This is in the main function, the last statement being the message sending I want to ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    37

    Question Help with setting text into LTEXT using Window Handle

    Hi, need help again:

    This is in the main function, the last statement being the message sending I want to send to my LTEXT.

    Code:
    case WM_INITDIALOG:{
               HWND handle;
               LRESULT l;
    
               handle = NULL;
               handle = GetDlgItem(hwnd2, ID_S_C);
               if (handle == NULL){
                  MessageBox(hwnd2, "handle == NULL", "........", MB_OK);
               }
               l = SendMessage(handle, CB_ADDSTRING, (WPARAM) 0, (LPARAM) "Name"); /* LPARAM must be char[] string */
               l = SendMessage(handle, CB_SETCURSEL, (WPARAM) 0, (LPARAM) "Name");
               l = SendMessage(handle, CB_ADDSTRING, 0, (LPARAM) "Telephone");
               l = SendMessage(handle, CB_ADDSTRING, 0, (LPARAM) "Email");
               handle = NULL;
               handle = GetDlgItem(hwnd2, ID_GROUP);
               l = SendMessage(handle, CB_ADDSTRING, 0, (LPARAM) "All Contacts");
               l = SendMessage(handle, CB_SETCURSEL, 0, (LPARAM) "All Contacts");
               handle = GetDlgItem(hwnd2, ID_VIEW);
               l = SendMessage(handle, WM_SETTEXT, 0, (LPARAM) "abc");
            }
    The construction of my dialog, the last and only LTEXT being the one I want to set text into.

    Code:
    IDD_FILE DIALOGEX DISCARDABLE  0, 17, 259, 300
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
    FONT 8, "MS Sans Serif"
    BEGIN
        PUSHBUTTON      "&Search", ID_SEARCH, 200, 20, 50, 14
        COMBOBOX        ID_S_C, 13, 20, 40, 100, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
        EDITTEXT        ID_EDIT_FIELD, 60, 20, 133, 13
        PUSHBUTTON      "&Add", ID_ADD, 200, 65, 50, 14
        PUSHBUTTON      "&Edit", ID_EDIT, 200, 82, 50, 14
        PUSHBUTTON      "&Delete", ID_DELETE, 200, 99, 50, 14
        COMBOBOX        ID_GROUP, 13, 42, 60, 100, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
        LISTBOX         ID_LIST, 13, 65, 120, 150, LBS_NOTIFY | WS_BORDER | WS_VSCROLL
        LTEXT           ID_VIEW, 13, 220, 230, 70, SS_WHITERECT | WS_BORDER | SS_LEFT
    END
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    The LTEXT control is normally used to display static text. For example, an edit control used to input a last name would have a LTEXT control to the left of it to display "Last Name" static text. It normally would be assigned IDC_STATIC control id. In this example, the LTEXT in your resource file should look like the following:

    Code:
     LTEXT         "abc",  IDC_STATIC, 13, 220, 230, 70, SS_WHITERECT | WS_BORDER | SS_LEFT
    Use the following if you wish to change the text in LTEXT control:

    Code:
     handle = GetDlgItem(hwnd2, ID_VIEW);
     SetWindowText(handle, "abc");
    Again, to repeat myself, LTEXT is normally used to display static text. That is, text that is not changed by the program.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    37
    Then which resource should I use for text boxes that can only be changed by the program but not the user?

    Many thanks.

    And the code provided somehow doesn't work also... I guess I need to know the proper way to do it.
    Last edited by jasperleeabc; 05-08-2009 at 05:09 AM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by jasperleeabc View Post
    Then which resource should I use for text boxes that can only be changed by the program but not the user?

    Many thanks.

    And the code provided somehow doesn't work also... I guess I need to know the proper way to do it.
    SS_WHITERECT will cause the static to show as a white colored bar without text. Remove the SS_WHITERECT and this should resolve your problem.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    37
    Then is it possible to set the background color of that LTEXT to be white?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by jasperleeabc View Post
    Then is it possible to set the background color of that LTEXT to be white?
    Here's a simple example of one way to set the background color to any color of your choice. This example sets the background color to green.

    Code:
    //{{NO_DEPENDENCIES}}
    // Microsoft Developer Studio generated include file.
    
    #define IDC_EXIT                        1001
    #define IDD_EXAMPLE                     1002
    #define IDC_STATIC_TEXT                 1003
    #define IDC_EDIT1                       1004
    #define IDC_CHANGE                      1005
    
    // Next default values for new objects
    // 
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_NEXT_RESOURCE_VALUE        101
    #define _APS_NEXT_COMMAND_VALUE         40001
    #define _APS_NEXT_CONTROL_VALUE         1003
    #define _APS_NEXT_SYMED_VALUE           101
    #endif
    #endif
    Code:
    //Microsoft Developer Studio generated resource script.
    //
    #include "resource.h"
    
    #define APSTUDIO_READONLY_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 2 resource.
    //
    #define APSTUDIO_HIDDEN_SYMBOLS
    #include "windows.h"
    #undef APSTUDIO_HIDDEN_SYMBOLS
    
    /////////////////////////////////////////////////////////////////////////////
    #undef APSTUDIO_READONLY_SYMBOLS
    
    /////////////////////////////////////////////////////////////////////////////
    // English (U.S.) resources
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
    #ifdef _WIN32
    LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
    #pragma code_page(1252)
    #endif //_WIN32
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Dialog
    //
    
    IDD_EXAMPLE DIALOG DISCARDABLE  0, 0, 122, 59
    STYLE DS_MODALFRAME | DS_3DLOOK | DS_CENTER | WS_POPUP
    FONT 16, "Comic Sans MS"
    BEGIN
        DEFPUSHBUTTON   "Exit",IDC_EXIT,64,34,50,14
    //   LTEXT           "Last Name:",IDC_STATIC_TEXT,11,12,35,8
    //SS_WHITERECT will result the static to show as a white colored bar without text
    //CONTROL       "Last Name:",IDC_STATIC_TEXT, "static",WS_BORDER | SS_LEFT,11,12,35,8, 
    LTEXT           "Last Name:",IDC_STATIC_TEXT,11,12,35,8, WS_BORDER | SS_LEFT
    
        EDITTEXT        IDC_EDIT1,48,11,67,12,ES_AUTOHSCROLL
        DEFPUSHBUTTON   "Change Text",IDC_CHANGE,6,34,50,14
    END
    
    
    #ifdef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // TEXTINCLUDE
    //
    
    1 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "resource.h\0"
    END
    
    2 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
        "#include ""windows.h""\r\n"
        "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
        "#include ""labels.h""\r\n"
        "\0"
    END
    
    3 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "\r\n"
        "\0"
    END
    
    #endif    // APSTUDIO_INVOKED
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // DESIGNINFO
    //
    
    #ifdef APSTUDIO_INVOKED
    GUIDELINES DESIGNINFO DISCARDABLE 
    BEGIN
        IDD_EXAMPLE, DIALOG
        BEGIN
            RIGHTMARGIN, 121
            BOTTOMMARGIN, 57
        END
    END
    #endif    // APSTUDIO_INVOKED
    
    #endif    // English (U.S.) resources
    /////////////////////////////////////////////////////////////////////////////
    
    
    
    #ifndef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 3 resource.
    //
    
    
    /////////////////////////////////////////////////////////////////////////////
    #endif    // not APSTUDIO_INVOKED
    Code:
    #pragma comment(lib, "user32.lib")
    #pragma comment(lib, "gdi32.lib")
    #include <windows.h>
    #include "resource.h"
    
    BOOL CALLBACK MyExample(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static BOOL bSwap = TRUE;
    	HWND handle;
    	switch (message) {
    		case WM_INITDIALOG:
    			SetBkColor((HDC) wParam, RGB(0,255,0) );
    			return (int)CreateSolidBrush(RGB(0,255,0));
    		case WM_CTLCOLORSTATIC:
    			{
    				if(GetDlgItem(hDlg, IDC_STATIC_TEXT) == (HWND)lParam)
    				{
    					SetBkColor((HDC) wParam, RGB(0,255,0) );
    					return (int)CreateSolidBrush(RGB(0,255,0));
    				}
    				return 0;
    			}
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case IDC_EXIT:
    					EndDialog(hDlg, TRUE);
    					break;
    				case IDC_CHANGE:
    					handle = GetDlgItem(hDlg, IDC_STATIC_TEXT);
    					if(bSwap)
    						SetWindowText(handle , "abc");
    					else
    						SetWindowText(handle , "Last Name:");   
    					bSwap = !bSwap;
    					break;
    			}
    			break;
    	}
    	return FALSE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,
    	   HINSTANCE hPrevInstance,
    	   LPSTR lpszArgs,
    	   int nWinMode)
    {
    	DialogBox(hInstance ,MAKEINTRESOURCE(IDD_EXAMPLE) , 0, MyExample);
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    37
    Many thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  4. Grabbing Text from a Window
    By jimothygu in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2003, 07:29 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21