Office VBA用WinAPI实现“另存为”对话框

Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Dim OFName As OPENFILENAME

Private Function ShowSave() As String
    OFName.lStructSize = Len(OFName)
    OFName.hwndOwner = Application.Hwnd
    OFName.lpstrFilter &

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Delphi中,可以使用TColorDialog组件实现颜色选择对话框。如果想要自定义颜色选择对话框,可以继承TColorDialog并重写其中的一些方法。 下面是一个简单的示例代码,演示如何实现自定义颜色选择对话框: ```delphi unit CustomColorDialog; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls; type TCustomColorDialog = class(TColorDialog) private FCustomColors: array[0..15] of TColor; protected procedure DoShow; override; procedure PaintRect(Canvas: TCanvas; Rect: TRect; Color: TColor); virtual; public constructor Create(AOwner: TComponent); override; published property CustomColors: array[0..15] of TColor read FCustomColors write FCustomColors; end; procedure Register; implementation {$R *.dfm} constructor TCustomColorDialog.Create(AOwner: TComponent); begin inherited; FCustomColors[0] := clBlack; FCustomColors[1] := clMaroon; FCustomColors[2] := clGreen; FCustomColors[3] := clOlive; FCustomColors[4] := clNavy; FCustomColors[5] := clPurple; FCustomColors[6] := clTeal; FCustomColors[7] := clGray; FCustomColors[8] := clSilver; FCustomColors[9] := clRed; FCustomColors[10] := clLime; FCustomColors[11] := clYellow; FCustomColors[12] := clBlue; FCustomColors[13] := clFuchsia; FCustomColors[14] := clAqua; FCustomColors[15] := clWhite; end; procedure TCustomColorDialog.DoShow; var I: Integer; BtnRect, ColorRect: TRect; BtnWidth, BtnHeight, ColorWidth, ColorHeight: Integer; BtnTop, ColorTop, ColorLeft: Integer; BtnCaption: string; BtnFont: TFont; BtnColor: TColor; Canvas: TCanvas; begin inherited; Canvas := TCanvas.Create; try Canvas.Handle := GetDC(Handle); BtnWidth := 50; BtnHeight := 20; ColorWidth := 15; ColorHeight := 15; BtnTop := ClientHeight - BtnHeight - 10; ColorTop := 10; ColorLeft := ClientWidth - 20 - (4 * ColorWidth); BtnFont := TFont.Create; try BtnFont.Assign(Font); BtnFont.Style := [fsBold]; for I := 0 to 3 do begin BtnRect := Rect(20 + (I * BtnWidth), BtnTop, 20 + ((I + 1) * BtnWidth), BtnTop + BtnHeight); BtnColor := CustomColors[I]; PaintRect(Canvas, BtnRect, BtnColor); BtnCaption := 'Color ' + IntToStr(I + 1); Canvas.Font := BtnFont; Canvas.Brush.Style := bsClear; DrawText(Canvas.Handle, PChar(BtnCaption), -1, BtnRect, DT_SINGLELINE or DT_CENTER or DT_VCENTER); for J := 0 to 3 do begin ColorRect := Rect(ColorLeft + (J * ColorWidth), ColorTop + (I * ColorHeight), ColorLeft + ((J + 1) * ColorWidth), ColorTop + ((I + 1) * ColorHeight)); PaintRect(Canvas, ColorRect, CustomColors[(I * 4) + J]); end; end; finally BtnFont.Free; end; finally ReleaseDC(Handle, Canvas.Handle); Canvas.Free; end; end; procedure TCustomColorDialog.PaintRect(Canvas: TCanvas; Rect: TRect; Color: TColor); begin Canvas.Brush.Color := Color; Canvas.Pen.Color := Color; Canvas.Rectangle(Rect); end; procedure Register; begin RegisterComponents('Samples', [TCustomColorDialog]); end; end. ``` 以上代码中,我们创建了一个名为TCustomColorDialog的自定义颜色选择对话框。在DoShow方法中,我们使用TCanvas绘制了自定义颜色区域和按钮区域。在PaintRect方法中,我们绘制了矩形,并填充了指定的颜色。 要使用自定义颜色选择对话框,只需将TCustomColorDialog组件放置在窗体中,并将其Execute方法调用即可:CustomColorDialog1.Execute。 希望这个示例对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值