Thread: Adding text where cursor is?

  1. #1
    Registered User
    Join Date
    Apr 2002

    Adding text where cursor is?

    I am working on a simple HTML editor with a styles wizard. I know how to generate the text but I can only figure out how to add the text to the end. I want to insert it wherever the cursor is but dont know how. This is what I have so far:

    case IDOK:{
    int len = GetWindowTextLength(GetDlgItem(hWnd, IN_HOVERCOLOR));
    char* ret_color;
    ret_color = (char*)GlobalAlloc(GPTR, len + 1);
    GetDlgItemText(hWnd, IN_HOVERCOLOR, ret_color, 16);
    //ret_color now has value of IN_HOVERCOLOR//
    HWND hwndpar;
    hwndpar = GetWindow(hWnd,GW_OWNER);
    HWND hEdit;
    hEdit = GetDlgItem(hwndpar, MAIN_EDIT);
    char* before_text;
    int txtlen = GetWindowTextLength(hEdit);
    before_text = (char*)GlobalAlloc(GPTR, txtlen + 75);
    GetWindowText(hEdit, before_text, txtlen+1);
    char style_text[75];
    strcat(style_text,";}\r\n-->\r\n</style>"); //22 len
    //Style Text now has the style tag//
    strcat(before_text, style_text);
    SetWindowText(hEdit, before_text);
    //Free up memory//
    EndDialog(hWnd, wCommand);

  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    Most times the cursor will be at the end of the text. Could look at the WM_COMMAND messages for the edit.

    case ID_EDIT:
    if (EN_CHANGE == HIWORD(wParam)) //user is typing text so cursor is at end

    or play with SendMessage() and EM_GETSEL (could work?)

    I think you are going to have some size errors here if length ret_color is about 30 or more. Alloc some more mem here

    strcat(style_text,";}\r\n-->\r\n</style>"); //22 len
    better would be
    char *before_text, *edit_Text;
    int     iLen=0;
    iLen=  lstrlen( "<style>\r\n<!--\r\na:hover{color:" ) 
           + lstrlen( ";}\r\n-->\r\n</style>" ) 
           + lstrlen(ret_color) 
           + GetWindowTextLength( hEdit );
    //you should always error test the mem allocs
    edit_Text     = (char*)GlobalAlloc(GPTR, GetWindowTextLength ( hEdit ) + 1);
    before_text = (char*)GlobalAlloc(GPTR, ilen + 1);
    //use one sprintf with a %s type where the other string are added
    sprintf(before_text,"%s<style>\r\n<!--\r\na:hover{color:%s;}\r\n-->\r\n</style>" , before_text, ret_color);
    SetWindowText(hEdit, before_text);
    //free mem

  3. #3
    Registered User
    Join Date
    Apr 2002
    Since this is an HTML editor i decided to make a function to add text to a edit box in any window.

    BOOL InsertText(HWND hnd, INT id, LPSTR txttoins2){
    BOOL bSuccess = FALSE;
    HWND newHandle = GetDlgItem(hnd, id);
    int LenOLD = GetWindowTextLength(newHandle);
    char* txttoins, *OLDTEXT;
    txttoins = (char*)GlobalAlloc(GPTR, strlen(txttoins2)+1);
    strcpy(txttoins, txttoins2);
    OLDTEXT = (char*)GlobalAlloc(GPTR, LenOLD+strlen(txttoins2)+1);
    GetWindowText(newHandle, OLDTEXT, LenOLD+1);
    strcat(OLDTEXT, txttoins);
    if (SetWindowText(newHandle, OLDTEXT)){
    return bSuccess;
    My old code was very sloppy but I am a newbie. Is this one any better? Another question is how do I set up a menu to copy,paste,select text in hEdit (handle: hwnd). Do I have to use clipboard functions or is there an easier way to do it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining the Date of the [nth] [Day] in [Month]
    By Dave_Sinkula in forum C Programming
    Replies: 5
    Last Post: 05-25-2007, 05:12 PM
  2. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  3. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 09:54 AM
  4. Adding Text to an Edit
    By akrocks in forum Windows Programming
    Replies: 9
    Last Post: 03-17-2004, 11:45 AM
  5. Replies: 1
    Last Post: 07-13-2002, 06:45 PM
Website Security Test