使 SaveDialog 对话框的文件名选择变灰 - 回复 "delphan" 的问题

本文介绍了一个使用Delphi实现的窗体控件禁用效果的示例程序。通过自定义过程使文件名输入框变为灰色且固定为ABC.txt,并在保存对话框选择更改时触发相应事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


问题来源: http://www.cnblogs.com/del/archive/2009/03/18/1410030.html#1480550

本例效果图:

26153931_KyZR.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

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/320842

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值