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.

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;


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 on other sites
Have a look at Evil Steve's response in this thread.

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 on other sites
Thanks all very much! Rating you up!

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634131
• Total Posts
3015725
×