unit sharepas;

interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, strutils,  DB, ADODB, AdvGrid,ComCtrls,dxtree,
  RzButton,DateUtils,WinSock,Registry;



procedure cmbView(Sender1, Sender2: TObject; TabName, fieldName, aName, bName, cName: string); //从数据库中读取数据到COMBOBOX
function   ShowMsg(const   Msg,captionstr:   string;   DlgType:   TMsgDlgType;
      Btn:   TMsgDlgButtons;   Captions:   array   of   string;mycolor,fontcolor:TColor):   Integer;//自定消息框
function GetAveCharSize(Canvas: TCanvas): TPoint;
function InputBoxEx(const ACaption, APrompt, ADefault: string): string;  //自定义输入框
implementation

procedure cmbView(Sender1, Sender2: TObject; TabName, fieldName, aName, bName, cName: string);
//给COMBOBOX赋值,Sender:Combobox名,TabName:表名,fieldName,显示字段名,aName:查询条件,bName:增加到COMBOBOX中的字段名,cName:查询条件名
var
  str: string;
begin
  str := 'select ' + fieldName + ' from ' + TabName;
  if (aName <> '') and (bName <> '') then
    str := str + ' where ' + cName + '=''' + aName + '''';
  TComboBox(Sender1).Items.Clear;
  with TADOQuery(Sender2) do
  begin
    TADOQuery(Sender2).close;
    TADOQuery(Sender2).sql.Clear;
    TADOQuery(Sender2).sql.Add(str);
    TADOQuery(Sender2).open;
    while not eof do
    begin
      TComboBox(Sender1).Items.Add(TADOQuery(Sender2).FieldByName(bName).AsString);
      Next;
    end;
  end;
  TComboBox(Sender1).ItemIndex := 0;
end;


function   ShowMsg(const   Msg,captionstr:   string;   DlgType:   TMsgDlgType;
      Btn:   TMsgDlgButtons;   Captions:   array   of   string;mycolor,fontcolor:TColor):   Integer;
//显示自定义对话框
  var
      aMsgDlg:   TForm;
      i:   Integer;
      dlgButton:   TRzButton;
      CaptionIndex:   Integer;
  begin

      aMsgDlg   :=   CreateMessageDialog(Msg,   DlgType,   Btn);
      captionIndex   :=   0;
      for   i   :=   0   to   aMsgDlg.ComponentCount   -   1   do
      begin
          if   (aMsgDlg.Components[i]   is   TButton)   then
          begin
              dlgButton   :=   TRzButton(aMsgDlg.Components[i]);
              if   CaptionIndex   >   High(Captions)   then   Break;
              dlgButton.Caption   :=   Captions[CaptionIndex];
              dlgButton.Color:=clRed;
              Inc(CaptionIndex);
          end;
      end;
      aMsgDlg.Caption := captionstr;
      aMsgDlg.Color   :=mycolor;
      aMsgDlg.Font.Color:=fontcolor;
      Result   :=   aMsgDlg.ShowModal;
  end;

function GetAveCharSize(Canvas: TCanvas): TPoint;

var
  I: Integer;
  Buffer: array[0..51] of Char;
begin
  for I := 0 to 25 do Buffer[I] := Chr(I + Ord('A'));
  for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a'));
  GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
  Result.X := Result.X div 52;
end;



function InputBoxEx(const ACaption, APrompt, ADefault: string): string;
//自定义INPUT窗口
var
  Form: TForm;
  Prompt: TLabel;
  Edit: TEdit;
  DialogUnits: TPoint;
  ButtonTop, ButtonWidth, ButtonHeight: Integer;
begin
  Result := ADefault;

  Form := TForm.Create(Application);
  with Form do
    try
      Font.Name := '宋体';
      Font.Size := 9;
      Canvas.Font := Font;
      DialogUnits := GetAveCharSize(Canvas);
      BorderStyle := bsDialog;
      Caption := ACaption;
      Color:=$00efebef;
      ClientWidth := MulDiv(180, DialogUnits.X, 4);
      Position := poMainFormCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        Caption := APrompt;
        Left := MulDiv(8, DialogUnits.X, 4);
        Top := MulDiv(8, DialogUnits.Y, 8);
        Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
        WordWrap := True;
      end;
      Edit := TEdit.Create(Form);
      with Edit do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := Prompt.Top + Prompt.Height + 5;
        Width := MulDiv(164, DialogUnits.X, 4);
        MaxLength := 255;
        Text := Result;
        SelectAll;
      end;
      ButtonTop := Edit.Top + Edit.Height + 15;
      ButtonWidth := MulDiv(50, DialogUnits.X, 4);
      ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := '确定';
        ModalResult := mrOk;
        Default := True;
        SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
          ButtonHeight);
      end;
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := '取消';
        ModalResult := mrCancel;
        Cancel := True;
        SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15,
          ButtonWidth, ButtonHeight);
        Form.ClientHeight := Top + Height + 13;
      end;
      if ShowModal = mrOk then
      begin
        Result := Edit.Text;
      end;
    finally
      Form.Free;
    end;
end;



end.