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

📄 common.~pas

📁 是一款针对啤酒行业的销售进销存管理系统,包括基本的进货,销售,库存
💻 ~PAS
字号:
unit Common;


interface
//调用Api函数库///
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, Buttons, PicButton, DB, ADODB,Grids,
  COMObj,Quickrpt,QRExtra;
  /////建立一个类////

 ////字符串替换/////
Function ReplaceSubString(sub1,sub2,s:string):string;
//////去掉最后一次出现的字符串/////
Function DeleteLastSubString(sub1,s:string):string;
////// 判断子串在字符串中是否存在////
Function isSubstring(sub1,s:string):boolean;
//////////应用函数定义/////
//procedure CreateButton(ctrl:Tcontrol;Aleft,Atop,Awidth,Aheight:integer;Avisible:boolean);
///建立一个区域/////
function CreateRegion(wMask: TBitmap; wColor: TColor;
      hControl: THandle): HRGN;
///定义显示以图片为形状的窗体////
procedure ShowPloyForm(bmp:Tbitmap;Hcontrol:Thandle);
//////////下面是基本的数据库的操作//////
////连接数据库////
Function ConnectToDatabase:String;
////定义得到指定表的记录数///
Function GetRsCount(adoquery:Tadoquery;Table_Name:string):integer;
////定义按指定条件查询的函数///
Function isRunSql(adoquery:Tadoquery;strSql:string):boolean;
////定义得到某个符合条件的某个字段值(字符串型)///
Function GetFieldValue(adoquery:Tadoquery;strSql:string;Field_Name:string):string;
 ////定义得到某个符合条件的某个字段值(整型)///
Function GetFieldValueOfint(adoquery:Tadoquery;strSql:string;Field_Name:string):integer;
/////定义不返回值的sql查询语句(Insert,Update,Delete)////
procedure RunNonSql(adoquery:Tadoquery;strSql:string);
/////定义返回结果集的sql查询语句(select)////
procedure RunDataSet(adoquery:Tadoquery;strSql:string);
/////得到汇总信息/////
Function GetToTalInfo(adoquery:TAdoQuery;TableName,FieldName,strWhere:string):Double;
  ////定义按指定条件查询的函数///
Function RunSqlOfAdo(adoquery:Tadoquery;strSql:string):TAdoQuery;
/////填充值到控件combobox中///
procedure FillValueToControl(adoquery:Tadoquery;strSql:string;FieldName:string;ctrl:Tcontrol);
/////填充值到控件combobox中///
//procedure FillValueToControl1(adoquery:Tadoquery;strSql:string;FieldName:string;ctrl:Tcontrol);
/////填充值到控件listview中///
procedure FillValueToListView(adoquery:Tadoquery;strSql:string;FieldIndex:integer;ctrl:Tcontrol);

var
strValue:Array[0..6] of string;
billCode:string;

/////////////////
implementation

uses DM;

{procedure CreateButton(ctrl:Tcontrol;Aleft,Atop,Awidth,Aheight:integer;Avisible:boolean);
var
  button:Tbutton;
begin
    with TButton.Create(button) do
    begin
       try
          if (ctrl is TstringGrid) then
          begin
              parent:=TstringGrid(ctrl);
              left:=Aleft;
              top:=Atop;
              width:=Awidth;
              height:=Aheight;
              visible:=Avisible;
          end;
       finally
          free;
       end;
    end;
end;
}
///实现建立区域的定义///
function CreateRegion(wMask:TBitmap;wColor:TColor;hControl:THandle): HRGN;
var
  dc, dc_c: HDC;
  rgn: HRGN;
  x, y: integer;
  coord: TPoint;
  line: boolean;
  color: TColor;
begin
  dc := GetWindowDC(hControl);
  dc_c := CreateCompatibleDC(dc);
  SelectObject(dc_c, wMask.Handle);
  BeginPath(dc);
  for x:=0 to wMask.Width-1 do
  begin
    line := false;
    for y:=0 to wMask.Height-1 do
    begin
      color := GetPixel(dc_c, x, y);
      if not (color = wColor) then
      begin
        if not line then
        begin
          line := true;
          coord.x := x;
          coord.y := y;
        end;
      end;
      if (color = wColor) or (y=wMask.Height-1) then
      begin
        if line then
        begin
          line := false;
          MoveToEx(dc, coord.x, coord.y, nil);
          LineTo(dc, coord.x, y);
          LineTo(dc, coord.x + 1, y);
          LineTo(dc, coord.x + 1, coord.y);
          CloseFigure(dc);
        end;
      end;
    end;
  end;
  EndPath(dc);
  rgn := PathToRegion(dc);
  ReleaseDC(hControl, dc);
  Result := rgn;
end;
////实现区域形状的显示///
procedure ShowPloyForm(bmp:Tbitmap;Hcontrol:Thandle);
var
   w1:Tbitmap;
   w2:Tcolor;
   Rgn:Hrgn;
begin
   w1:=TBitmap.Create;
   w1.Assign(bmp);
   w2:=w1.Canvas.Pixels[0,0];
   rgn := CreateRegion(w1,w2,hcontrol);
   if rgn<>0 then
   begin
      SetWindowRgn(Hcontrol, rgn, true);
   end;
   w1.Free;
end;
////
  ////////连接本地数据库XSH.mdb/////
Function ConnectToDatabase:String;
var
   CurrentPath:string;
begin
    CurrentPath:=ExtractFilePath(ParamStr(0));
    result:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+CurrentPath+'\sale.mdb;Persist Security Info=False';
end;
////实现得到指定表记录数//
Function GetRsCount(adoquery:Tadoquery;Table_Name:string):integer;

begin
   //adoquery:=Tadoquery.Create(adoquery);
   with adoquery do
       begin
            connectionstring:=connecttodatabase;
            close;
            sql.Clear;
            sql.Add('select *  from  ');
            sql.add(Table_Name);
            prepared;
            open;
            requery();
            if recordcount<>0 then
            begin
               result:=recordcount;
            end
            else
               begin
                  result:=0;
               end;
       end;
end;
////实现查询指定条件记录是否存在的函数///
Function isRunSql(adoquery:Tadoquery;strSql:string):boolean;
begin
   //adoquery:=Tadoquery.Create(adoquery);

   with adoquery do
   begin
        ConnectionString:=ConnectToDatabase;
        close;
        sql.Clear;
        sql.add(strSql);
        prepared;
        open;
        if recordcount<>0 then
        begin
           result:=true;
        end
        else
           begin
               result:=false;
           end;
   end;
end;
 ////定义得到某个符合条件的某个字段值(字符串型字段)///
Function GetFieldValue(adoquery:Tadoquery;strSql:string;Field_Name:string):string;

begin
   //adoquery:=Tadoquery.Create(adoquery);
   with adoquery do
   begin
      ConnectionString:=ConnectToDatabase;
      close;
      sql.Clear;
      sql.add(strsql);
      prepared;
      open;
      if recordcount<>0 then
      begin
         result:=adoquery.FieldValues[Field_Name];
      end
      else
         begin
              result:='';
         end;

   end;
end;
 ////定义得到某个符合条件的某个字段值(整形字段)///
Function GetFieldValueOfint(adoquery:Tadoquery;strSql:string;Field_Name:string):integer;
begin
   //adoquery:=Tadoquery.Create(adoquery);
   with adoquery do
   begin
      ConnectionString:=ConnectToDatabase;
      close;
      sql.Clear;
      sql.add(strsql);
      prepared;
      open;
      if recordcount<>0 then
      begin
         result:=adoquery.FieldValues[Field_Name];
      end
      else
         begin
              result:=0;
         end;

   end;
end;
/////定义不返回结果集的sql查询语句(Insert,Update,Delete)////
procedure RunNonSql(adoquery:Tadoquery;strSql:string);

begin
   //adoquery:=Tadoquery.Create(adoquery);
   with adoquery do
   begin
        ConnectionString:=ConnectToDatabase;
        close;
        sql.Clear;
        sql.add(strSql);
        prepared;
        Execsql;

   end;

end;
/////定义返回结果集的sql查询语句(select)////
procedure RunDataSet(adoquery:Tadoquery;strSql:string);
begin
     with adoquery do
   begin
        ConnectionString:=ConnectToDatabase;
        close;
        sql.Clear;
        sql.add(strSql);
        prepared;
        open;

   end;
end;
  ////实现按指定条件查询的函数返回结果集对象///
Function RunSqlOfAdo(adoquery:Tadoquery;strSql:string):TAdoQuery;

begin
   //adoquery:=Tadoquery.Create(adoquery);
   with adoquery do
   begin
        ConnectionString:=ConnectToDatabase;
        close;
        sql.Clear;
        sql.Add(strSql);
        prepared;
        open;
        requery();
        
   end;
   result:=adoquery;

end;
/////得到汇总信息///
Function GetToTalInfo(adoquery:TAdoQuery;TableName,FieldName,strWhere:string):Double;
var
   strSql:string;
begin
    With adoquery do
    begin
        ConnectionString:=ConnectToDatabase;
        close;
        sql.Clear;
        strSql:='SELECT SUM('+FieldName+') as s1 FROM  '+TableName+'  where  '+strWhere;
        //Showmessage(strSql);
        sql.Add(strSql);
        prepared;
        open;
        if Recordcount<>0 then
        begin
           result:=FieldByName('s1').AsFloat;
        end
        else
           begin
              result:=0;
           end;

    end;
end;
//////得到二级商产品汇总数量/////

/////填充值到控件combobox中///
/////FieldValues['code']得到字段的值////
procedure FillValueToControl(adoquery:Tadoquery;strSql:string;FieldName:string;ctrl:Tcontrol);
begin

          with adoquery  do
          begin
            ConnectionString:=ConnectToDatabase;
            close;
            sql.Clear;
            sql.add(strSql);
            prepared;
            open;
            if (ctrl is Tcombobox) then
            begin
              Tcombobox(ctrl).Items.Clear;
              while not eof do
              begin
                     Tcombobox(ctrl).Items.add(FieldValues[FieldName]);
                     next;
              end;
            end;///if结束///
            if (ctrl is TListbox) then
            begin
              TListbox(ctrl).Items.Clear;
              while not eof do
              begin
                 TListbox(ctrl).Items.add(FieldValues[FieldName]);
                 next;
              end;
            end;///if结束///
          end;///with 结束
end;

/////填充值到控件listview中///
/////FieldList.count得到字段的个数///
/////FieldDefList[0].name得到字段的名字////
/////Tlistview(ctrl)控件转换////
procedure FillValueToListView(adoquery:Tadoquery;strSql:string;FieldIndex:integer;ctrl:Tcontrol);
var
   listitem:Tlistitem;
   i:integer;
begin
          with adoquery do
          begin
            ConnectionString:=ConnectToDatabase;
            close;
            sql.Clear;
            sql.add(strSql);
            prepared;
            open;
            //setlength(FieldIndex,FieldList.Count-1); ///设置数组大小
            if (ctrl is TListView) then
            begin
              TListView(ctrl).Clear;
              //FieldIndex:=0;
              first;
              while not eof  do
              begin
                 listitem:=TlistView(ctrl).Items.Add;

                 for FieldIndex:=0 to FieldList.Count-2 do
                 begin
                    listitem.Caption:=FieldValues[FieldDefList[0].Name];
                    if not  FieldbyName(FieldDefList[FieldIndex+1].Name).IsNull then
                    begin
                       listitem.SubItems.Add(FieldValues[FieldDefList[FieldIndex+1].Name]);
                    end;   
                 end;
                 next;

              end;
            end;///if结束///
            if (ctrl is TStringGrid) then
            begin
               first;
               TStringGrid(ctrl).ColCount:=FieldList.Count;
               TStringGrid(ctrl).RowCount:=Recordcount+2;
               while not eof  do
               begin
                   for i:=0 to FieldList.Count-1 do
                   begin
                       TStringGrid(ctrl).Cells[i,FieldIndex+1]:=FieldValues[FieldDefList[i].Name];
                   end;
                   next;
                   FieldIndex:=FieldIndex+1;
               end;
            end;///if 结束
            
          end;///with 结束

end;////函数实现结束///
 ////字符串替换,用字串sub2替换sub1///
Function ReplaceSubString(sub1,sub2,s:string):string;
var
  strLength:integer;
begin
   while Pos(sub1, s) > 0 do
   begin
    //S[Pos(' ', S)] := '0';
    strLength:=length(sub1);
    delete(s,pos(sub1,s),strLength);
    insert(sub2,s,pos(sub1,s));
    //delete(s,
   end;
   result:=s;
end;
////删除最后一次出现的子字符串////
Function DeleteLastSubString(sub1,s:string):string;
var
  srcLength:integer;
  subLength:integer;
begin
    srcLength:=Length(s);
    //showmessage(inttostr(srclength));
    subLength:=Length(sub1);
     //showmessage(inttostr(sublength));

    result:=copy(s,1,srcLength-subLength);
end;
////// 判断子串在字符串中是否存在////
Function isSubstring(sub1,s:string):boolean;
begin
    if pos(sub1,s)>0 then
    begin
       result:=true;
    end
    else
       result:=false;
end;

end.////类结束////

⌨️ 快捷键说明

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