⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 normalcode.pas

📁 delphi学习指南
💻 PAS
字号:
unit NormalCode;

interface

uses Windows,SysUtils,StrUtils, Classes,Variants,Dialogs,xmldom, XMLIntf, msxmldom, XMLDoc,ShellApi;
//========基础函数
    function exec(hWnd: HWND; Operation, FileName, Parameters, Directory: PChar; ShowCmd: Integer): HINST; stdcall;
    function getCurrentDir(): String;
    function GetStrLength(const S: string): integer;
    function getCountStr(strSrc:String;const pos:Integer=1;const dep:Char='.'):String;
    function SmallTOBig(small:real):string;    //将金额转换的大写金额。
    function getXmlNodeValue(strEntityEngineFile:String; xmlNodePath:String; const xmlattrname:String=''; const dep:Char ='.'):String;
    function setXmlNodeValue(strEntityEngineFile:String; xmlNodePath:String; const xmlattrname:String=''; const value:String=''; const dep:Char ='.'):boolean;

implementation

{----------------------------------------------------------------------------------------------------------------------------
  函数功能:返回当前路径
  返 回 值:以\结束
}
function getCurrentDir(): String;
begin
   result:= SysUtils.GetCurrentDir();
   if(StrUtils.RightStr(result,1) <> '\') then begin
      result:=result+'\';
   end;
end;

{----------------------------------------------------------------------------------------------------------------------------
  函数功能:在Delphi中,通过什么函数取'我们123'此字符串的长度为5,而Length('我们123')=7
  返 回 值:长度
}
function GetStrLength(const S: string): integer;
begin
    Result:=Length(WideString(S));
end;

{----------------------------------------------------------------------------------------------------------------------------
  函数功能:执行外部程序,调用ShellApi中函数
  入口参数: hWnd: 父窗口句柄
             Operation: 操作模式 open 或 print
             FileName: 文件名指针
             Parameter: 传递给执行文件的参数
             Directory: 缺省目录
             ShowCmd: 程序启动后的状态:(1)SW_SHOWNORMAL 正常 (2)SW_MINIMIZE 最小 (3)SW_MAXIMIZE 最大
}
function exec(hWnd: HWND; Operation, FileName, Parameters, Directory: PChar; ShowCmd: Integer): HINST; stdcall;
begin
    result:=ShellExecute(hWnd,Operation,FileName,Parameters, Directory,ShowCmd);
end;

{----------------------------------------------------------------------------------------------------------------------------
  函数功能:返回字符串中用指定分隔符分隔的第n个字符串
  入口参数:strSrc 源串
            pos取第几个(1 。。n)
            dep分隔字符
  返 回 值:y
}
function getCountStr(strSrc:String ; const pos:Integer=1; const dep:Char='.'):String;
var
    SS       : TStrings;
begin
    SS:=TStringList.Create;
    SS.Delimiter:=dep;
    SS.DelimitedText:=strSrc;

    if(pos>=SS.Count) then begin
       result:=SS[SS.Count-1];
    end else begin
       result:=SS[pos-1];
    end;
end;


{---------------------------------------------------------------------------------------------------------------------------}
function getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
   i: Integer;
begin
   for i :=1  to childnodes.Count do begin
      if(childnodes.Get(i-1).NodeName = nodename) then begin
         result:= childnodes[i-1];
         exit;
      end;
   end;
end;
{----------------------------------------------------------------------------------------------------------------------------
  函数功能:直接读取xml文件中的某个节点第一次出现的值
  入口参数:xmlFile xml文件
            xmlnodepath 节点
            xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
            dep  节点的参数的分隔符,默认为.
  返 回 值:末级点的值
}
function getXmlNodeValue(strEntityEngineFile:String; xmlNodePath:String;
                         const xmlattrname:String=''; const dep:Char ='.'):String;
var
  xmlDocument :IXMLDocument;
  node        :IXMLNode;
  xmlnodeList :TStrings;
  i           :Integer;
  urlcount    :Integer;
begin
    //xml节点路径
    xmlnodeList:=TStringList.Create;
    xmlnodeList.Delimiter:=dep;
    xmlnodeList.DelimitedText:=xmlnodepath;
    urlcount:=xmlnodeList.Count;
    //xml对象
    xmlDocument :=TXMLDocument.Create(nil);
    xmlDocument.LoadFromFile(strEntityEngineFile);
    xmlDocument.Active:=true;
    try
        node:= xmlDocument.DocumentElement;
        if(node.NodeName = xmlnodeList[0]) then begin
            //扫描节点
            for i := 1  to urlcount-1 do begin
                if(node<>nil) then
                    node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i])
                else Break;
            end;
            if(node=nil)then begin
                result:='';
            end else begin
                //判断是取属性还是取节点内容
                if(Trim(xmlattrname)='') then
                    result:=node.Text
                else
                    result:=node.AttributeNodes.Nodes[xmlattrname].NodeValue;
            end;
        end else begin
          result:='';
        end;

    except
        result:='error';
    end;
    xmlDocument.Active:=false;
end;

  
{----------------------------------------------------------------------------------------------------------------------------
  函数功能:保存xml文件中的某个节点或属性的值
  入口参数:xmlFile xml文件
            xmlnodepath 节点
            xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
            dep  节点的参数的分隔符,默认为.
  返 回 值:末级点的值
}
function setXmlNodeValue(strEntityEngineFile:String; xmlNodePath:String;
                         const xmlattrname:String=''; const value:String=''; const dep:Char ='.'):boolean;
var
  xmlDocument :IXMLDocument;
  node        :IXMLNode;
  xmlnodeList :TStrings;
  i           :Integer;
  urlcount    :Integer;
begin
    //xml节点路径
    xmlnodeList:=TStringList.Create;
    xmlnodeList.Delimiter:=dep;
    xmlnodeList.DelimitedText:=xmlnodepath;
    urlcount:=xmlnodeList.Count;
    //xml对象
    xmlDocument :=TXMLDocument.Create(nil);
    xmlDocument.LoadFromFile(strEntityEngineFile);
    xmlDocument.Active:=true;
    try
        node:= xmlDocument.DocumentElement;
        if(node.NodeName = xmlnodeList[0]) then begin
            //扫描节点
            for i := 1  to urlcount-1 do begin
                if(node<>nil) then
                    node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i])
                else Break;
            end;
            
            if(node <> nil)then begin
                if(Trim(xmlattrname)='') then
                    node.Text:=value
                else
                    node.AttributeNodes.Nodes[xmlattrname].NodeValue:=value;
                xmlDocument.SaveToFile(strEntityEngineFile);
            end;
        end;
        result:=true;
    except
        result:=false;
    end;
    xmlDocument.Active:=false;
end;

{----------------------------------------------------------------------------------------------------------------------------
  函数功能:将金额转换的大写金额。
  入口参数:small 小写金额
  返 回 值:大写金额
}
function SmallTOBig(small:real):string;
  var SmallMonth,BigMonth:string;
      wei1,qianwei1:string[2];
      qianwei,dianweizhi,qian:integer;
  begin
   {小数点后的位置,需要的话也可以改动-2值}
   qianwei:=-2;
   {转换成货币形式,需要的话小数点后加多几个零}
   Smallmonth:=FormatFloat('0.00',small);
   dianweizhi :=pos('.',Smallmonth);{小数点的位置}
   {循环小写货币的每一位,从小写的右边位置到左边}
   for qian:=length(Smallmonth) downto 1 do
   begin
     {如果读到的不是小数点就继续}
     if qian<>dianweizhi then
     begin
     {位置上的数转换成大写}
        case strtoint(copy(Smallmonth,qian,1)) of
          1:wei1:='壹'; 2:wei1:='贰';
          3:wei1:='叁'; 4:wei1:='肆';
          5:wei1:='伍'; 6:wei1:='陆';
          7:wei1:='柒'; 8:wei1:='捌';
          9:wei1:='玖'; 0:wei1:='零';
        end;
        {判断大写位置,可以继续增大到real类型的最大值}
        case qianwei of
          -3:qianwei1:='厘';
          -2:qianwei1:='分';
          -1:qianwei1:='角';
          0 :qianwei1:='元';
          1 :qianwei1:='拾';
          2 :qianwei1:='佰';
          3 :qianwei1:='千';
          4 :qianwei1:='万';
          5 :qianwei1:='拾';
          6 :qianwei1:='佰';
          7 :qianwei1:='千';
          8 :qianwei1:='亿';
          9 :qianwei1:='十';
          10:qianwei1:='佰';
          11:qianwei1:='千';
        end;
        inc(qianwei);
        BigMonth :=wei1+qianwei1+BigMonth;{组合成大写金额}
      end;
   end;
   SmallTOBig:=BigMonth;
end;


//===========================================================================================================================
//over
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -