您的位置:首页 > 编程语言 > Delphi

Delphi基于HTML页面和XML实现表单填写

2010-04-13 22:36 441 查看
请先看效果,为了达到这个效果,需要完成HTML界面、XML数据存储结构定义、JavaScript数据验证和保存、Delphi实现桌面程序调用4个方面。桌面程序也可以用VB、VC、.NET来实现,道理类似。

代码

1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, ComCtrls, OleCtrls, SHDocVw, Menus, xmldom, XMLIntf, msxmldom,
8 XMLDoc, StdCtrls, ExtCtrls, DB, DBClient, ImgList, MSHTML, Activex;
9
10 type
11 TForm1 = class(TForm)
12 XMLDocument1: TXMLDocument;
13 Panel2: TPanel;
14 WebBrowser1: TWebBrowser;
15 Panel3: TPanel;
16 BtnSave: TButton;
17 procedure FormShow(Sender: TObject);
18 procedure BtnSaveClick(Sender: TObject);
19 procedure FormDestroy(Sender: TObject);
20 private
21 { Private declarations }
22 public
23 { Public declarations }
24 RootTreeNode: TTreeNode;
25 end;
26
27 var
28 Form1: TForm1;
29
30 implementation
31
32 {$R *.dfm}
33
34 {加载页面}
35 procedure TForm1.FormShow(Sender: TObject);
36 var
37 starthtmpath: string;
38 begin
39 OleInitialize(nil);
40
41 starthtmpath := ExtractFilePath(Application.ExeName) + 'AddWeiPainForm\AddWeiPianForm.htm';
42 //加载初始化页面
43 WebBrowser1.Navigate(starthtmpath);
44 end;
45
46 {保存}
47 procedure TForm1.BtnSaveClick(Sender: TObject);
48 var
49 xmlstr, xmlpath, temppath: string;
50 fp: integer;
51 begin
52 try
53 temppath := ExtractFilePath(Application.ExeName) + 'temp.txt';
54 xmlstr := webbrowser1.OleObject.Document.parentWindow.SaveData();
55 xmlpath := extractfilepath(application.exename) + 'AddWeiPainForm\AddWeiPainForm.xml';
56 if xmlstr <>'' then
57 begin
58 if FileExists(temppath) then
59 begin
60 deletefile(temppath);
61 end;
62
63 //创建临时文本文件
64 fp:=filecreate(temppath);
65 xmlstr:= StringReplace(xmlstr, '<?xml version="1.0"?>', '<?xml version="1.0" encoding="GB2312" ?>', [rfReplaceAll]);
66 filewrite(fp, xmlstr[1], length(xmlstr));
67 fileclose(fp);
68
69 XMLDocument1 := TXMLDocument.Create(Self);
70 XMLDocument1.LoadFromFile(temppath);
71 XMLDocument1.Active := true;
72
73 //保存基本信息
74 if FileExists(xmlPath) then
75 begin
76 DeleteFile(xmlPath);
77 end;
78 XMLDocument1.SaveToFile(xmlpath);
79
80 XMLDocument1.Free();
81 XMLDocument1 := nil;
82 Application.MessageBox(Pchar('保存成功!'), '提示', MB_ICONINFORMATION);
83 end;
84 except
85 on e: Exception do
86 Application.MessageBox(Pchar('保存失败!' + e.Message), '提示', MB_ICONERROR);
87 end;
88 end;
89
90 procedure TForm1.FormDestroy(Sender: TObject);
91 begin
92 OleUninitialize();
93 end;
94
95 end.

5、写到这里就基本完成了,有兴趣的朋友可以到http://download.csdn.net/source/2236926下载全部源代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: