delphi6里如何调用WinApi wsprintf和wvsprintf? ( 积分: 50 )

  • 主题发起人 主题发起人 xingsx
  • 开始时间 开始时间
X

xingsx

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi6里如何调用WinApi wsprintf 和 wvsprintf ?
 
delphi6里如何调用WinApi wsprintf 和 wvsprintf ?
 
var<br> &nbsp;name : string; &nbsp;//人的姓名<br> &nbsp;age : integer; &nbsp;//人的年龄<br> &nbsp;str: PChar;<br>begin<br> &nbsp;name :='zs';<br> &nbsp;age :=23;<br> &nbsp;GetMem(str, 255);<br> &nbsp;wsprintf(str, PChar(Format('年龄: %s 岁: %d', [name, age])));<br> &nbsp;messagebox(0,str,'hint',0);<br> &nbsp;FreeMem(str);<br>end;<br>
 
这样就和直接用Format一样了……<br>我的意思是用wsprintf实现格式化
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;s : String;<br> &nbsp;i : Integer;<br> &nbsp;args : array[0..1] of Pointer;<br> &nbsp;szBuffer : array[0..1024] of Char;<br>begin<br> &nbsp;s := 'String';<br> &nbsp;i := 100;<br> &nbsp;args[0] := PChar(s);<br> &nbsp;args[1] := Pointer(i);<br> &nbsp;wvsprintf(szBuffer, 'xx %s %d', @args);<br> &nbsp;ShowMessage(szBuffer);<br>end;
 
谢谢,还差一个wsprintf
 
program &nbsp;ScrnSize;<br><br>uses<br> &nbsp;Windows;<br><br> &nbsp;// 格式化输出函数 &nbsp; &nbsp; &nbsp; &nbsp;{标题} &nbsp; {输出格式} &nbsp; &nbsp; {输出变量列表}<br>procedure MessageBoxPrintf(Caption, Format: PChar; const Args: array of const);<br>var<br> &nbsp;OutBuff: array[0..100] of Char; // 最终输出文字<br> &nbsp;OutList: array of LongWord; &nbsp; &nbsp; // 输出变量列表<br> &nbsp;J: Integer;<br>begin<br> &nbsp;SetLength(OutList, 0);<br> &nbsp;for J := Low(Args) to High(Args) do // Args -&amp;gt; OutList<br> &nbsp;begin<br> &nbsp; &nbsp;case Args[J].VType of &nbsp;// 这里仅处理了两种类型<br> &nbsp; &nbsp; &nbsp;vtInteger: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetLength(OutList, Length(OutList)+1);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutList[High(OutList)] := Args[J].VInteger;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp;vtString : begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetLength(OutList, Length(OutList)+1);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutList[High(OutList)] := LongWord(Args[J].VString);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;if Length(OutList) = Length(Args) then // 列表成员均合法<br> &nbsp;begin<br> &nbsp; &nbsp;wvsprintf(OutBuff, Format, @OutList[0]); &nbsp;// 转换<br> &nbsp; &nbsp;MessageBox(0, OutBuff, Caption, 0); &nbsp; &nbsp; &nbsp; // 输出<br> &nbsp;end;<br>end;<br><br>var<br> &nbsp;cxScreen, cyScreen: Integer;<br><br>begin<br> &nbsp;cxScreen := GetSystemMetrics(SM_CXSCREEN); // 屏幕宽度<br> &nbsp;cyScreen := GetSystemMetrics(SM_CYSCREEN); // 屏幕高度<br> &nbsp;MessageBoxPrintf('ScrnSize', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'The screen is %i pixels wide by %i pixels high.',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [cxScreen, cyScreen]);<br>end.<br>
 
// 简单封装API, 便于调用<br>function wvsprintf(Output: PChar; Format: PChar; Arg_List: array of Integer): Integer;<br>begin<br> &nbsp;Result := Windows.wvsprintf(Output, Format, @Arg_List[Low(Arg_List)]);<br>end;
 
后退
顶部