您现在的位置:首页 >> VCL >> VCL >> 内容

Delphi中从Windows资源管理器中拖放图片到TImage控件上

时间:2011/9/3 16:23:23 点击:

  核心提示:600) this.width = 600;'>unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Gra...

600) this.width = 600;">

unit Unit1;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, ExtCtrls;

type
   TForm1 = class(TForm)
     Panel1: TPanel;
     Image1: TImage;
     procedure FormCreate(Sender: TObject) ;
   private
     originalPanelWindowProc : TWndMethod;
     procedure PanelWindowProc (var Msg : TMessage) ;
     procedure PanelImageDrop (var Msg : TWMDROPFILES) ;
   public
     { Public declarations }
   end;

var
   Form1: TForm1;

implementation
{$R *.dfm}

uses ShellApi;

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   originalPanelWindowProc := Panel1.WindowProc;
   Panel1.WindowProc := PanelWindowProc;

   DragAcceptFiles(Panel1.Handle,true) ;
end; (*FormCreate*)

procedure TForm1.PanelWindowProc(var Msg: TMessage) ;
begin
   if Msg.Msg = WM_DROPFILES then
     PanelImageDrop(TWMDROPFILES(Msg))
   else
     originalPanelWindowProc(Msg) ;
end; (*PanelWindowProc*)

procedure TForm1.PanelImageDrop(var Msg: TWMDROPFILES) ;
var
   numFiles : longInt;
   buffer : array[0..MAX_PATH] of char;
begin
   numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0) ;
   if numFiles > 1 then
   begin
     ShowMessage('You can drop only one image file at a time!') ;
   end
   else
   begin
     DragQueryFile(Msg.Drop, 0, @buffer, sizeof(buffer)) ;
     try
       Image1.Picture.LoadFromFile(buffer) ;
     except
       on EInvalidGraphic do ShowMessage('Unsupported image file, or not an image!') ;
     end;
   end;
end; (*PanelImageDrop*)

end.

作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号