📄 pictedit.pas
字号:
function TGraphicPropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paDialog];
end;
function TGraphicPropertyEditor.GetValue: string;
var
Graphic: TGraphic;
begin
Graphic := TGraphic(GetOrdValue);
if (Graphic = nil) or Graphic.Empty then Result := ResStr(srNone)
else Result := '(' + Graphic.ClassName + ')';
end;
procedure TGraphicPropertyEditor.SetValue(const Value: string);
begin
if Value = '' then SetOrdValue(0);
end;
{ TGraphicsEditor }
procedure TGraphicsEditor.EditProperty(const Prop: IProperty; var Continue: Boolean);
var
PropName: string;
begin
PropName := Prop.GetName;
if (CompareText(PropName, 'PICTURE') = 0) or
(CompareText(PropName, 'IMAGE') = 0) or
(CompareText(PropName, 'GLYPH') = 0) then
begin
Prop.Edit;
Continue := False;
end;
end;
{ TPictureEditDialog }
procedure TPictureEditDialog.SetGraphicClass(Value: TGraphicClass);
begin
FGraphicClass := Value;
CheckEnablePaste;
DecreaseBox.Enabled := (GraphicClass = TBitmap) or (GraphicClass = TGraphic);
end;
procedure TPictureEditDialog.CheckEnablePaste;
begin
Paste.Enabled := EnablePaste(GraphicClass);
end;
procedure TPictureEditDialog.ValidateImage;
var
Enable: Boolean;
begin
Enable := ValidPicture(Pic);
Save.Enabled := Enable;
Clear.Enabled := Enable;
Copy.Enabled := Enable;
end;
{$IFDEF RX_D3}
procedure TPictureEditDialog.GraphicProgress(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);
begin
if Stage in [psStarting, psEnding] then begin
FProgressPos := 0;
FProgress.Position := 0;
end
else if Stage = psRunning then begin
if PercentDone >= FProgressPos + 10 then begin
FProgress.Position := PercentDone;
FProgressPos := PercentDone;
end;
end;
if RedrawNow then ImagePaintBox.Update;
end;
{$ENDIF}
procedure TPictureEditDialog.UpdateClipboard(Sender: TObject);
begin
CheckEnablePaste;
end;
procedure TPictureEditDialog.FormCreate(Sender: TObject);
begin
Pic := TPicture.Create;
{$IFDEF RX_D3}
FileDialog := TOpenPictureDialog.Create(Self);
SaveDialog := TSavePictureDialog.Create(Self);
UsePreviewBox.Visible := False;
FProgress := TProgressBar.Create(Self);
with FProgress do begin
SetBounds(UsePreviewBox.Left, UsePreviewBox.Top, UsePreviewBox.Width,
UsePreviewBox.Height);
Parent := Self;
Min := 0; Max := 100;
Position := 0;
end;
Pic.OnProgress := GraphicProgress;
{$ELSE}
FileDialog := TOpenDialog.Create(Self);
SaveDialog := TSaveDialog.Create(Self);
{$ENDIF}
FileDialog.Title := 'Load picture';
SaveDialog.Title := 'Save picture as';
{$IFDEF WIN32}
Bevel.Visible := False;
Font.Style := [];
with FormStorage do begin
UseRegistry := True;
IniFileName := SDelphiKey;
end;
{$ELSE}
if NewStyleControls then Font.Style := [];
{$ENDIF}
PathsMRU.RecentMenu := PathsMenu.Items;
FIconColor := clBtnFace;
HelpContext := hcDPictureEditor;
Save.Enabled := False;
Clear.Enabled := False;
Copy.Enabled := False;
FClipMonitor := TClipboardMonitor.Create(Self);
FClipMonitor.OnChange := UpdateClipboard;
CheckEnablePaste;
end;
function TPictureEditDialog.GetDecreaseColors: Boolean;
begin
Result := DecreaseBox.Checked;
end;
procedure TPictureEditDialog.FormDestroy(Sender: TObject);
begin
FClipMonitor.Free;
Pic.Free;
end;
procedure TPictureEditDialog.LoadFile(const FileName: string);
begin
Application.ProcessMessages;
StartWait;
try
Pic.LoadFromFile(FileName);
finally
StopWait;
end;
ImagePaintBox.Invalidate;
ValidateImage;
end;
procedure TPictureEditDialog.LoadClick(Sender: TObject);
{$IFNDEF RX_D3}
var
FileName: string;
{$ENDIF}
begin
{$IFNDEF RX_D3}
if UsePreviewBox.Checked then begin
FileName := '';
if DirExists(FileDialog.InitialDir) then
SetCurrentDir(FileDialog.InitialDir);
if SelectImage(FileName, GraphicExtension(GraphicClass),
GraphicFilter(GraphicClass)) then
begin
FileDialog.Filename := FileName;
Self.LoadFile(FileName);
end;
end
else begin
{$ENDIF}
if FileDialog.Execute then begin
Self.LoadFile(FileDialog.Filename);
end;
{$IFNDEF RX_D3}
end;
{$ENDIF}
end;
procedure TPictureEditDialog.SaveClick(Sender: TObject);
begin
if (Pic.Graphic <> nil) and not Pic.Graphic.Empty then begin
with SaveDialog do begin
DefaultExt := GraphicExtension(TGraphicClass(Pic.Graphic.ClassType));
Filter := GraphicFilter(TGraphicClass(Pic.Graphic.ClassType));
if Execute then begin
StartWait;
try
Pic.SaveToFile(Filename);
finally
StopWait;
end;
end;
end;
end;
end;
procedure TPictureEditDialog.DecreaseBMPColors;
begin
if ValidPicture(Pic) and (Pic.Graphic is TBitmap) and DecreaseColors then
SetBitmapPixelFormat(Pic.Bitmap, pf4bit, DefaultMappingMethod);
end;
procedure TPictureEditDialog.CopyClick(Sender: TObject);
begin
CopyPicture(Pic, FIconColor);
end;
procedure TPictureEditDialog.PasteClick(Sender: TObject);
begin
if (Pic <> nil) then begin
PastePicture(Pic, GraphicClass);
DecreaseBMPColors;
ImagePaintBox.Invalidate;
ValidateImage;
end;
end;
procedure TPictureEditDialog.ImagePaintBoxPaint(Sender: TObject);
var
DrawRect: TRect;
SNone: string;
{$IFDEF WIN32}
Ico: HIcon;
W, H: Integer;
{$ENDIF}
begin
with TPaintBox(Sender) do begin
Canvas.Brush.Color := Color;
DrawRect := ClientRect;
if ValidPicture(Pic) then begin
with DrawRect do
if (Pic.Width > Right - Left) or (Pic.Height > Bottom - Top) then
begin
if Pic.Width > Pic.Height then
Bottom := Top + MulDiv(Pic.Height, Right - Left, Pic.Width)
else
Right := Left + MulDiv(Pic.Width, Bottom - Top, Pic.Height);
Canvas.StretchDraw(DrawRect, Pic.Graphic);
end
else begin
with DrawRect do begin
{$IFDEF WIN32}
if Pic.Graphic is TIcon then begin
Ico := CreateRealSizeIcon(Pic.Icon);
try
GetIconSize(Ico, W, H);
DrawIconEx(Canvas.Handle, (Left + Right - W) div 2,
(Top + Bottom - H) div 2, Ico, W, H, 0, 0, DI_NORMAL);
finally
DestroyIcon(Ico);
end;
end else
{$ENDIF}
Canvas.Draw((Right + Left - Pic.Width) div 2,
(Bottom + Top - Pic.Height) div 2, Pic.Graphic);
end;
end;
end
else
with DrawRect, Canvas do begin
SNone := ResStr(srNone);
TextOut(Left + (Right - Left - TextWidth(SNone)) div 2, Top + (Bottom -
Top - TextHeight(SNone)) div 2, SNone);
end;
end;
end;
procedure TPictureEditDialog.CreateHandle;
begin
inherited CreateHandle;
DragAcceptFiles(Handle, True);
end;
procedure TPictureEditDialog.WMDestroy(var Msg: TMessage);
begin
DragAcceptFiles(Handle, False);
inherited;
end;
procedure TPictureEditDialog.WMDropFiles(var Msg: TWMDropFiles);
var
AFileName: array[0..255] of Char;
Num: Cardinal;
begin
Msg.Result := 0;
try
Num := DragQueryFile(Msg.Drop, {$IFDEF WIN32} $FFFFFFFF {$ELSE}
$FFFF {$ENDIF}, nil, 0);
if Num > 0 then begin
DragQueryFile(Msg.Drop, 0, PChar(@AFileName), Pred(SizeOf(AFileName)));
Application.BringToFront;
Self.LoadFile(StrPas(AFileName));
end;
finally
DragFinish(Msg.Drop);
end;
end;
procedure TPictureEditDialog.UpdatePathsMenu;
var
I: Integer;
begin
for I := 0 to PathsMenu.Items.Count - 1 do begin
PathsMenu.Items[I].Checked := CompareText(PathsMenu.Items[I].Caption,
FileDialog.InitialDir) = 0;
end;
end;
procedure TPictureEditDialog.ClearClick(Sender: TObject);
begin
Pic.Graphic := nil;
ImagePaintBox.Invalidate;
Save.Enabled := False;
Clear.Enabled := False;
Copy.Enabled := False;
end;
procedure TPictureEditDialog.HelpBtnClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
const
sBackColorIdent = 'ClipboardBackColor';
sFileDir = 'FileDialog.InitialDir';
procedure TPictureEditDialog.FormStorageRestorePlacement(Sender: TObject);
begin
FIconColor := TColor(IniReadInteger(FormStorage.IniFileObject,
FormStorage.IniSection, sBackColorIdent, clBtnFace));
FileDialog.InitialDir := IniReadString(FormStorage.IniFileObject,
FormStorage.IniSection, sFileDir, FileDialog.InitialDir);
end;
procedure TPictureEditDialog.FormStorageSavePlacement(Sender: TObject);
begin
IniWriteInteger(FormStorage.IniFileObject, FormStorage.IniSection,
sBackColorIdent, FIconColor);
IniWriteString(FormStorage.IniFileObject, FormStorage.IniSection,
sFileDir, FileDialog.InitialDir);
end;
procedure TPictureEditDialog.PathsClick(Sender: TObject);
begin
if EditFolderList(PathsMRU.Strings) then
UpdatePathsMenu;
end;
procedure TPictureEditDialog.PathsMRUClick(Sender: TObject;
const RecentName, Caption: string; UserData: Longint);
begin
if DirExists(RecentName) then begin
{SetCurrentDir(RecentName);}
FileDialog.InitialDir := RecentName;
end
else begin
PathsMRU.Remove(RecentName);
end;
UpdatePathsMenu;
end;
procedure TPictureEditDialog.PathsMenuPopup(Sender: TObject);
begin
UpdatePathsMenu;
end;
procedure TPictureEditDialog.PathsMRUChange(Sender: TObject);
begin
PathsBtn.Enabled := PathsMRU.Strings.Count > 0;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -