Advertisement Jump to content
Sign in to follow this  

How do I insert text properly in Win32 API?

This topic is 4992 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
					SYSTEMTIME 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);
						wsprintf(szHour, "0%d", systime.wHour);

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

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

					if (systime.wMonth > 9)
						wsprintf(szMonth,"%d", systime.wMonth);
						wsprintf(szMonth,"0%d", systime.wMonth);
					if (systime.wDay > 9)
						wsprintf(szDay, "%d", systime.wDay);
						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);

Thanks in advance!

Share this post

Link to post
Share on other sites
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

Link to 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

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!