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.
'Delphi Tip > 이미지-영상' 카테고리의 다른 글
이미지 마우스로 움직이기 (0) | 2024.01.04 |
---|---|
델파이 RGB 와 HSL 유틸 유닛 (0) | 2021.10.22 |
판넬 이미지파일 저장 (0) | 2021.10.21 |
이미지(JPG) 사이즈 구하기 (0) | 2021.10.20 |
델파이 API 애니매이션 효과내기 (0) | 2021.10.18 |
댓글