On Wed, 13 Apr 2005 18:27:10 -0400, "Steve N."
<St****@discussions.microsoft.com> wrote:
In converting a console app to windows, I'd like to emulate the console
(stdout) output into a textbox. I am wondering how I can easily
translate the c style format strings into .NET format strings. It looks
like one can do it by an ICustomFormatter interface, but I'm wondering
if there is a quick a dirty way of doing it, as I'm not sure I will want
to keep it in the long run.
Or is there something already implemented that I just don't know about?
Using C or C++ and the Win32 API, it's quite easy; vsprintf() to a
string and append that to an editbox, static or listbox. Sorry, I
don't know what's involved in calling this from managed code.
Untested example, using the safe TCHAR version of vsprintf and output
to an edit box.
#include <windows.h>
#include <tchar.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
int SevEditPrintf(HWND hedit, LPCTSTR fmt, ...)
{
int n;
TCHAR buf[1000];
va_list vargs;
va_start(vargs, fmt);
n = _vsntprintf(buf, sizeof buf, fmt, vargs);
/* Move caret to end of text, no selection */
SendMessage(hedit, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
/* Append the new data */
SendMessage(hedit, EM_REPLACESEL, FALSE, (LPARAM)buf);
/* Be sure it's scrolled into view */
SendMessage(hedit, EM_SCROLLCARET, 0, 0);
return n;
}
--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real