Jump to content
• Advertisement

# How do I insert text properly in Win32 API?

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello, i'm doing a time and date function like the one found in notepad, ..for my notepad remake. However it works fine except that when I insert the current time/date it erases all the text already there, and moves the caret to the beginning of that line. I tried to make up an solution by selecting and cutting/pasting what was previously there but only got it working half decent never well enough. So my question is: Does anyone know how I can insert text like this whitout erasing all the previously typed text in the client area? And how to get the caret to the position after the inserted text? (I guess this may be answered by the first quiz..) if it helps understand what I'm trying to do...
// Print current time and date
case ID_EDIT_TIMEDATE :
{
SYSTEMTIME systime;
GetLocalTime(&systime);

hWndEdit = GetDlgItem(hWnd, ID_EDIT);

char szTimeDate[50];
char szHour[5];
char szMinute[5];
char szYear[5];
char szMonth[5];
char szDay[5];

szTimeDate[0] = NULL;

if (systime.wHour > 10)
wsprintf(szHour, "%d", systime.wHour);
else
wsprintf(szHour, "0%d", systime.wHour);

if (systime.wMinute > 10)
wsprintf(szMinute, "%d", systime.wMinute);
else
wsprintf(szMinute, "0%d", systime.wMinute);

wsprintf(szYear, "%d", systime.wYear);

if (systime.wMonth > 9)
wsprintf(szMonth,"%d", systime.wMonth);
else
wsprintf(szMonth,"0%d", systime.wMonth);

if (systime.wDay > 9)
wsprintf(szDay, "%d", systime.wDay);
else
wsprintf(szDay, "0%d", systime.wDay);

lstrcat(szTimeDate, szHour);
lstrcat(szTimeDate, ":");
lstrcat(szTimeDate, szMinute);
lstrcat(szTimeDate, " ");
lstrcat(szTimeDate, szYear);
lstrcat(szTimeDate, "-");
lstrcat(szTimeDate, szMonth);
lstrcat(szTimeDate, "-");
lstrcat(szTimeDate, szDay);

SendMessage(hWndEdit, WM_SETTEXT, 0, (LPARAM)szTimeDate);

SetFocus(hWndEdit);
}
break;

Thanks in advance!

#### Share this post

##### Share on other sites
Advertisement
I think you can only get the current text in the textfield (using GetWindowText), create you new string out of the time and the old content and write it back to the control ...

#### Share this post

##### Share on other sites
Have a look at Evil Steve's response in this thread.

#### Share this post

##### Share on other sites
To insert text into a text box, you need to work with 'Selected Text' instead of regular 'Window Text'

Basically, you set the selected text wherever you want to insert, set the selection length to 0, then set the selected text to your new string:
void AppendText(HWND Control, const char *Text){   //Save current selection   DWORD SelectionStart, SelectionEnd;   SendMessage(Control, EM_GETSEL, &SelectionStart, &SelectionEnd);   //Set new selection   const DWORD TextLength = GetWindowTextLength(Control);   SendMessage(Control, EM_SETSEL, TextLength , TextLength);   //Append Text   SendMessage(Control, EM_REPLACESEL, FALSE, Text);   //Restore previous selection   SendMessage(Control, EM_SETSEL, SelectionStart, SelectionEnd);}

You could easily make an 'insert' variant by adding a parameter controlling where to insert and using that instead of the length of the window's text length.

#### Share this post

##### Share on other sites
Thanks all very much! Rating you up!

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5
• Advertisement

• 9
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632147
• Total Posts
3004445

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!