#include <windows.h>
#include <richedit.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "winlib.h"
char *NewString(char *s)
{
char *tmp;
tmp = (char*)malloc(strlen(s) + 1);
strcpy(tmp, s);
return tmp;
}
int Prompt(HWND hParent, char *caption, char *prompt, int dflt)
{
int r;
UINT flags;
HWND focus;
flags = MB_YESNO | MB_ICONQUESTION;
if (! dflt)
flags |= MB_DEFBUTTON2;
focus = GetFocus();
r = MessageBox(hParent, prompt, caption, flags);
if (focus)
SetFocus(focus);
if (r == IDYES)
return 1;
else
return 0;
}
void MBPrintf(TCHAR *szCaption, TCHAR *szFormat, ...)
{
TCHAR szBuffer [1024] ;
va_list pArgList ;
// The va_start macro (defined in STDARG.H) is usually equivalent to:
// pArgList = (char *) &szFormat + sizeof (szFormat);
va_start(pArgList, szFormat);
// The last argument to wvsprintf points to the arguments
_vsntprintf(szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList);
// The va_end macro just zeroes out pArgList for no good reason
va_end (pArgList);
MessageBox(NULL, szBuffer, szCaption, 0);
}
void EditPrintf(HWND hwndEdit, int maxLen, TCHAR * szFormat, ...)
{
TCHAR szBuffer[1024];
va_list pArgList;
CHARRANGE cr;
int len;
char *tmp;
va_start(pArgList, szFormat);
_vsntprintf(szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList);
va_end(pArgList);
cr.cpMin = -1;
cr.cpMax = -1;
SendMessage(hwndEdit, EM_EXSETSEL, (WPARAM)0, (LPARAM)&cr);
SendMessage(hwndEdit, EM_REPLACESEL, FALSE, (LPARAM) szBuffer);
len = SendMessage(hwndEdit, WM_GETTEXTLENGTH, (WPARAM)0, (LPARAM)0);
if (len > maxLen) {
tmp = (char*)malloc(len + 1);
memset(tmp, 0, len + 1);
SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)len, (LPARAM)tmp);
SendMessage(hwndEdit, WM_SETTEXT, (WPARAM)0, (LPARAM)(tmp + 1000));
free(tmp);
}
SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
}
void ConPrintf(TCHAR * szFormat, ...)
{
TCHAR szBuffer[1024];
va_list pArgList;
DWORD written;
va_start(pArgList, szFormat);
_vsntprintf(szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList);
va_end(pArgList);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), (void*)szBuffer,
(DWORD)strlen(szBuffer), &written, NULL);
}