U_server.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:5k
- unit U_server;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Db, DBTables, Grids, DBGrids, ExtCtrls, shellapi, Menus, Provider,
- ComCtrls, registry, ScktComp, StdCtrls, Buttons, AxCtrls, OleCtrls, vcf1, Winsock,
- DBCtrls;
- const
- CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
- WZGL_APP_NAME = 'WZGL_System';
- const
- WM_WZGLNOTIFY = WM_USER + 100; //自定义消息
- strNotifyTip = '物资管理应用服务器'; //状态区的提示信息
- ID_MAIN = 100; // 定义图标的ID
- type
- TfrmMain = class(TForm)
- Panel1: TPanel;
- PopupMenu: TPopupMenu;
- NClose: TMenuItem;
- NShowForm: TMenuItem;
- StatusBar1: TStatusBar;
- ListView: TListView;
- Database: TDatabase;
- procedure FormDestroy(Sender: TObject);
- procedure NCloseClick(Sender: TObject);
- procedure NShowFormClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- public
- function AddIcon(hwnd: HWND): Boolean; //在状态区添加图标
- function RemoveIcon(hwnd: HWND): Boolean; //从状态区移去图标
- procedure Notify(var Msg: TMessage); message WM_WZGLNOTIFY; //自定义消息处理函数
- procedure minimize(sender: Tobject); //定义最小化过程,赋给Application.OnMinimize;
- function DBConnect(aServerName, aDatabaseName, aUserName, aPassword: string): boolean;
- procedure CreateParams(var Params: TCreateParams); override;
- procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
- { Public declarations }
- end;
- var
- frmMain: TfrmMain;
- nid: NOTIFYICONDATA;
- i: integer;
- implementation
- uses WZGL_RDB, DBConnectFrm;
- {$R *.DFM}
- procedure TFrmMain.CreateParams(var Params: TCreateParams);
- begin
- inherited CreateParams(Params);
- Params.WinClassName := WZGL_APP_NAME;
- end;
- {处理“恢复”消息}
- procedure TFrmMain.RestoreRequest(var message: TMessage);
- begin
- if IsIconic(Application.Handle) = True then //窗体是否最小化
- Application.Restore //恢复窗体
- else
- Application.BringToFront; //提到前面显示
- end;
- //在状态区添加图标
- function TfrmMain.AddIcon(hwnd: HWND): Boolean;
- begin
- nid.cbSize := sizeof(NOTIFYICONDATA);
- nid.Wnd := hwnd;
- nid.uID := ID_MAIN;
- nid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
- nid.uCallbackMessage := WM_WZGLNOTIFY;
- nid.hIcon := LoadIcon(hInstance, 'MAINICON');
- strCopy(nid.szTip, strNotifyTip);
- AddIcon := Shell_NotifyIcon(NIM_ADD, @nid);
- end;
- //从状态区移去图标
- function TfrmMain.RemoveIcon(hwnd: HWND): Boolean;
- var
- nid: NOTIFYICONDATA;
- begin
- nid.cbSize := sizeof(NOTIFYICONDATA);
- nid.Wnd := hwnd;
- nid.uID := ID_MAIN;
- nid.uFlags := 0;
- RemoveIcon := Shell_NotifyIcon(NIM_DELETE, @nid);
- end;
- //自定义消息处理函数
- procedure TfrmMain.Notify(var Msg: TMessage);
- var
- Pt: TPoint;
- begin
- case msg.LParam of
- WM_RBUTTONDOWN: //当点击右键时,弹出快捷菜单
- begin
- SetForeGroundWindow(nid.wnd);
- GetCursorPos(Pt);
- Popupmenu.Popup(pt.x, pt.y);
- end;
- end;
- end;
- //定义最小化过程,赋给Application.OnMinimize;
- procedure TfrmMain.minimize(sender: Tobject);
- begin
- AddIcon(handle);
- ShowWindow(Application.handle, sw_hide)
- end;
- procedure TfrmMain.FormDestroy(Sender: TObject);
- begin
- RemoveIcon(handle);
- end;
- procedure TfrmMain.NCloseClick(Sender: TObject);
- begin
- close;
- end;
- procedure TfrmMain.NShowFormClick(Sender: TObject);
- begin
- RemoveIcon(handle);
- ShowWindow(Application.handle, SW_SHOWNORMAL);
- end;
- procedure TfrmMain.FormCreate(Sender: TObject);
- var
- serverName, databaseName, userName, password: string;
- begin
- readFromIni(serverName, databaseName, userName, password);//读取配置文件
- if not DBConnect(ServerName, DatabaseName, UserName, Password) then //测试连接
- if not DBConnectExecute(TfrmDBConnect) then
- Application.Terminate; //如果连接不成功,退出应用程序
- Application.OnMinimize := minimize;
- end;
- procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- if application.messagebox('请确认是否退出!', '服务器端应用程序监测', mb_iconinformation + mb_yesno) = IDno then
- abort;
- end;
- function TfrmMain.DBConnect(aServerName, aDatabaseName, aUserName, aPassword: string): boolean;
- begin
- Result := True;
- Database.connected := False;
- Database.DriverName := 'mssql'; //驱动名称
- Database.Params.Values['Server Name'] := aServerName; //服务器名
- Database.Params.Values['Database Name'] := aDatabaseName; //数据库名
- Database.Params.Values['User Name'] := aUserName;//登录数据库用户名
- Database.Params.Values['Password'] := aPassword; //相应数据库用户的密码
- try
- Database.Connected := True;
- except
- result := False;
- end;
- end;
- end.