📄 dialogs.pas
字号:
for Option := Low(Option) to High(Option) do
if Option in FOptions then
Flags := Flags or OpenOptions[Option];
if NewStyleControls then
begin
Flags := Flags xor OFN_EXPLORER;
if (Win32MajorVersion >= 5) and (Win32Platform = VER_PLATFORM_WIN32_NT) or { Win2k }
((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and (Win32MajorVersion >= 4) and (Win32MinorVersion >= 90)) then { WinME }
for OptionEx := Low(OptionEx) to High(OptionEx) do
if OptionEx in FOptionsEx then
FlagsEx := FlagsEx or OpenOptionsEx[OptionEx];
end
else
Flags := Flags and not OFN_EXPLORER;
TempExt := FDefaultExt;
if (TempExt = '') and (Flags and OFN_EXPLORER = 0) then
begin
TempExt := ExtractFileExt(FFilename);
Delete(TempExt, 1, 1);
end;
if TempExt <> '' then lpstrDefExt := PChar(TempExt);
if (ofOldStyleDialog in Options) or not NewStyleControls then
lpfnHook := DialogHook
else
lpfnHook := ExplorerHook;
if Template <> nil then
begin
Flags := Flags or OFN_ENABLETEMPLATE;
lpTemplateName := Template;
end;
hWndOwner := Application.Handle;
Result := TaskModalDialog(Func, OpenFileName);
if Result then
begin
GetFileNames(OpenFilename);
if (Flags and OFN_EXTENSIONDIFFERENT) <> 0 then
Include(FOptions, ofExtensionDifferent) else
Exclude(FOptions, ofExtensionDifferent);
if (Flags and OFN_READONLY) <> 0 then
Include(FOptions, ofReadOnly) else
Exclude(FOptions, ofReadOnly);
FFilterIndex := nFilterIndex;
end;
end;
end;
procedure TOpenDialog.GetFileNames(var OpenFileName: TOpenFileName);
var
Separator: Char;
function ExtractFileName(P: PChar; var S: TFilename): PChar;
begin
Result := AnsiStrScan(P, Separator);
if Result = nil then
begin
S := P;
Result := StrEnd(P);
end
else
begin
SetString(S, P, Result - P);
Inc(Result);
end;
end;
procedure ExtractFileNames(P: PChar);
var
DirName, FileName: TFilename;
begin
P := ExtractFileName(P, DirName);
P := ExtractFileName(P, FileName);
if FileName = '' then
FFiles.Add(DirName)
else
begin
if AnsiLastChar(DirName)^ <> '\' then
DirName := DirName + '\';
repeat
if (FileName[1] <> '\') and ((Length(FileName) <= 3) or
(FileName[2] <> ':') or (FileName[3] <> '\')) then
FileName := DirName + FileName;
FFiles.Add(FileName);
P := ExtractFileName(P, FileName);
until FileName = '';
end;
end;
begin
Separator := #0;
if (ofAllowMultiSelect in FOptions) and
((ofOldStyleDialog in FOptions) or not NewStyleControls) then
Separator := ' ';
with OpenFileName do
begin
if ofAllowMultiSelect in FOptions then
begin
ExtractFileNames(lpstrFile);
FFileName := FFiles[0];
end else
begin
ExtractFileName(lpstrFile, FFileName);
FFiles.Add(FFileName);
end;
end;
end;
function TOpenDialog.GetStaticRect: TRect;
begin
if FHandle <> 0 then
begin
if not (ofOldStyleDialog in Options) then
begin
GetWindowRect(GetDlgItem(FHandle, stc32), Result);
MapWindowPoints(0, FHandle, Result, 2);
end
else GetClientRect(FHandle, Result)
end
else Result := Rect(0,0,0,0);
end;
function TOpenDialog.GetFileName: TFileName;
var
Path: array[0..MAX_PATH] of Char;
begin
if NewStyleControls and (FHandle <> 0) then
begin
SendMessage(GetParent(FHandle), CDM_GETFILEPATH, SizeOf(Path), Integer(@Path));
Result := StrPas(Path);
end
else Result := FFileName;
end;
function TOpenDialog.GetFilterIndex: Integer;
begin
if FHandle <> 0 then
Result := FCurrentFilterIndex
else
Result := FFilterIndex;
end;
procedure TOpenDialog.SetHistoryList(Value: TStrings);
begin
FHistoryList.Assign(Value);
end;
procedure TOpenDialog.SetInitialDir(const Value: string);
var
L: Integer;
begin
L := Length(Value);
if (L > 1) and IsPathDelimiter(Value, L)
and not IsDelimiter(':', Value, L - 1) then Dec(L);
FInitialDir := Copy(Value, 1, L);
end;
function TOpenDialog.Execute: Boolean;
begin
Result := DoExecute(@GetOpenFileName);
end;
procedure TOpenDialog.DoIncludeItem(const OFN: TOFNotifyEx; var Include: Boolean);
begin
if Assigned(FOnIncludeItem) then FOnIncludeItem(OFN, Include);
end;
{ TSaveDialog }
function TSaveDialog.Execute: Boolean;
begin
Result := DoExecute(@GetSaveFileName);
end;
{ TColorDialog }
constructor TColorDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCustomColors := TStringList.Create;
end;
destructor TColorDialog.Destroy;
begin
FCustomColors.Free;
inherited Destroy;
end;
function TColorDialog.Execute: Boolean;
const
DialogOptions: array[TColorDialogOption] of DWORD = (
CC_FULLOPEN, CC_PREVENTFULLOPEN, CC_SHOWHELP, CC_SOLIDCOLOR,
CC_ANYCOLOR);
var
ChooseColorRec: TChooseColor;
Option: TColorDialogOption;
CustomColorsArray: TCustomColors;
const
ColorPrefix = 'Color';
procedure GetCustomColorsArray;
var
I: Integer;
begin
for I := 0 to MaxCustomColors - 1 do
FCustomColors.Values[ColorPrefix + Char(Ord('A') + I)] :=
Format('%.6x', [CustomColorsArray[I]]);
end;
procedure SetCustomColorsArray;
var
Value: string;
I: Integer;
begin
for I := 0 to MaxCustomColors - 1 do
begin
Value := FCustomColors.Values[ColorPrefix + Char(Ord('A') + I)];
if Value <> '' then
CustomColorsArray[I] := StrToInt('$' + Value) else
CustomColorsArray[I] := -1;
end;
end;
begin
with ChooseColorRec do
begin
SetCustomColorsArray;
lStructSize := SizeOf(ChooseColorRec);
hInstance := SysInit.HInstance;
rgbResult := ColorToRGB(FColor);
lpCustColors := @CustomColorsArray;
Flags := CC_RGBINIT or CC_ENABLEHOOK;
for Option := Low(Option) to High(Option) do
if Option in FOptions then
Flags := Flags or DialogOptions[Option];
if Template <> nil then
begin
Flags := Flags or CC_ENABLETEMPLATE;
lpTemplateName := Template;
end;
lpfnHook := DialogHook;
hWndOwner := Application.Handle;
Result := TaskModalDialog(@ChooseColor, ChooseColorRec);
if Result then
begin
FColor := rgbResult;
GetCustomColorsArray;
end;
end;
end;
procedure TColorDialog.SetCustomColors(Value: TStrings);
begin
FCustomColors.Assign(Value);
end;
{ TFontDialog }
const
IDAPPLYBTN = $402;
var
FontDialog: TFontDialog;
function FontDialogHook(Wnd: HWnd; Msg: UINT; WParam: WPARAM; LParam: LPARAM): UINT; stdcall;
begin
if (Msg = WM_COMMAND) and (LongRec(WParam).Lo = IDAPPLYBTN) and
(LongRec(WParam).Hi = BN_CLICKED) then
begin
FontDialog.DoApply(Wnd);
Result := 1;
end else
Result := DialogHook(Wnd, Msg, wParam, lParam);
end;
constructor TFontDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFont := TFont.Create;
FOptions := [fdEffects];
end;
destructor TFontDialog.Destroy;
begin
FFont.Free;
inherited Destroy;
end;
procedure TFontDialog.WndProc(var Message: TMessage);
begin
{ Make sure we only take values from the color combobox and script combobox
if they have been changed. }
if (Message.Msg = WM_COMMAND) and (Message.WParamHi = CBN_SELENDOK) then
if (Message.WParamLo = cmb4) then FFontColorModified := True
else if (Message.WParamLo = cmb5) then FFontCharsetModified := True;
inherited WndProc(Message);
end;
procedure TFontDialog.Apply(Wnd: HWND);
begin
if Assigned(FOnApply) then FOnApply(Self, Wnd);
end;
procedure TFontDialog.DoApply(Wnd: HWND);
const
IDCOLORCMB = $473;
var
I: Integer;
LogFont: TLogFont;
begin
SendMessage(Wnd, WM_CHOOSEFONT_GETLOGFONT, 0, LongInt(@LogFont));
UpdateFromLogFont(LogFont);
I := SendDlgItemMessage(Wnd, IDCOLORCMB, CB_GETCURSEL, 0, 0);
if I <> CB_ERR then
Font.Color := SendDlgItemMessage(Wnd, IDCOLORCMB, CB_GETITEMDATA, I, 0);
try
Apply(Wnd);
except
Application.HandleException(Self);
end;
end;
function TFontDialog.Execute: Boolean;
const
FontOptions: array[TFontDialogOption] of DWORD = (
CF_ANSIONLY, CF_TTONLY, CF_EFFECTS, CF_FIXEDPITCHONLY, CF_FORCEFONTEXIST,
CF_NOFACESEL, CF_NOOEMFONTS, CF_NOSIMULATIONS, CF_NOSIZESEL,
CF_NOSTYLESEL, CF_NOVECTORFONTS, CF_SHOWHELP,
CF_WYSIWYG or CF_BOTH or CF_SCALABLEONLY, CF_LIMITSIZE,
CF_SCALABLEONLY, CF_APPLY);
Devices: array[TFontDialogDevice] of DWORD = (
CF_SCREENFONTS, CF_PRINTERFONTS, CF_BOTH);
var
ChooseFontRec: TChooseFont;
LogFont: TLogFont;
Option: TFontDialogOption;
SaveFontDialog: TFontDialog;
OriginalFaceName: string;
begin
with ChooseFontRec do
begin
lStructSize := SizeOf(ChooseFontRec);
hDC := 0;
if FDevice <> fdScreen then hDC := Printer.Handle;
lpLogFont := @LogFont;
GetObject(Font.Handle, SizeOf(LogFont), @LogFont);
OriginalFaceName := LogFont.lfFaceName;
Flags := Devices[FDevice] or (CF_INITTOLOGFONTSTRUCT or CF_ENABLEHOOK);
for Option := Low(Option) to High(Option) do
if Option in FOptions then
Flags := Flags or FontOptions[Option];
if Assigned(FOnApply) then Flags := Flags or CF_APPLY;
if Template <> nil then
begin
Flags := Flags or CF_ENABLETEMPLATE;
lpTemplateName := Template;
end;
rgbColors := Font.Color;
lCustData := 0;
lpfnHook := FontDialogHook;
nSizeMin := FMinFontSize;
nSizeMax := FMaxFontSize;
if nSizeMin > nSizeMax then Flags := Flags and (not CF_LIMITSIZE);
hWndOwner := Application.Handle;
SaveFontDialog := FontDialog;
FontDialog := Self;
FFontColorModified := False;
FFontCharsetModified := False;
Result := TaskModalDialog(@ChooseFont, ChooseFontRec);
FontDialog := SaveFontDialog;
if Result then
begin
if AnsiCompareText(OriginalFaceName, LogFont.lfFaceName) <> 0 then
FFontCharsetModified := True;
UpdateFromLogFont(LogFont);
if FFontColorModified then Font.Color := rgbColors;
end;
end;
end;
procedure TFontDialog.SetFont(Value: TFont);
begin
FFont.Assign(Value);
end;
procedure TFontDialog.UpdateFromLogFont(const LogFont: TLogFont);
var
Style: TFontStyles;
begin
with LogFont do
begin
Font.Name := LogFont.lfFaceName;
Font.Height := LogFont.lfHeight;
if FFontCharsetModified then
Font.Charset := TFontCharset(LogFont.lfCharSet);
Style := [];
with LogFont do
begin
if lfWeight > FW_REGULAR then Include(Style, fsBold);
if lfItalic <> 0 then Include(Style, fsItalic);
if lfUnderline <> 0 then Include(Style, fsUnderline);
if lfStrikeOut <> 0 then Include(Style, fsStrikeOut);
end;
Font.Style := Style;
end;
end;
{ Printer dialog routines }
procedure GetPrinter(var DeviceMode, DeviceNames: THandle);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -