delphi 使 SaveDialog 对话框的文件名选择变灰  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 使 SaveDialog 对话框的文件名选择变灰


本例效果图:



代码文件:
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

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.1413950920105 seconds