📄 syspublic.pas
字号:
WSAStartup($101, WSAData);
SockAddrIn.sin_addr.s_addr := inet_addr(PChar(IPAddr));
HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt <> nil then
begin
Result := StrPas(Hostent^.h_name)
end
else
begin
Result := '';
end;
end;
function GetFocusedComponent(frmForm: TForm): TComponent;
var
i: Integer;
begin
Result := nil;
with frmForm do
begin
for i := 0 to ComponentCount - 1 do
begin
if (Components[i] is TWinControl) and TWinControl(Components[i]).Focused then
begin
Result := Components[i];
break;
end;
end;
end;
end;
function DBGridRecordSize(mColumn: TColumn): Boolean;
begin
Result := False;
if not Assigned(mColumn.Field) then Exit;
mColumn.Field.Tag := Max(mColumn.Field.Tag,
TDBGrid(mColumn.Grid).Canvas.TextWidth(mColumn.Field.DisplayText));
Result := True;
end; { DBGridRecordSize }
function DBGridAutoSize(mDBGrid: TDBGrid; mOffset: Integer = 5): Boolean;
var
I: Integer;
begin
Result := False;
if not Assigned(mDBGrid) then Exit;
if not Assigned(mDBGrid.DataSource) then Exit;
if not Assigned(mDBGrid.DataSource.DataSet) then Exit;
if not mDBGrid.DataSource.DataSet.Active then Exit;
for I := 0 to mDBGrid.Columns.Count - 1 do begin
if not mDBGrid.Columns[I].Visible then Continue;
if Assigned(mDBGrid.Columns[I].Field) then
mDBGrid.Columns[I].Width := Max(mDBGrid.Columns[I].Field.Tag,
mDBGrid.Canvas.TextWidth(mDBGrid.Columns[I].Title.Caption)) + mOffset
else mDBGrid.Columns[I].Width :=
mDBGrid.Canvas.TextWidth(mDBGrid.Columns[I].Title.Caption) + mOffset;
mDBGrid.Refresh;
end;
Result := True;
end; { DBGridAutoSize }
function GetSQLServerList(Combobox :TComBoBox): Boolean;
var
SQLServer:Variant;
ServerList:Variant;
i,nServers:integer;
sRetValue:String;
begin
Result := True;
try
SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
nServers:=ServerList.Count;
for i := 1 to nservers do
Combobox.Items.Add(ServerList.Item(i));
SQLServer:=NULL;
serverList:=NULL;
except
Result := False;
end;
end;
procedure DeriveToExcel(Title: String; DBGrid: TDBGrid; Total: Boolean);
var
ExcelApp, WorkBook: Variant;
i, j: Integer;
Row, Col: Integer;
FieldName: string;
DataSet: TDataSet;
S: String;
begin
// 数据发送到 Excel
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('你的机器里未安装Microsoft Excel. ', '', 32);
Exit;
end;
Application.ProcessMessages;
WorkBook := ExcelApp.WorkBooks.Add;
Col := 1;
ExcelApp.Cells(2, Col) := Title;
Row := 4;
DataSet := DBGrid.DataSource.DataSet;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
if DBGrid.Columns[I].Visible then
begin
FieldName := DBGrid.Columns[I].Title.Caption;
ExcelApp.Cells(Row, Col) := FieldName;
Col := Col + 1;
end;
end;
Row := Row + 1;
DataSet.First;
while not DataSet.Eof do
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := ' ' + DataSet.FieldByName(FieldName).AsString + ' ';
Col := Col + 1;
end;
Row := Row + 1;
DataSet.Next;
end;
if Total then
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
S := Char(64 + ((J+1) mod 26));
if (J+1) > 26 then
begin
S := Char(65+(((J+1)-26) div 26)) + S;
end;
if J = 0 then
begin
ExcelApp.Cells(Row, Col) := '合计';
end
else if DBGrid.Columns[J].Field.DataType in [ftInteger, ftSmallint, ftFloat, ftBCD] then
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := '=SUM('+S+'4:'+S+IntToStr(Row-1)+')';
end;
Col := Col + 1;
end;
end;
ExcelApp.Visible := True;
// WorkBook.SaveAs(SaveDialog1.FileName);
// WorkBook.Close;
// ExcelApp.Quit;
// ExcelApp := Unassigned;
end;
procedure DeriveToPrint(Title: String; DBGrid: TDBGrid; Total: Boolean);
const
LeftMargin = 3;
RightMargin = 3;
var
I, L: Integer;
QR: TQuickRep;
QRLabel: TQRLabel;
QRShape: TQRShape;
QRDBText: TQRDBText;
QRExpr: TQRExpr;
PageHeader: TQRBand;
ColumnHeader: TQRBand;
SummaryBand: TQRBand;
Detail: TQRBand;
PageFooter: TQRBand;
DataSet: TDataSet;
begin
QR := TQuickRep.Create(Application.MainForm);
PageHeader := TQRBand.Create(QR);
PageHeader.Parent := QR;
PageHeader.BandType := rbPageHeader;
PageHeader.Height := 80;
ColumnHeader := TQRBand.Create(QR);
ColumnHeader.Parent := QR;
ColumnHeader.BandType := rbColumnHeader;
ColumnHeader.Height := 24;
ColumnHeader.Frame.DrawTop := True;
ColumnHeader.Frame.DrawBottom := False;
ColumnHeader.Frame.DrawLeft := True;
ColumnHeader.Frame.DrawRight := True;
Detail := TQRBand.Create(QR);
Detail.Parent := QR;
Detail.BandType := rbDetail;
Detail.Height := 24;
Detail.Frame.DrawTop := False;
Detail.Frame.DrawBottom := False;
Detail.Frame.DrawLeft := True;
Detail.Frame.DrawRight := True;
SummaryBand := nil;
if Total then
begin
SummaryBand := TQRBand.Create(QR);
SummaryBand.Parent := QR;
SummaryBand.BandType := rbSummary;
SummaryBand.Height := 24;
SummaryBand.Frame.DrawTop := False;
SummaryBand.Frame.DrawBottom := True;
SummaryBand.Frame.DrawLeft := True;
SummaryBand.Frame.DrawRight := True;
end;
try
DataSet := DBGrid.DataSource.DataSet;
QR.DataSet := DataSet;
// 标题行
if Trim(Title) <> '' then
begin
QRLabel := TQRLabel.Create(QR);
QRLabel.Parent := PageHeader;
QRLabel.AutoSize := False;
QRLabel.Top := 30;
QRLabel.Left := 0;
QRLabel.Width := PageHeader.Width;
QRLabel.Alignment := taCenter;
QRLabel.Caption := Title;
QRLabel.Font.Name := '宋体';
QRLabel.Font.Size := 16;
QRLabel.Font.Style := [fsBold];
end;
QRShape := TQRShape.Create(QR);
QRShape.Parent := ColumnHeader;
QRShape.Shape := qrsHorLine;
QRShape.Width := ColumnHeader.Width;
QRShape.Left := 0;
QRShape.Top := ColumnHeader.Height - 1;
QRShape.Height := 1;
L := 0;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
QRLabel := TQRLabel.Create(QR);
QRLabel.Parent := ColumnHeader;
QRLabel.AutoSize := False;
QRLabel.Left := L;
QRLabel.Top := 5;
QRLabel.Width := DBGrid.Columns[I].Width;
QRLabel.Alignment := taCenter;
QRLabel.Caption := DBGrid.Columns[I].Title.Caption;
QRLabel.Font.Name := '宋体';
QRLabel.Font.Size := 9;
L := L + DBGrid.Columns[I].Width;
if I < DBGrid.Columns.Count - 1 then
begin
if (L + DBGrid.Columns[I+1].Width) > ColumnHeader.Width then
Break;
end;
if I < DBGrid.Columns.Count - 1 then
begin
QRShape := TQRShape.Create(QR);
QRShape.Parent := ColumnHeader;
QRShape.Shape := qrsVertLine;
QRShape.Width := 1;
QRShape.Left := L;
QRShape.Top := 0;
QRShape.Height := ColumnHeader.Height;
L := L + 1;
end;
end;
QRShape := TQRShape.Create(QR);
QRShape.Parent := Detail;
QRShape.Shape := qrsHorLine;
QRShape.Width := Detail.Width;
QRShape.Left := 0;
QRShape.Top := Detail.Height - 1;
QRShape.Height := 1;
L := 0;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
QRDBText := TQRDBText.Create(QR);
QRDBText.Parent := Detail;
QRDBText.AutoSize := False;
QRDBText.Left := L + LeftMargin;
QRDBText.Top := 6;
QRDBText.Width := DBGrid.Columns[I].Width - (LeftMargin + RightMargin);
QRDBText.Alignment := DBGrid.Columns[I].Alignment;
QRDBText.DataSet := DataSet;
QRDBText.DataField := DBGrid.Columns[I].FieldName;
QRDBText.Font.Name := '宋体';
QRDBText.Font.Size := 9;
L := L + DBGrid.Columns[I].Width;
if I < DBGrid.Columns.Count - 1 then
begin
if (L + DBGrid.Columns[I+1].Width) > Detail.Width then
Break;
end;
if I < DBGrid.Columns.Count - 1 then
begin
QRShape := TQRShape.Create(QR);
QRShape.Parent := Detail;
QRShape.Shape := qrsVertLine;
QRShape.Width := 1;
QRShape.Left := L;
QRShape.Top := 0;
QRShape.Height := Detail.Height;
L := L + 1;
end;
end;
// 增加合计行
if Total then
begin
L := 0;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
if I = 0 then
begin
QRLabel := TQRLabel.Create(QR);
QRLabel.Parent := SummaryBand;
QRLabel.AutoSize := False;
QRLabel.Left := L + LeftMargin;
QRLabel.Top := 6;
QRLabel.Width := DBGrid.Columns[I].Width - (LeftMargin + RightMargin);
QRLabel.Alignment := taCenter;
QRLabel.Caption := '合计';
QRLabel.Font.Name := '宋体';
QRLabel.Font.Size := 9;
end
else if DBGrid.Columns[I].Field.DataType in [ftInteger, ftSmallint, ftFloat, ftBCD] then
begin
QRExpr := TQRExpr.Create(QR);
QRExpr.Parent := SummaryBand;
QRExpr.AutoSize := False;
QRExpr.Left := L + LeftMargin;
QRExpr.Top := 6;
QRExpr.Width := DBGrid.Columns[I].Width - (LeftMargin + RightMargin);
QRExpr.Alignment := DBGrid.Columns[I].Alignment;
QRExpr.Expression := 'SUM(' + DBGrid.Columns[I].FieldName + ')';
QRExpr.Font.Name := '宋体';
QRExpr.Font.Size := 9;
end;
L := L + DBGrid.Columns[I].Width;
if I < DBGrid.Columns.Count - 1 then
begin
if (L + DBGrid.Columns[I+1].Width) > SummaryBand.Width then
Break;
end;
if I < DBGrid.Columns.Count - 1 then
begin
QRShape := TQRShape.Create(QR);
QRShape.Parent := SummaryBand;
QRShape.Shape := qrsVertLine;
QRShape.Width := 1;
QRShape.Left := L;
QRShape.Top := 0;
QRShape.Height := SummaryBand.Height;
L := L + 1;
end;
end;
end;
// 打印页尾
PageFooter := TQRBand.Create(QR);
PageFooter.Parent := QR;
PageFooter.BandType := rbPageFooter;
PageFooter.Height := 40;
QRLabel := TQRLabel.Create(QR);
QRLabel.Parent := PageFooter;
QRLabel.Left := 30;
QRLabel.Top := 4;
QRLabel.Font.Name := '宋体';
QRLabel.Font.Size := 9;
QRLabel.Caption := '打印日期:' + DateToStr(Date);
QR.PreviewModal;
finally
PageHeader.Free;
ColumnHeader.Free;
Detail.Free;
if Total then
SummaryBand.Free;
PageFooter.Free;
QR.Free;
end;
end;
Function RealToTxt(Amount : Real) : String;
var
Num : LongInt;
Fracture : Integer;
function Num2Str(Num: LongInt): String;
Const hundred = 100;
thousand = 1000;
million = 1000000;
billion = 1000000000;
begin
if Num >= billion then
if (Num mod billion) = 0 then
Num2Str := Num2Str(Num div billion) + ' Billion'
else
Num2Str := Num2Str(Num div billion) + ' Billion ' +
Num2Str(Num mod billion)
else
if Num >= million then
if (Num mod million) = 0 then
Num2Str := Num2Str(Num div million) + ' Million'
else
Num2Str := Num2Str(Num div million) + ' Million ' +
Num2Str(Num mod million)
else
if Num >= thousand then
if (Num mod thousand) = 0 then
Num2Str := Num2Str(Num div thousand) + ' Thousand'
else
Num2Str := Num2Str(Num div thousand) + ' Thousand ' +
Num2Str(Num mod thousand)
else
if Num >= hundred then
if (Num mod hundred) = 0 then
Num2Str := Num2Str(Num div hundred) + ' Hundred'
else
Num2Str := Num2Str(Num div hundred) + ' Hundred ' +
Num2Str(Num mod hundred)
else
case (Num div 10) of
6,7,9: if (Num mod 10) = 0 then
Num2Str := Num2Str(Num div 10) + 'ty'
else
Num2Str := Num2Str(Num div 10) + 'ty-' +
Num2Str(Num mod 10);
8: if Num = 80 then
Num2Str := 'Eighty'
else
Num2Str := 'Eighty-' + Num2Str(Num mod 10);
5: if Num = 50 then
Num2Str := 'Fifty'
else
Num2Str := 'Fifty-' + Num2Str(Num mod 10);
4: if Num = 40 then
Num2Str := 'Forty'
else
Num2Str := 'Forty-' + Num2Str(Num mod 10);
3: if Num = 30 then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -