2020智能投顾平台TOP50
全世界各行各业联合起来,internet一定要实现!
老文章正文

教你在DELPHI中基础开发技巧

2004-03-03 eNet&Ciweek

  ◇[DELPHI]WebBrowser控件技巧
1。实现打印功能
var vaIn, vaOut: OleVariant;
WebBrowser.ControlInterface.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, vaIn, vaOut);
2。WebBrowser从流中读取页面
function Tform1.LoadFromStream(const AStream: TStream): HRESULT;
begin
AStream.seek(0, 0);
Result := (WebBrowser1.document.nbspas IPersistStreamInit).Load(TStreamAdapter.Create(AStream));
end;
3。"about:" protocol will let you Navigate to an HTML string:
procedure Tform1.LoadHTMLString(sHTML: String);
var Flags, TargetFrameName, PostData, Headers: OleVariant;
WebBrowser1.Navigate('about:' + sHTML, Flags, TargetFrameName, PostData, Headers)
4。"res:" protocol will let you Navigate to an HTML file stored as a resource. More informations is available from the Microsoft site:
procedure Tform1.LoadHTMLResource;
var Flags, TargetFrameName, PostData, Headers: OleVariant;
WebBrowser1.Navigate('res://' + Application.ExeName + '/myhtml', Flags, TargetFrameName, PostData, Headers)
使用brcc32.exe建立资源文件 (*.rc)
MYHTML 23 ".\html\myhtml.htm"
MOREHTML 23 ".\html\morehtml.htm"
{$R HTML.RES} //html.rc被编译成html.res
5。保存完整的HTML文件
var
HTMLdocument. IHTMLdocument.;
Persistfile&: IPersistFile;
begin
HTMLdocument.nbsp:= WebBrowser1.document.nbspas IHTMLdocument.;
PersistFile := HTMLdocument.nbspas IPersistFile;
PersistFile.Save(StringToOleStr('test.htm'), True);
while HTMLdocument.readyState <> 'complete' do
Application.ProcessMessages;
end;

  ◇[DELPHI]安装WebBrowser控件(内嵌IE控件)
你必须先确定系统已安装Internet Explorer4或以后版本,DELPHI菜单--Component- - Import ActiveX Contro,列表中选择Microsoft Internet Controls"并ADD到一个已存在的包文件中,WebBrowser控件将显示在ActiveX控件面板。

  ◇[DELPHI]实现windows2000半透明窗体
function SetLayeredWindowAttributes(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint; stdcall; external user32;//函数声明
procedure Tform1.formCreate(Sender: TObject);
var l:longint;
begin
l:=getWindowLong(Handle, GWL_EXstyle);
l := l Or $80000;
SetWindowLong (handle, GWL_EXstyle, l);
SetLayeredWindowAttributes(handle, 0, 180, 2);
end;

  ◇[DELPHI]程序显示广告WebBrowser加载图片
var Flag, frame, pData, Header: OLEVariant;
begin ', flag, frame,pData, Header)
end;

  ◇[DELPHI]计算一个目录的大小
function GetDirectorySize(const ADirectory: string): Integer;
var
Dir: TSearchRec;
Ret: integer;
Path: string;
begin
Result := 0;
Path := ExtractFilePath(ADirectory);
Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);
if Ret <> NO_ERROR then
exit;
try
while ret=NO_ERROR do
begin
inc(Result, Dir.Size);
//如果是目录,且不是'.'或'..'则进行递归调用
if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then
Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));
Ret := Sysutils.FindNext(Dir);
end;
finally
Sysutils.FindClose(Dir);
end;
end;

  ◇[DELPHI]清空一个目录
function EmptyDirectory(TheDirectory :String Recursive : Boolean) :
Boolean;
var
SearchRec : TSearchRec;
Res : Integer;
begin
Result := False;
TheDirectory := NormalDir(TheDirectory);
Res := FindFirst(TheDirectory + '*.*', faAnyFile, SearchRec);
try
while Res = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
if ((SearchRec.Attr and faDirectory) > 0) and Recursive
then begin
EmptyDirectory(TheDirectory + SearchRec.Name, True);
RemoveDirectory(PChar(TheDirectory + SearchRec.Name));
end
else begin
DeleteFile(PChar(TheDirectory + SearchRec.Name))
end;
end;
Res := FindNext(SearchRec);
end;
Result := True;
finally
FindClose(SearchRec.FindHandle);
end;
end;

  ◇[DELPHI]发布ADO程序之安装ADO
运行一次 MDac_typ.exe ,这个文件在微软的 Windows、IE、Office、Visual Studio 中都有。
安装程序所安装后的目录与程序中设置的目录路径一样,C:\Program Files\Common Files\System\ado文件夹中有没有ADO组件,装ACCESS2000就有ADO2.1,没有则安装MS OFfice2000,编译要去掉project->Option->Packages对话框中的Build With RunTime Library的勾。

  ◇[DELPHI]拦截Windows系统消息:WM_CLOSE消息
procedure WMClose(var Msg: TMessage);message WM_CLOSE;
procedure TMainform.WMClose(var Msg: TMessage);
begin
m_bCloseNoQuery := false;
inherited;
end;

相关频道: eNews 老文章

您对本文或本站有任何意见,请在下方提交,谢谢!

投稿信箱:tougao@enet16.com
广告