unit U_GetDllForm;

interface

uses
  Windows, Messages, forms, OleCtnrs, DateUtils, SysUtils, ADODB, IniFiles,
  Dxcore, activex;

function GetDllForm(App: Tapplication; FormH: hwnd; FormID: integer; Language: integer; WinStyle: integer; GCode: Pchar; GName: Pchar; DataBase: Pchar; Title: PChar; Parameters1: PChar; Parameters2: PChar; Parameters3: PChar; Parameters4: PChar; Parameters5: PChar; Parameters6: PChar; Parameters7: PChar; Parameters8: PChar; Parameters9: PChar; Parameters10: PChar; DataBaseStr: PChar): hwnd; export; stdcall;

function ConnData(): Boolean;

implementation

uses
  U_DataLink, U_CPType, U_CPManage, U_KHGetCPManage, U_iniParam;

       /////////////////////////////////////////////////////////////////
      //         功能说明:取Dll中得窗体                              //
     //        参数说明：App>>调用应用程序；                        //
    //                 FormH>>调用窗口句柄 ；                      //
   //                 FormID>>窗口号;                             //
  //                 Language>>语言种类;                         //
 //                 WinStyle>>窗口类型；                        //
/////////////////////////////////////////////////////////////////
var
  frmCPManageGL, frmCPManageLR, frmCPManageCX: TfrmCPManage;

function GetDllForm(App: Tapplication; FormH: hwnd; FormID: integer; Language: integer; WinStyle: integer; GCode: Pchar; GName: Pchar; DataBase: Pchar; Title: PChar; Parameters1: PChar; Parameters2: PChar; Parameters3: PChar; Parameters4: PChar; Parameters5: PChar; Parameters6: PChar; Parameters7: PChar; Parameters8: PChar; Parameters9: PChar; Parameters10: PChar; DataBaseStr: PChar): hwnd;
var
  i: Integer;
  bFound: Boolean;
  mnewHandle: hwnd;
  mstyle: TFormStyle;     // 0:子窗口; 1：普通窗口
  mstate: TWindowState;
  mborderstyle: TFormBorderStyle;
begin
  mnewHandle := 0;
  DName := GName;
  DCode := GName;
  DdataBase := DataBase;
  DTitCaption := Title;
  DParameters1 := Parameters1;
  DParameters2 := Parameters2;
  DParameters3 := Parameters3;
  DParameters4 := Parameters4;
  DParameters5 := Parameters5;
  DParameters6 := Parameters6;
  DParameters7 := Parameters7;
  DParameters8 := Parameters8;
  DParameters9 := Parameters9;
  DParameters10 := Parameters10;

  MainApplication := App;
  DCurHandle := FormH;
  IsDelphiLanguage := Language;

  Application := TApplication(App);
  DCurHandle := 0;


   //赋值链接字符串
  SetLength(server, 255);
  SetLength(dtbase, 255);
  SetLength(user, 255);
  SetLength(pswd, 255);

//  server := '.';
  server := '121.40.233.100,7781';
  dtbase := 'chengxiedata';
  user := 'sa';
  pswd := 'rightsoft@123';
//  pswd := 'rightsoft';
  DConString := 'Provider=SQLOLEDB.1;Password=' + pswd + ';Persist Security Info=True;User ID=' + user + ';Initial Catalog=' + dtbase + ';Data Source=' + server;
  DConString := DataBaseStr;
//   DParameters1:='高权限';
  if not ConnData() then
  begin
    result := 0;
    exit;
  end;
  if IsINIFile() then
    ReadINIFile()
  else
    WriteINIFile;
   // 定义窗口类型 、状态
  if WinStyle = 0 then
  begin
    mstyle := fsMDIChild;
    mstate := wsMaximized;
    mborderstyle := bsSizeable;
  end
  else
  begin
    mstyle := fsNormal;
    mstate := wsNormal;
    mborderstyle := bsSizeable;
  end;
  /////////////////////
  //调用子模块窗口
  case FormID of
    1:  // 样品类别
      begin
        if frmCPType = nil then
        begin
          frmCPType := TfrmCPType.Create(application.MainForm);
          with frmCPType do
          begin
            caption := Trim(Title);
            FormStyle := mstyle;
            windowState := mstate;
            BorderStyle := mborderstyle;
           //show;
          end;
        end
        else
          frmCPType.BringToFront;
       //句柄
        mnewHandle := frmCPType.Handle;

      end;
    2:  // 样品信息管理
      begin
        bFound := FALSE;
        for i := (App.MainForm.MDIChildCount - 1) downto 0 do
        begin
          if App.MainForm.MDIChildren[i].Caption = '样品信息管理' then
          begin
            BringWindowToTop(frmCPManageGL.Handle);
            bFound := TRUE;
            Break;
          end;
        end;
        if not bFound then
       //if frmBaoGuanListBGZL=nil then
        begin
          frmCPManageGL := TfrmCPManage.Create(application.MainForm);
          with frmCPManageGL do
          begin
            Title := '样品信息管理';
            caption := Trim(Title);
            FormStyle := mstyle;
            windowState := mstate;
            BorderStyle := mborderstyle;
           //show;
          end;
        end
        else
          frmCPManageGL.BringToFront;
       //句柄
        mnewHandle := frmCPManageGL.Handle;
      end;
    3:  // 样品信息录入
      begin
        bFound := FALSE;
        for i := (App.MainForm.MDIChildCount - 1) downto 0 do
        begin
          if App.MainForm.MDIChildren[i].Caption = '样品信息录入' then
          begin
            BringWindowToTop(frmCPManageLR.Handle);
            bFound := TRUE;
            Break;
          end;
        end;
        if not bFound then
       //if frmBaoGuanListBGZL=nil then
        begin
          frmCPManageLR := TfrmCPManage.Create(application.MainForm);
          with frmCPManageLR do
          begin
            Title := '样品信息录入';
            caption := Trim(Title);
            FormStyle := mstyle;
            windowState := mstate;
            BorderStyle := mborderstyle;
           //show;
          end;
        end
        else
          frmCPManageLR.BringToFront;
       //句柄
        mnewHandle := frmCPManageLR.Handle;
      end;
    4:  // 样品信息查询
      begin
        bFound := FALSE;
        for i := (App.MainForm.MDIChildCount - 1) downto 0 do
        begin
          if App.MainForm.MDIChildren[i].Caption = '样品信息查询' then
          begin
            BringWindowToTop(frmCPManageCX.Handle);
            bFound := TRUE;
            Break;
          end;
        end;
        if not bFound then
       //if frmBaoGuanListBGZL=nil then
        begin
          frmCPManageCX := TfrmCPManage.Create(application.MainForm);
          with frmCPManageCX do
          begin
            Title := '样品信息查询';
            caption := Trim(Title);
            FormStyle := mstyle;
            windowState := mstate;
            BorderStyle := mborderstyle;
           //show;
          end;
        end
        else
          frmCPManageCX.BringToFront;
       //句柄
        mnewHandle := frmCPManageCX.Handle;
      end;

  end;  // end case

  Result := mnewHandle;
//  NewDllApp := Application;
end;
//===========================================================
  //建立数据库连接池
//===========================================================

function ConnData(): Boolean;
var
  IniFile: TIniFile;
begin
  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SYSTEMSET.INI');
    PicSvr := IniFile.ReadString('SERVER', '服务器地址', '127.0.0.1');
    UserDataFlag := IniFile.ReadString('SERVER', '服务器地址类型', '-1');
  finally
    IniFile.Free;
  end;
  if not Assigned(DataLink_YPGL) then
    DataLink_YPGL := TDataLink_YPGL.Create(Application);
  try
    with DataLink_YPGL.ADOLink do
    begin
      if not Connected then
      begin
        Connected := false;
        ConnectionString := DConString;
        LoginPrompt := false;
        Connected := true;
      end;
    end;
    Result := true;
  except
    Result := false;
    application.MessageBox('数据库连接失败！', '错误', mb_Ok + MB_ICONERROR);
  end;

end;

//initialization
//  OldDllApp := Application;
//
//finalization
//  DataLink_YPGL.Free;
//  Application := OldDllApp;
initialization
  CoInitialize(nil);
  dxUnitsLoader.Initialize;


finalization
  DataLink_YPGL.Free;
  application := NewDllApp;
  dxUnitsLoader.Finalize;

end.

