首页  编辑  

屏蔽打开对话框的弹出菜单

Tags: /超级猛料/VCL/Common.Dialog.通用对话框和控件/   Date Created:

屏蔽打开对话框的弹出菜单

How to suppress right clicking in Open File dialogs

By Kingron

在打开对话框中,如果我想屏蔽弹出菜单禁止用户的某些操作,该怎么办?

该开始的时候,我Hook OpenDialog的本身的WM_RBUTTONDOWN, WM_RBUTTONUP, WM_CONTEXTMENU,怎么样都不起作用,后来发现ListView是有独立的消息循环的,和其容器没有任何关系,因此必须Hook那个ListView才可以,回头一看新闻组,Peter Below已经给出了例子,不愧是大牛。

unit Unit1;

interface

uses

 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

 Dialogs, StdCtrls;

type

 TForm1 = class(TForm)

   OpenDialog1: TOpenDialog;

   Button1: TButton;

   procedure Button1Click(Sender: TObject);

 private

   { Private declarations }

 public

   { Public declarations }

 end;

var

 Form1: TForm1;

implementation

{$R *.dfm}

Type

 TSafeOpenDialog = class( Dialogs.TOpenDialog )

 private

   FOldListviewProc: Pointer;

   FListviewMethodInstance: Pointer;

   FLIstview: HWND;

   Procedure WMApp( Var msg: TMEssage ); message WM_APP;

 protected

   procedure DoShow; override;

   Procedure ListviewWndProc( Var msg: TMessage );

 public

   Destructor Destroy; override;

 End;

destructor TSafeOpenDialog.Destroy;

begin

 inherited;

 If Assigned( FListviewMethodInstance ) Then

   FreeObjectInstance( FListviewMethodInstance );

end;

procedure TSafeOpenDialog.DoShow;

begin

 inherited;

 PostMessage( handle, WM_APP, 0, 0 );

end;

procedure TSafeOpenDialog.ListviewWndProc(var msg: TMessage);

begin

 msg.result := 0;

 Case msg.Msg Of

   WM_RBUTTONDOWN, WM_RBUTTONUP, WM_CONTEXTMENU:

     Exit;

 End; { Case }

 msg.result := CallWindowProc( FOldListviewProc, FLIstview,

                               msg.Msg, msg.WParam, msg.LParam );

end;

procedure TSafeOpenDialog.WMApp(var msg: TMEssage);

begin

 FListviewMethodInstance:= MakeObjectInstance(ListviewWndProc);

 FListview := FindWindowEx( Windows.GetParent( handle ),

                            0,

                            'SHELLDLL_DefView', nil );

 If FListview <> 0 Then Begin

   FListview := GetWindow( FListview, GW_CHILD );

   If FListview <> 0 Then

     FOldListviewProc := Pointer(

                           SetWindowLong( FListview, GWL_WNDPROC,

                                         

Integer(FListviewMethodInstance)))

   Else

     OutputDebugString('Listview not found');

 End

 Else

   OutputDebugString('Shell view not found');

end;

procedure TForm1.Button1Click(Sender: TObject);

var

 s : TSafeOpenDialog;

begin

 s := TSafeOpenDialog.Create(self);

 S.Execute;

 S.Free;

end;

end.