Jason
Jason

Reputation: 3307

How to print many format strings into window?

After successfully solve this problem how to print a format string into window, another problem comes to me.

If there are many format strings, how to print them into window? For example below:

sprintf(buf, formatString-1...);
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)buf);
...
sprintf(buf, formatString-2...);
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)buf);
...
sprintf(buf, formatString-3...);
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)buf);
...

Notice that only formatString-3 is printed into window, while i want to put them all into window. How to do this?(PS: Please Do not use buf concatenate) Thank you!~

Upvotes: 0

Views: 239

Answers (2)

BrendanMcK
BrendanMcK

Reputation: 14498

Are you trying to produce a console-style or log-style window, with multiple lines of text, one after the other?

If so, simplest approach is to pick a control that will do this for you. Something like a static (usually used for labels) typically is only useful for one string at a time. If you want to display more than one line of output, your two main options are:

  • Listbox control: add items to the end using LB_ADDSTRING. (You may want to follow that with LB_SETCURSEL or similar to select the last item, so that as items are added to the end, it will scroll to show the last item.)

  • Read-only Multi-line Edit control: append text to the end using the technique outlined here on MSDN. Note that with this approach, you need to supply the "\r\n" yourself to create a new line.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612964

Each WM_SETTEXT message overwrites the previous one. That's why you only observe the effects of the final message.

Although you state that you don't want to concatenate the buffer before sending the WM_SETTEXT message, that's the only option with WM_SETTEXT.

If you have an edit control then you can insert text using the EM_REPLACESEL message.

Upvotes: 2

Related Questions