问题来源: http://www.cnblogs.com/del/archive/2009/03/18/1410030.html#1480550
本例效果图:
![](https://www.cnblogs.com/images/cnblogs_com/del/179593/o_0931801.png)
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; SaveDialog1: TSaveDialog; procedure Button1Click(Sender: TObject); procedure SaveDialog1SelectionChange(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} const FileName = 'ABC.txt'; {变灰并维持指定的文件名} procedure EnableFileName(h: HWND; name: string); var hc: HWND; buf: array[Byte] of Char; begin h := GetWindow(h, GW_HWNDFIRST); while h <> 0 do begin if GetParent(h) = Form1.Handle then begin hc := GetWindow(h, GW_CHILD); while hc <> 0 do begin GetClassName(hc, buf, SizeOf(buf)); if buf = 'ComboBoxEx32' then begin EnableWindow(hc, False); SetWindowText(hc, name); Exit; end; hc := GetWindow(hc, GW_HWNDNEXT); end; end; h := GetWindow(h, GW_HWNDNEXT); end; end; {其实变灰只是假象, 使用时还需要调整下} procedure TForm1.Button1Click(Sender: TObject); var path: string; begin SaveDialog1.FileName := FileName; SaveDialog1.OptionsEx := [ofExNoPlacesBar]; if SaveDialog1.Execute(Handle) then {注意这里 Execute 参数是当前窗口的句柄} begin path := SaveDialog1.FileName; path := ExtractFilePath(path) + FileName; ShowMessage(path); end; end; {在 SaveDialog 的 OnSelectionChange 事件中调用上面的自定义过程 EnableFileName} procedure TForm1.SaveDialog1SelectionChange(Sender: TObject); begin EnableFileName(Handle, FileName); end; end.
窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 129 ClientWidth = 225 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 128 Top = 83 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object SaveDialog1: TSaveDialog OnSelectionChange = SaveDialog1SelectionChange Left = 24 Top = 16 end end