본문 바로가기
Delphi Tip/이미지-영상

Timage에 외부 이미지 파일/txt파일 드래그&드롭으로 가져오기

by MonoSoft 2021. 10. 19.
728x90
반응형

Timage에 외부 이미지 파일/txt파일 드래그&드롭으로 가져오기

 

unit Unit1;

 

interface

 

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,

System.Classes, Vcl.Graphics,

Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,

{Units Added}

Vcl.Imaging.jpeg,

Vcl.Imaging.pngimage,

Winapi.ShellAPI;

{Units Added}

 

type

TForm1 = class(TForm)

edt1: TEdit;

img1: TImage;

mmo1: TMemo;

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

procedure Drag_Drop_File(var Msg: TMessage); message WM_DROPFILES;

end;

 

var

 

Form1: TForm1;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.Drag_Drop_File(var Msg: TMessage);

var

extension: string;

limit, number: Integer;

path: array [0 .. MAX_COMPUTERNAME_LENGTH + MAX_PATH] of Char;

begin

limit := DragQueryFile(Msg.WParam, $FFFFFFFF, path, 275) - 1;

{

if the index value is 0xFFFFFFFF,

the return value is a count of the dropped files.

}

for number := 0 to limit do

begin

DragQueryFile(Msg.WParam, number, path, 275);

{

if the index value is between zero and the total number of dropped files,

the return value is the required size, in characters.

}

if (FileExists(path)) then

begin

extension := ExtractFileExt(path);

// Extracts the extension part of path like [.jpg, .png, .txt]

extension := StringReplace(extension, '.', '',

[rfReplaceAll, rfIgnoreCase]);

// Try to replase '.' to ''.

if (extension = 'jpg') or (extension = 'png') or (extension = 'bmp') then

begin

edt1.Text := path;

img1.Picture.LoadFromFile(path);

end

else if (extension = 'txt') then

begin

edt1.Text := path;

mmo1.Clear;

mmo1.Lines.LoadFromFile(path);

end

else

begin

MessageBox(Form1.Handle, PChar('The image or text is not valid'),

PChar('SwePC Drag & Drop'), MB_ICONWARNING);

end;

end;

 

end;

DragFinish(Msg.WParam);

// This frees the resources used to store information about the drop.

end;

 

procedure TForm1.FormCreate(Sender: TObject);

begin

DragAcceptFiles(Handle, True);

// Passing the handle of the window that is to receive WM_DROPFILES messages.

end;

end.

728x90
반응형

댓글