HWND hEdit1 = GetDlgItem(g_hWnd, IDC_MAIN_EDIT); // get string lengths int selStart = GetWindowTextLength(hEdit1); int selEnd = selStart + strlen(buffer) + 8; // this calls the function to insert data before formating insertMessage((HWND) hWnd, buffer); CHARRANGE cr; cr.cpMin = selStart; cr.cpMax = selEnd; SendMessage(hEdit1, EM_EXSETSEL, 0, (LPARAM)&cr); CHARFORMAT2 cf; memset( &cf, 0, sizeof cf ); cf.cbSize = sizeof(cf); cf.dwMask = CFM_COLOR; cf.crTextColor = RGB(255,0,0); SendMessage(hEdit1, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);