有谁知道wsprintf吗? (10分)

小唐

Unregistered / Unconfirmed
GUEST, unregistred user!
上次我问了关于wsprintf函数的用法的问题,得到了如下答案:<br>var<br>&nbsp; name : string; &nbsp;//人的姓名<br>&nbsp; age : integer; &nbsp;//人的年龄<br>&nbsp; str: PChar;<br>begin<br>&nbsp; name :='张三';<br>&nbsp; age :=23;<br>&nbsp; GetMem(str, 255);<br>&nbsp; wsprintf(str, PChar(Format('%s的年龄是%d岁', [name, age])));<br>&nbsp; messagebox(0,str,'标题',0);<br>&nbsp; FreeMem(str);<br>end;<br><br>现在的问题是:虽然上面的程序可以运行,可是Format函数是在sysutils单元中定义的,<br>我想用API编小巧的程序,同时学习API函数。所以不能在我的程序中添加除了windows.pas<br>和messages.pas两个单元以外的其它单元。这样一来,上面的这句:<br>&nbsp; wsprintf(str, PChar(Format('%s的年龄是%d岁', [name, age])));<br>中的format函数就不能用了。可是不用format函数,又有什么其它的办法将name和age<br>这两个不同类型的变量合并为一个字符串呢?
 
你可以用wvsprintf<br>function AllocMem(Size: Cardinal): Pointer;<br>begin<br>&nbsp; GetMem(Result, Size);<br>&nbsp; FillChar(Result^, Size, 0);<br>end;<br>procedure Ex;<br>var<br>&nbsp; Buf: pChar;<br>&nbsp; va_list: array [0..1] of longint;<br>&nbsp; Str: String;<br>begin<br>&nbsp; Str:= 'Delphi';<br>&nbsp; va_list[0] := longint( Pchar(Str));<br>&nbsp; va_list[1] := 1234;<br>&nbsp; Buf := allocMem(10);<br>&nbsp; wvsprintf(Buf, 'Str: %s, v: %i', @va_list );<br>&nbsp; MessageBox(0, Buf, 'Example', mb_OK);<br>&nbsp; FreeMem(Buf, 20);<br>end;<br>//---------------------------------------------------------------<br>也可以试试用这个:(别人写的)<br>function _wsprintf(lpOut: PChar; lpFmt: PChar; lpVars: Array of Const):<br>Integer; assembler;<br>var<br>&nbsp; Count: integer;<br>&nbsp; v1, v2: integer;<br>asm<br>&nbsp; mov v1, eax<br>&nbsp; mov v2, edx<br>&nbsp; mov eax, ecx { data pointer }<br>&nbsp; mov ecx, [ebp+$08] { count }<br>&nbsp; inc ecx<br>&nbsp; mov Count, ecx<br>&nbsp; { Make ebx point to last entry in lpVars }<br>&nbsp; dec ecx<br>&nbsp; imul ecx, 8<br>&nbsp; add eax, ecx<br>&nbsp; mov ecx, Count<br>@@1:<br>&nbsp; mov edx, [eax]<br>&nbsp; push edx<br>&nbsp; sub eax, 8<br>&nbsp; loop @@1<br><br>&nbsp; push v2<br>&nbsp; push v1<br><br>&nbsp; call wsprintf<br><br>&nbsp; { clean up stack }<br>&nbsp; mov ecx, Count<br>&nbsp; imul ecx, 4<br>&nbsp; add ecx, 8<br>&nbsp; add esp, ecx<br>end;<br>procedure Ex2;<br>var<br>&nbsp; A: Array[0..512] of char;<br>begin<br>&nbsp; _wsprintf(A, '%d %c%c%c%c%c%c %s ', [1234, 68, 69,76,80,72,73, 'Delphi']);<br>&nbsp;MessageBox(0,A,'hi',MB_OK);<br>end;<br>//-------------------------<br>在这儿找找 <br>http://groups.google.com/groups?q=delphi+wsprintf&amp;ie=UTF-8&amp;oe=UTF-8&amp;hl=en<br>//-------------------------<br>CIONO1
 
忘了结贴,谢谢kcahcn
 
顶部