从资源管理器拖拽文件到RichEdit
我已经实现了从资源管理器拖拽文件到Form上,即用DragAcceptFiles(Form.Handle,True)并覆盖WndProc的方法,
但我发现当把文件拖到Form.RichEdit上时鼠标又变成了不能拖拽的形状,就好像RichEdit所在的地方就不属于Form的客户区了一样。
而奇怪的是,如果把RichEdit的ReadOnly属性设置为True,或者把Enabled设置为False,那么RichEdit所在的地方又可以接受拖拽文件了。
然后我又尝试了用DragAcceptFiles(RichEdit.Handle,True)并替换RichEdit.WindowProc的方法,结果还是一样,ReadOnly为False或者Enabled为True时不能接受拖拽,ReadOnly为True或者Enabled为False时可以接受拖拽。
我把RichEdit换成Memo按上述方法测试,没有出现这种情况。
我想问有没有什么解决办法,使文件拖拽到允许编辑的RichEdit上也能成功?
贴上代码:
- Delphi(Pascal) code
unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI, StdCtrls, ComCtrls;type TForm1 = class(TForm) Button1: TButton; RichEdit1: TRichEdit; procedure FormCreate(Sender: TObject); private { Private declarations } procedure DropFiles(var Msg:TMessage); protected procedure WndProc(var Msg:TMessage);override; public { Public declarations } end;var Form1: TForm1;implementationvar OldWndProc:TWndMethod;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);begin DragAcceptFiles({RichEdit1.}Handle,True);{ OldWndProc:=RichEdit1.WindowProc; RichEdit1.WindowProc:=DropFiles; }end;procedure TForm1.DropFiles(var Msg: TMessage);var hDrop:THandle;count,i:integer;buf:array[0..254]of char;begin if Msg.Msg=WM_DROPFILES then begin RichEdit1.Clear; hDrop:=Msg.WParam; count:=DragQueryFile(hDrop,$FFFFFFFF,nil,0); for i:=0 to count-1 do begin DragQueryFile(hDrop,i,buf,255); RichEdit1.Lines.Add(StrPas(buf)); end; DragFinish(hDrop); end else OldWndProc(Msg);end; procedure TForm1.WndProc(var Msg: TMessage);var hDrop:THandle;count,i:integer;buf:array[0..254]of char;begin if Msg.Msg=WM_DROPFILES then begin RichEdit1.Clear; hDrop:=Msg.WParam; count:=DragQueryFile(hDrop,$FFFFFFFF,nil,0); for i:=0 to count-1 do begin DragQueryFile(hDrop,i,buf,255); RichEdit1.Lines.Add(StrPas(buf)); end; DragFinish(hDrop); end; inherited;end;end.[解决办法]
是和OS有,我在XP下是OK的。
可以考一下Vista新引入的一叫Integrity levels西
[解决办法]
拖曳之前令ReadOnly为true,拖曳结束后令它为False,于是楼主问题解决。