请问如何让TWebBrowser变成只读的?(200分)

  • 主题发起人 主题发起人 w_anghe
  • 开始时间 开始时间
W

w_anghe

Unregistered / Unconfirmed
GUEST, unregistred user!
我用TWebBrowser显示一个word文件的内容,但我不希望用户能对这个文件操作,
比如复制,粘贴,编辑,请问各位大侠有没有办法?
 
我做过类似的东西,主要也是在大富翁找了一些资料,基本能屏蔽各种操作,只让用户能浏览word文档。
一个是屏蔽掉鼠标,另一个是注册掉快捷键

1、屏蔽鼠标单元(不知道哪位大侠的,拷贝过来也没理解能用)

unit hook;

interface

uses
forms, extctrls, Windows, Messages, stdctrls, Sysutils, Classes;

var
hkMouse: HHOOK;
hkKey: HHOOK;
f_wnd: Hwnd;
f_rect: TRect;
f_form: TForm;
f_panel: TPanel;

function MouseHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT; stdcall;
function EnableMouseHook(a_form: TForm; a_panel: Tpanel; a_wnd: Hwnd):Boolean; stdcall; export;
function DisableMouseHook:Boolean; stdcall; export;

implementation

function MouseHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
var
m: PMOUSEHOOKSTRUCT;
v_rect: TRect;
PEvt: ^EVENTMSG;
begin
Result := 0;
if nCode < 0 then
begin
Result := CallNextHookEx(hkMouse,nCode,WParam,LParam);
exit;
end;

case wParam of
WM_RBUTTONDOWN,
WM_RBUTTONUP,
WM_LBUTTONDOWN,
WM_LBUTTONUP:
begin
m := PMOUSEHOOKSTRUCT(lParam);
v_rect := f_panel.BoundsRect;
MapWindowPoints(f_form.handle, 0, v_rect, 2); // 座标换算
Dec(v_rect.Right, 18);
Dec(v_rect.Bottom, 18);

// 如果鼠标在v_rect范围内
if PtInRect(v_rect, Point(m.pt.x, m.pt.y)) then
// 如果Form处于激活状态
if GetActiveWindow=f_form.handle then
begin
Result := 1; //丢弃消息
exit;
end;
end;
end;
Result := CallNextHookEx(hkMouse, nCode, wParam, lParam);
end;


function EnableMouseHook(a_form: TForm; a_panel: Tpanel; a_wnd: Hwnd):Boolean; stdcall; export;
begin
f_form := a_form;
f_panel := a_panel;
f_wnd := a_wnd;
if hkMouse = 0 then
begin
hkMouse := SetWindowsHookEx(14, @MouseHookProc,Hinstance,0); //使用低级鼠标钩子
Result := True;
end
else
Result := False;
end;

function DisableMouseHook:Boolean; stdcall; export;
begin
if hkMouse <> 0 then
begin
UnHookWindowsHookEx(hkMouse);
hkMouse := 0;
Result := True;
end
else
Result := False;
end;

二、主单元(包括注册快捷键)
窗体中有一pannel,然后webboowser放在上面
unit look;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, LEADVCL,LtVclDef, ltVcltyp,LTLCKVCL,
OleCtrls, SHDocVw, OleCtnrs, Buttons;

type
TlookForm = class(TForm)
Panel1: TPanel;
WebBrowser1: TWebBrowser;
Panel2: TPanel;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY;
public
{ Public declarations }
end;
type
TWBPosition = (wbPosBottom, wbPosTop, wbPosRight);

var
lookForm: TlookForm;
v_wnd: HWND;
v_rec: TRect;
temp:integer;
HotKeyId,HotKeyId1,HotKeyId2: Integer;
implementation
uses hook;
{$R *.dfm}


procedure TlookForm.HotKeyDown(var Msg: Tmessage);
begin
if (Msg.LparamLo = MOD_CONTROL) AND (Msg.LParamHi =ord('A')) then //
begin
showmessage('查看word文档');
end;
if (Msg.LparamLo = MOD_CONTROL) AND (Msg.LParamHi =ord('C')) then //
begin
showmessage('查看word文档');
end;
if (Msg.LparamLo = MOD_CONTROL) AND (Msg.LParamHi =ord('X')) then //
begin
showmessage('查看word文档');
end;
end;

procedure TlookForm.FormCreate(Sender: TObject);
begin
HotKeyId := GlobalAddAtom('MyHotKey');
HotKeyId1 := GlobalAddAtom('MyHotKey1');
HotKeyId2 := GlobalAddAtom('MyHotKey2');
RegisterHotKey(Handle, hotkeyid, MOD_CONTROL, ord ('A'));
RegisterHotKey(Handle, hotkeyid1, MOD_CONTROL, ord ('C'));
RegisterHotKey(Handle, hotkeyid2, MOD_CONTROL, ord ('X'));
v_wnd := GetActiveWindow;
end;

procedure TlookForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DisableMouseHook;
UnRegisterHotKey(handle, HotKeyId);
UnRegisterHotKey(handle, HotKeyId1);
UnRegisterHotKey(handle, HotKeyId2);
end;

procedure TlookForm.FormShow(Sender: TObject);
begin
// EnableKeyHook(v_wnd);
EnableMouseHook(lookForm, Panel1, v_wnd);
end;
end.

你可以参考一下。

 
收藏,谢谢!楼上的主页正在建设当中的呀!
 
谢谢tgdjw:)
但是按你提供的方法,我只能屏蔽ctrl+c,ctrl+v,ctrl+x
我试着在HotKeyDown但也加入
if (Msg.LparamLo = MOD_CONTROL) AND (Msg.LParamHi =ord('N')) then //
begin
showmessage('查看word文档');
end;
也不能屏蔽ctrl+N
在word文档中也可以录入
 
这样子不能根本上解决问题,你可以在WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);事件里取得word对象的接口:_Document
然后想怎么对word控制都行了。
QueryInterface(_Document, Doc);
 
谢谢chaha,能否告诉我怎么利用_Document接口来让word成为只读的,我查找了_Document
还是没有发现怎么可以让word变成只读的,谢谢chaha.
 
to w_anghe:

怎么说你呢,你第二次问关于ctrl+N的问题感觉又好笑又可气,你仿造我的方法加上不就可以了吗?不会连这个也不会吧,需要在下面添上ctrl+n

procedure TlookForm.FormCreate(Sender: TObject);
begin
HotKeyId := GlobalAddAtom('MyHotKey');
HotKeyId1 := GlobalAddAtom('MyHotKey1');
HotKeyId2 := GlobalAddAtom('MyHotKey2');
HotKeyId2 := GlobalAddAtom('MyHotKey3');
RegisterHotKey(Handle, hotkeyid, MOD_CONTROL, ord ('A'));
RegisterHotKey(Handle, hotkeyid1, MOD_CONTROL, ord ('C'));
RegisterHotKey(Handle, hotkeyid2, MOD_CONTROL, ord ('X'));
RegisterHotKey(Handle, hotkeyid3, MOD_CONTROL, ord ('N'));

v_wnd := GetActiveWindow;
end;
方法虽然不好,但不会你连这个也不会加吧。
你想即使你把文件改为只读,用户仍可以复制,所以你所要求的功能并不是只读的问题。
 
不好意思tgdjw大侠,我忘了注册热键:)
 
其实我的本意是让word只能查看,不能复制,编辑,说到底就是做一个word
的只读浏览器,像把pdf文件那样不能修改,暂时不考虑文件复制的情况
我本来想用超文本,可我们客户就喜欢word格式,我也没有办法
无论如何谢谢各位大侠,特别是tgdjw
如果没有其他答案再过两天就给分了
 
web:TWebBrowser

function TWordEditForm.GetDocument: _Document;
var
Doc: _Document;
begin
if Web.Document=nil then
Result := nil
else begin
Web.Document.QueryInterface(_Document,Doc);
Result := Doc;
end;
end;
保存:
procedure TWordEditForm.ProtectDocument;
begin
if not Assigned(FDocument) then Exit;
if (FDocument.ProtectionType <> -1) then
FDocument.Unprotect(EmptyParam);
FDocument.Protect(wdAllowOnlyComments, EmptyParam, EmptyParam);
end;
你可以在word->工具->保护文档 菜单项设置,看到的就是这种效果。
 
谢谢各位帮忙,非常感谢,问题已解决[:)]
 
后退
顶部