Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期四, 12月 24, 2009

DCC32 參數說明

它的編譯器參數如下:
選項    描述
Aunit=alias 設置單元別名
B      編譯所有單元
CC     編譯控制台程序
CG     編譯圖形界面程序
Ddefines  編譯條件符號定義
Epath    可執行文件輸出路徑
Foffset   查找運行期間錯誤
GD     生成完整.Map文件
GP     生成.Map文件Public段
GS     生成.Map文件Segment段
H      輸出提示信息
Ipaths   文件包含路徑
J      生成.Obj目標文件
JP     生成C++類型.Obj目標文件
Kaddress  Set image base address
LEpath   包.BPL文件輸出路徑
LNpath   .dcp文件輸出路徑
LUpackage  使用運行期間包列表
M      編譯有改動的源文件
Npath    dcu/dpu文件輸出目錄
Opaths   .Obj文件(彙編目標代碼文件)路徑
P      按8.3格式文件名查找
Q      安靜模式
Rpaths   資源文件(.RES)路徑
TXext    目標文件擴展名
Upaths   單元文件路徑
V      為Turbo Debugger生成調試信息文件
VN     以.Giant格式生成包含命名空間的調試信息文件(將用於C++Builder)
VR     生成調試信息文件.rsm
W      輸出警告信息
Z      Disable implicit compilation
$directive Compiler directives
--Help   顯示編譯選項的幫助。同樣的,如果你在命令行單獨輸入dcc32,也會顯示編譯選項的幫助。
--version  顯示產品名稱和版本

基本 DOS 批次處理詳細解說

一、echo@callpauserem 是批次檔案最常用的幾個命令,我們就從他們開始學起
echo
表示顯示此命令後的字元
echo off
表示在此語句後所有運行的命令都不顯示命令行本身
@
echo off相象,但它是加在其他命令行的最前面,表示運行時
不顯示命令行本身。
call
調用另一條批次檔案(如果直接調用別的批次檔案 ,執行完
那條檔後將無法執行當前檔後續命令)
pause
運行此句會暫停,顯示 Press any key to continue... 等待用戶
按任意鍵後繼續
rem
表示此命令後的字元為解釋行,不執行,只是給自己今後查找用的


二、if goto choice for 是批次檔案中比較高級的命令,如果這幾個你用 得很熟練,你就是批次檔案的專家啦。
if
表示將判斷是否符合規定的條件,從而決定執行不同的命令。 有三種格式:
1
if "參數" == "字串"  待執行的命令
參數如果等於指定的字串,則條件成立,運行命令,否則運行下一句。
(
注意是兩個等號)
如:if "%1"=="a" format a:
2
if exist 檔案名  待執行的命令
如果有指定的檔,則條件成立,運行命令,否則運行下一句。
如:if exist config.sys edit config.sys
3
if errorlevel 數位  待執行的命令
如果返回碼等於指定的數字,則條件成立,運行命令,否則運行下一句。
if errorlevel 2 goto x2  DOS程式運行時都會返回一個數位給DOS
稱為錯誤碼errorlevel或稱返回碼

goto
批次檔案運行到這裏將跳到goto 所指定的標號處,一般與 if 配合使用。
:
goto end
:end
echo this is the end
:
這是標號字元(所在位是冒號),是用來執行被 goto 跳轉所用的標號字元,
標號所在行是不會被執行,: :end 等等...

choice
使用此命令可以讓用戶輸入一個字元,從而運行不同的命令。使用時應該
/c:參數,c:後應寫提示可輸入的字元,之間無空格。它的返回碼為1234……
: choice /c:dme defrag,mem,end
將顯示
defrag,mem,end[D,M,E]?
例如,test.bat的內容如下:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag
應先判斷數值最高的錯誤碼
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
此檔運行後,將顯示 defrag,mem,end[D,M,E]? 用戶可選擇d m e
然後if語句將作出判斷,d表示執行標號為defrag的程式段,m表示執行
標號為mem的程式段,e表示執行標號為end的程式段,每個程式段最後都
goto end將程式跳到end標號處,然後程式將顯示good bye,檔結束。

for
迴圈命令,只要條件符合,它將多次執行同一命令。
格式FOR [%%f] in (集合) DO [命令]
只要參數f在指定的集合內,則條件成立,執行命令
如果一條批次檔案中有一行:
for %%c in (*.bat *.txt) do type %%c
含義是如果是以battxt結尾的檔,則顯示檔的內容。
device
devicehigh
載入一些記憶體駐留程序,用於管理設備。比如記憶體管理程式和
光碟機驅動程式等。
如: device=c:\dos\himem.sys
device=c:\dos\emm386.exe ram
devicehigh=c:\cdrom\cdrom.sys

himem.sys
emm386.exe
DOS
只能直接使用640K的記憶體,即基本記憶體,必須依靠其他記憶體管理程式來
使用更多的記憶體,這兩條命令就是最常用的記憶體管理程式。
himem.sys
負責管理擴展記憶體。
emm386.exe
負責管理高端記憶體並在擴展記憶體中類比延伸記憶體供某些軟體使用。
為了使用更多的記憶體,配置檔中應有:
device=c:\dos\himem.sys
device=c:\dos\emm386.exe ram
注意:EMM386.exe 要求先安裝 himem.sys,因而必須確保
安裝 himem.sys 的配置命令在 emm386.exe 之前。
dos=high,umb
這兩個參數也可以分開寫成兩條語句,即:
dos=high
dos=umb
應在config.sys中加入這條命令,這樣會將DOS的系統檔移入高端
記憶體,空出更多的基本記憶體給其他軟體使用。 注意,如果用戶沒有
安裝emm386.exeumb這個參數是沒有實際意義的
lastdriver lastdriver
規定用戶可以訪問的最大驅動器符數目,也就是DOS所能
識別的最後的驅動器符(字母)。如果設定的驅動器符數目小於本機上
的實際驅動器數,則此lastdriver命令會被忽略掉。缺省的最大驅動
器符數目為本機的實際驅動器數目加一。
::
二個冒號,它和批次檔案中的 rem 含義相同,即注釋
ramdrive.sys
虛擬磁片檔。
只要在 config.sys 中加入 device=ramdrive.sys 就可使用虛擬磁片了
比如:
   device=c:\dos\ramdrive.sys 1024 /e
表示在擴展記憶體中建立 1M 的虛擬磁片。如果加/a參數,則表示在擴充
記憶體中建立虛擬磁片。
%CDROM% %CDROM%
是光碟機盤符的變數字串,用它可以把所在的任意光碟機盤符替為真實盤符, 需要與光碟機驅動模組一同執行,:
LH \MSCDEX.EXE /D:mscd001 /L:%CDROM%
%RAMD% %RAMD%
是隨機虛擬磁片, 此盤創建在擴展記憶體中, 建立此盤的模組就是 ramdrive.sys ,見上面的虛擬磁片檔說明。
另外這個虛擬內在還有更強大的虛擬記憶體管理器,可以超過64MB的記憶體管理,就是 XMSDSK.EXE EMSDSK.EXE

星期三, 7月 01, 2009

讓程式不能重覆開啟


//專案檔最前面加上,1.檢查Mutex,若XMonitor已存在,則離開。
if ProgramAlreadyExists() then exit;

function ProgramAlreadyExists():boolean;
var
hRunningForm: Thandle;
begin
//g_hMutex:=CreateMutex(nil, False, 'X-Console');
g_hMutex:=CreateEvent(nil, False, TRUE, 'X-Monitor');
if (g_hMutex=0) OR (GetLastError()=ERROR_ALREADY_EXISTS) then //已存在
begin
hRunningForm := FindWindow(_XMONITOR_CLASS_NAME,nil);
if hRunningForm<>0 then
begin
SetForegroundWindow(hRunningForm);
// PostMessage(hRunningForm,WM_SYSCOMMAND,SC_MAXIMIZE,0);
end;

Result:=true;
end
else
Result:=false;
end;

星期二, 6月 16, 2009

壓縮mdb資料庫


function CompressDB(DBName: string): Boolean;
var
DaoVar: OLEVariant;
tmpDBName : string;
begin
if not FileExists(DBName) then
begin
Result := False;
exit;
end;

tmpDBName := ExtractFilePath(DBName) + 'temp.mdb';
if dm1.ADOConnection2.Connected then dm1.ADOConnection2.Connected := false; //如果資料庫打開,則要先關閉
try
try
DaoVar := CreateOleObject('DAO.DBEngine.36');
if FileExists(tmpDBName) then DeleteFile(tmpDBName); //刪除臨時資料庫"temp.mdb"
DaoVar.CompactDatabase(DBName, tmpDBName); //壓縮"dbName" 到"temp.mdb"
if DeleteFile(DBName) then //刪除文件"dbName"
RenameFile(tmpDBName, DBName); //將"temp.mdb"改名為"dbName "
Result := true;
except
Result := false;
end;
finally
// dm1.ADOConnection2.Connected := true; //為了其它操作能夠正常執行,還要打開資料庫連接。
end;
end;

星期四, 4月 30, 2009

取得最上層的視窗名稱及程序名稱


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, psAPI, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function GetProcessPath(PID : Integer): String;
var
hProcess: THandle;
hMod : hModule;
cbNeeded: DWORD;
szProcessName: array[0..1024] of Char;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ,
FALSE, PID );
szProcessName := 'unknown';
if (hProcess<>0) then
begin
if (EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded)) then
begin
GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName));
Result := StrPas(szProcessName);
end;
end;
CloseHandle(hProcess);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
wnd : THandle;
szText: array[0..254] of char;
ThreadId: DWORD;
FileName:string;
begin
wnd:= GetForegroundWindow;
if wnd <> 0 then
begin
if GetWindowText(wnd, @szText, 255)>0 then
Label1.caption := szText;

GetWindowThreadProcessId(wnd, ThreadId);
FileName:=GetProcessPath(ThreadId);
Label2.caption :=FileName;
end;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
Button1.Click;
end;

end.

星期六, 4月 04, 2009

讓滑鼠游標漏辦不要再現


{$R *.RES}
begin
Application.Initialize;
Screen.Cursors[crHourGlass] := Screen.Cursors[crDefault];
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

星期五, 3月 27, 2009

建立classname,然後可以幫助判斷該classname是否關閉

註冊classname

public
procedure createparams(var params:tcreateparams);override;

const
TSFPatch729ClassName = 'abcde';

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
StrCopy(Params.WinClassName, _TSFPatch729ClassName);
end;

判斷classname

const
TSFPatch729ClassName = 'abcde';

begin
while isWindow(FindWindow(_TSFPatch729ClassName,nil)) do
begin
sleep(300);
end;
end;

星期一, 3月 23, 2009

檢查日期時間合不合法


procedure CheckDate(var nYear: Integer; var nMonth: Integer;
var nDay: Integer);
begin
//Check day
case nMonth of
1, 3, 5, 7, 8, 10, 12: begin
//31 days
if(nDay >=31) then
begin
//進位
nMonth:=nMonth+1;
nDay:=nDay-31;
if(nMonth = 2) then
begin
if(nDay >= 28) then
//還要進位
CheckDate(nYear, nMonth, nDay);
end
else if(nDay >= 30) then
//還要進位
CheckDate(nYear, nMonth, nDay);
end
else if(nDay < 0) then
begin
//退位
nMonth:=nMonth-1;
//if(nMonth
end;
end;//break
4, 6, 9, 11: begin
//30 days
end;//break
2: begin
if(((nYear-1900) mod 4) = 0) then
begin
//29 days
end
else
begin
//29 days
end;
end;//break
end;
//Check month
if(nMonth > 12) then
begin
Inc(nYear);
Dec(nMonth, 12);
end
else if(nMonth < 0) then
begin
Dec(nYear);
nMonth:=12+nMonth;
end;
end;
//---------------------------------------------------------------------------

procedure CheckDateTime(var nYear: Integer; var nMonth: Integer;
var nDay: Integer; var nHour: Integer; var nMin: Integer;
var nSec: Integer; var nMSec: Integer);
var
a: Integer;

begin
//Check Millisecond.
if(nMSec >= 1000) then
begin
//進位
a:=nMSec div 1000;
nSec:=nSec+a;
nMSec:=nMSec-a*1000;
end
else if(nMSec < 0) then
begin
//退位
a:=nMSec div 1000;
Inc(a);
nSec:=nSec-a;
nMSec:=a*1000+nMSec;
end;
//Check second.
if(nSec >= 60) then
begin
//進位
a:=nSec div 60;
nMin:=nMin+a;
nSec:=nSec-a*60;
end
else if(nSec < 0) then
begin
//退位
a:=nSec div 60;
Inc(a);
nMin:=nMin-a;
nSec:=a*60+nSec;
end;
//Check minute.
if(nMin >= 60) then
begin
//進位
a:=nMin div 60;
nHour:=nHour+a;
nMin:=nMin-a*60;
end
else if(nMin < 0) then
begin
//退位
a:=nMin div 60;
Inc(a);
nHour:=nHour-a;
nMin:=a*60+nMin;
end;
CheckDate(nYear, nMonth, nDay);
end;
//---------------------------------------------------------------------------

檔案的時間轉系統時間


function FileTimeToDateTime(var ft: FILETIME): TDateTime;
var
//ft2: FILETIME;
st: SYSTEMTIME;

begin
Result:=0;
if((ft.dwHighDateTime = 0) and (ft.dwLowDateTime = 0)) then
begin
Exit;
end;
//if(not FileTimeToLocalFileTime(ft, ft2)) then
//begin
// Exit;
//end;
//if(not FileTimeToSystemTime(ft2, st)) then
// Exit;
if(not FileTimeToSystemTime(ft, st)) then
Exit;
Result:=SystemTimeToDateTime(st);
end;

星期三, 3月 18, 2009

createFile鎖定且會closehandle刪除


hfile := CreateFile('C:\TWO.TXT', // open TWO.TXT
GENERIC_WRITE, // open for writing
0, // do not share
0, // no security
OPEN_ALWAYS, // open or create
FILE_FLAG_DELETE_ON_CLOSE,//FILE_ATTRIBUTE_NORMAL, // normal file
0); // no attr. template




unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
hfile:THandle;

public
{ Public declarations }
end;

FileHeader = packed record //25 bytes
uid : int64;
mode : array[1..5] of Char; //模式
begin_time : TDateTime; //開始錄影時間
end_time : TDateTime; //結束錄影時間
total_frame : DWord; //總共Frame 0~65535
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


function IsFileInUse(FileName: TFileName): Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if not FileExists(FileName) then Exit;
HFileRes := CreateFile(PChar(FileName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
uidHeader : int64;
BytesRead : Cardinal; //讀寫用參數
begin
hfile := CreateFile('C:\TWO.TXT', // open TWO.TXT
GENERIC_READ, // open for READ
0, // do not share
0, // no security
OPEN_ALWAYS, // open or create
FILE_FLAG_DELETE_ON_CLOSE,//FILE_ATTRIBUTE_NORMAL, // normal file
0); // no attr. template

ReadFile(hFile, uidHeader, SizeOf(uidHeader), BytesRead, nil);
if uidHeader=23342542 then
caption :='good';

//ShowMessage(buf);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
caption := booltostr(IsFileInUse('C:\TWO.TXT'));
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
closehandle(hfile);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
a_fo : TSTReam;
h : FileHeader;
begin
a_fo:=TFileStream.Create('C:\TWO.txt',fmCreate);
h.uid := 23342542;
lstrcpy(@h.mode[1], pchar('abcde'));
a_fo.Write(h, sizeof(h));
a_fo.free;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
a_fo : TSTReam;
h : FileHeader;
z : int64;
begin
a_fo:=TFileStream.Create('C:\TWO.txt',fmOpenRead);
a_fo.Position := 0;
a_fo.Read(h, sizeof(h));
z:= h.uid;
a_fo.free;

end;

procedure TForm1.Button6Click(Sender: TObject);
var
uidHeader : int64;
BytesRead, BytesWrite : Cardinal; //讀寫用參數
pBuf:array[0..1024-1]of byte;
nFileSize : integer;
begin
hfile := CreateFile('C:\BT626.pdf', // open TWO.TXT
GENERIC_WRITE, // open for writing
0, // do not share
0, // no security
OPEN_ALWAYS, // open or create
FILE_ATTRIBUTE_NORMAL,//FILE_ATTRIBUTE_NORMAL, // normal file
0); // no attr. template
nFileSize:= GetFileSize(hfile,nil);

caption := inttostr(nFileSize);

ReadFile(hFile, pBuf, nFileSize, BytesRead, nil);
SetFilePointer(hFile,0,nil,FILE_End);
WriteFile(hFile,pBuf,nFileSize,BytesWrite,nil);
closehandle(hfile);
end;

end.

星期一, 3月 16, 2009

判斷檔案是否被鎖住


function IsFileInUse(FileName: TFileName): Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if not FileExists(FileName) then Exit;
HFileRes := CreateFile(PChar(FileName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;

星期五, 3月 13, 2009

取得磁碟剩餘空間


var
g_sTempDriver : integer;
DiskFreeSize : int64;
begin
g_sTempDriver := Ord(UpperCase(g_sTempPath)[1])-64; //UpperCase(g_sTempPath)[1]某個槽
DiskFreeSize := DiskFree(g_sTempDriver);

//DiskFreeSize div 1024 = bytes

取得APP PATH


procedure TForm1.FormCreate(Sender: TObject);
begin
caption := extractfilepath(ParamStr(0));
end;

星期四, 3月 05, 2009

避免Delphi的TDateTimePicker的onChange事件會執行二次


procedure TForm1.DateTimePicker1Change(Sender: TObject);
begin
if (DateTimePicker1.DroppedDown) then
exit;
DateTimePicker1CloseUp(sender);
end;

procedure TForm1.DateTimePicker1CloseUp(Sender: TObject);
begin
//你不想執行二次的程式碼
end;

星期五, 2月 27, 2009

Form要趨動按鍵有功能

屬性keypreview設為TRUE
接著即可在onkeypressj寫事件囉~

星期二, 2月 24, 2009

windows各種狀態判斷

判斷是否正在螢幕保護程式

function GetScreenSaverRunning : boolean;
var
res : integer
begin
SystemParametersInfo(GETSCREENSAVERRUNNING , 0, @res, 0);
Result := boolean(res);
end;


判斷是否正在電腦鎖定(螢幕保護及CTRL+ALT+DEL跳出另外的視窗也會判斷到)

function IsWorkstationLocked: Boolean;
var
hDesktop: HDESK;
begin
Result := False;
hDesktop := OpenDesktop('default', 0, False, DESKTOP_SWITCHDESKTOP);
if hDesktop <> 0 then
begin
Result := not SwitchDesktop(hDesktop);
CloseDesktop(hDesktop);
end;
end;


判斷是否正在關機、待命、休眠或關閉硬碟

function TEncodeVideoWindowProc(hWnd:HWND; iMsg:Integer; wParam:WPARAM; lParam:LPARAM): Integer; stdcall;
begin
Result:=0;
case iMsg of
WM_DESTROY :
begin
PostQuitMessage(0); //跟Windows說我要退出囉。
exit;
end;

WM_POWERBROADCAST: begin
case wParam of
PBT_APMQUERYSUSPEND:begin
//LogToFile('收到待命、休眠或關閉硬碟的須求');
end;
PBT_APMRESUMESUSPEND:begin
//LogTofile('收到電腦從待命、休眠或關閉硬碟返回的須求,及前錄影格數');
end;
end;

end;

WM_QUERYENDSESSION:
begin
;
end;
end;

Result := DefWindowProc(hWnd,iMsg,wParam,lParam); //不感興趣的消息,由windows去處理
end;

星期三, 2月 18, 2009

cxgrid 欄位 重新排序的popmenu

表單上放上TcxGridPopupMenu元件

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
cxGridTableView, cxGridDBTableView, cxGridCustomPopupMenu,
cxGridPopupMenu, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
cxGrid, cxGridStdPopupMenu;

type
TMenuStringForCxGrid = class(TObject)
class procedure HeaderMenuProc(Sender:TObject);
end;

type
TForm1 = class(TForm)
cxGrid1: TcxGrid;
cxGDBTV1: TcxGridDBTableView;
cxGLv1: TcxGridLevel;
cxGDBTV1Column1: TcxGridDBColumn;
cxGDBTV1Column2: TcxGridDBColumn;
cxGDBTV1Column3: TcxGridDBColumn;
cxGridPopupMenu1: TcxGridPopupMenu;
procedure cxGridPopupMenu1Popup(ASenderMenu: TComponent;
AHitTest: TcxCustomGridHitTest; X, Y: Integer;
var AllowPopup: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
function PopCxGridMenuProc(ASenderMenu: TComponent):boolean;

implementation

{$R *.dfm}

class procedure TMenuStringForCxGrid.HeaderMenuProc(Sender: TObject);
var
HM:TcxGridStdHeaderMenu;
begin
HM:=TcxGridStdHeaderMenu(Sender);
if HM.Items.Count<>17 then exit;
HM.Items[0].Caption:='&'+'遞增排序';//遞增排序
HM.Items[1].Caption:='&'+'遞減排序';//
HM.Items[2].Caption:='&'+'取消排序';//

HM.Items[4].Caption:='&'+'以此欄位分組';//
HM.Items[5].Caption:='&'+'分組視窗';//
HM.Items[6].Visible:=false;
HM.Items[7].Visible:=false;
HM.Items[8].Visible:=false;

HM.Items[10].Caption:='&'+'隱藏此欄位';//
HM.Items[11].Caption:='&'+'欄位選擇視窗';//

HM.Items[13].Visible:=false;
HM.Items[14].Caption:='&'+'最適欄寬';//
HM.Items[15].Visible:=false;
HM.Items[16].Caption:='&'+'最適欄寬(所有欄位)';//

end;

function PopCxGridMenuProc(ASenderMenu: TComponent):boolean;
begin
if ASenderMenu is TcxGridStdHeaderMenu then
TcxGridStdHeaderMenu(ASenderMenu).OnPopup := TMenuStringForCxGrid.HeaderMenuProc;
result:=true;
end;

procedure TForm1.cxGridPopupMenu1Popup(ASenderMenu: TComponent;
AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean);
begin
PopCxGridMenuProc(ASenderMenu);
end;

end.

星期三, 1月 21, 2009

使用blowfish加解密


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
g_LibHandle : THandle ; // dynamic load library
BlowfishEncodeString : function(lpszString: PChar; lpszKey: PChar;lpszBuffer: PChar; dwBufferSize: DWORD): DWORD; stdcall ;
BlowfishDecodeString : function(lpszEncodedString: PChar; lpszKey: PChar;lpszBuffer: PChar; dwBufferSize: DWORD): DWORD; stdcall ;
CalcCrc32 : function(pBuffer: Pointer; dwBufferSize: DWORD): DWORD; stdcall ;
//加解密
function EncrypBlowfishPassword(sPassword : String): String; //傳入欲加密字串,傳回加密後的字串 {Blowfish}
function DecrypBlowfishPassword(sEncrypedPWD : String): String; //傳入加密過的密碼,傳回解密後的明文密碼 {Blowfish}
const
_BLOWFISH_KEY = 'FORT_BLOWFISH_KEY'; //使用BlowFish 加密的Key

implementation

{$R *.dfm}
//----------------------------------------------------------------------------//
// LoadAllLibrary 載入所需Dll //
// 傳回值 : TRUE 載入成功 //
// False 載入失敗 //
//----------------------------------------------------------------------------//
function LoadAllLibrary():boolean ;
var
sPathName :string ;
begin
Result := true ;
sPathName := 'C:\facrypto.dll' ;
g_LibHandle := LoadLibrary(PAnsiChar(sPathName)) ;
if( g_LibHandle = 0 ) then
begin
showmessage('載入 DLL 發生錯誤');
Result := false ;
Exit ;
end ;

//加密
@BlowfishEncodeString := GetprocAddress( g_LibHandle, 'BlowfishEncodeString') ;

//解密
@BlowfishDecodeString := GetprocAddress( g_LibHandle, 'BlowfishDecodeString') ;

//CRC32
@CalcCrc32 := GetprocAddress( g_LibHandle, 'CalcCrc32') ;

//若三者有一出錯,即傳回False
if( (@BlowfishEncodeString=NIL) OR (@BlowfishDecodeString=NIL) OR (@CalcCrc32=NIL) ) then
begin
showmessage('載入 DLL 發生錯誤');
Result := false ;
Exit ;
end ;
end ;

procedure FreeAllLibrary() ;
begin
FreeLibrary(g_LibHandle) ;
end ;

//----------------------------------------------------------------------------//
// EncrypBlowfishPassword 加密欲加密的密碼 //
// 參數 : 明文字串 //
// 傳回值 : 加密後的字串 //
//----------------------------------------------------------------------------//
function EncrypBlowfishPassword(sPassword : String): String;
var
cEncrypt : array[0..255] of Char;

begin

try
{ encrypt } // 欲加密字串, 加密參數 ,加密後的秘文,buffer長度
BlowfishEncodeString( PChar(sPassword), PChar(_BLOWFISH_KEY),cEncrypt, 256) ;

//轉回字串後傳回
Result := string(cEncrypt);
except
//InfoMsgBox('Encryp Error!!');
Exit;
end;

end;

//----------------------------------------------------------------------------//
// DecrypBlowfishPassword 解開被加密後的密文 //
// 參數 : 密文字串 //
// 傳回值 : 解密後的明文字串 //
//----------------------------------------------------------------------------//
function DecrypBlowfishPassword(sEncrypedPWD : String): String;
var
cDecrypt : array[0..255] of Char;

begin

try
//解密
BlowfishDecodeString(PChar(sEncrypedPWD),PChar(_BLOWFISH_KEY), cDecrypt,255) ;

//轉回字串後 傳回
Result := string(cDecrypt);
except
//InfoMsgBox('Decry Error!!');
exit;
end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if not LoadAllLibrary() then
begin
showmessage('Error occurred while loading library') ;
exit;
end ;
caption := EncrypBlowfishPassword('kobe');
FreeAllLibrary() ; // free all library
end;

end.