南京市车辆异地年检:求一个可以格式化并在显示的函数

来源:百度文库 编辑:神马品牌网 时间:2024/05/08 03:31:51
我希望写一个函数可以实现sprintf的功能
并能在一个messagebox上显示出来
可能是我没说明白
我想定义这样一个函数
mprintf()
参数和sprintf的一样
多出的功能就是在messagebox上显示

void mprintf(char *format,...)
{
va_list args;
char buffer[4096];

va_start(args,format);
vsprintf(buffer,format,args);
va_end(args);

SendMessage(hwndMsgBox,EM_SETSEL,0,-1);
SendMessage(hwndMsgBox,EM_SETSEL,-1,-1);
SendMessage(hwndMsgBox,EM_REPLACESEL,FALSE,(LPARAM)buffer);
}

/*参照微软的代码加了一点点*/
/***
*sprintf.c - print formatted to string
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines sprintf() and _snprintf() - print formatted data to string
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <stdarg.h>
#include <internal.h>
#include <limits.h>
#include <mtdll.h>

#define MAXSTR INT_MAX

/***
*ifndef _COUNT_
*int sprintf(string, format, ...) - print formatted data to string
*else
*int _snprintf(string, cnt, format, ...) - print formatted data to string
*endif
*
*Purpose:
* Prints formatted data to the using the format string to
* format data and getting as many arguments as called for
* Sets up a FILE so file i/o operations can be used, make
* string look like a huge buffer to it, but _flsbuf will
* refuse to flush it if it fills up. Appends '\0' to make
* it a true string. _output does the real work here
*
* Allocate the 'fake' _iob[] entry statically instead of on
* the stack so that other routines can assume that _iob[]
* entries are in are in DGROUP and, thus, are near.
*
*ifdef _COUNT_
* The _snprintf() flavor takes a count argument that is
* the max number of bytes that should be written to the
* user's buffer.
*endif
*
* Multi-thread: (1) Since there is no stream, this routine must
* never try to get the stream lock (i.e., there is no stream
* lock either). (2) Also, since there is only one statically
* allocated 'fake' iob, we must lock/unlock to prevent collisions.
*
*Entry:
* char *string - pointer to place to put output
*ifdef _COUNT_
* size_t count - max number of bytes to put in buffer
*endif
* char *format - format string to control data format/number
* of arguments followed by list of arguments, number and type
* controlled by format string
*
*Exit:
* returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/

#ifndef _COUNT_

int __cdecl sprintf (
bool bMsgBox;/*是否显示提示信息*/
char *string,
const char *format,
...
)
#else /* _COUNT_ */

int __cdecl _snprintf (
bool bMsgBox;/*是否显示提示信息*/
char *string,
size_t count,
const char *format,
...
)
#endif /* _COUNT_ */

{
FILE str;
REG1 FILE *outfile = &str;
va_list arglist;
REG2 int retval;

va_start(arglist, format);

_ASSERTE(string != NULL);
_ASSERTE(format != NULL);

outfile->_flag = _IOWRT|_IOSTRG;
outfile->_ptr = outfile->_base = string;
#ifndef _COUNT_
outfile->_cnt = MAXSTR;
#else /* _COUNT_ */
outfile->_cnt = count;
#endif /* _COUNT_ */

retval = _output(outfile,format,arglist);

_putc_lk('\0',outfile); /* no-lock version */

if(bMsgBox) MessageBox(string); /*实现*/

return(retval);
}

楼主把这个问题想复杂了,不就是要实现定义一个函数形式消息框,参数和 spintf 一样.
下面的伪码:
mprintf(const char* fstr,const char* dstr) //参数和 sprinf的后面两个参数一样
{
char str[200];
sprintf(str,fstr,dstr); //格式化到 str 字符串
MessageBod(str); //将格式化后的字符串形式出来
}

int i = 100;

CString strMSG;
strMSG.Format("变量i的值是:%d", i);
::MessageBox(NULL,strMSG,"Me", MB_OK);//API
//AfxMessageBox(strMSG);

你的问题在下面的网页有解答 “龙车花卉”的很好。
http://faq.173now.com/topic/?20030713-07-2021931.html

在MessageBox上显示就不用写Sprintf了.
你可以将你要显示的内容直接写在MessageBox里.
象MessageBox("输出文字");
如果你想用变量也可以.
CString m_OutPut;
m_OutPut = "输出文字";
MessageBox(m_OutPut);
这样就可以了。

你可以这样实现:先定义一个CString s.然后就用字符串CString 的一个函数:Format(),他的作用和sprintf()作用及用法都一样。然后你在MessageBox()里显示这个s就是了。