如何在 Windows 主题下使用 Delphi 显示“变灰”的只读复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5240555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:22:29  来源:igfitidea点击:

How to display a "greyed-out" read-only checkbox using Delphi under windows themes

windowsdelphithemesreadonlytcheckbox

提问by Anonymous

I want read-only check boxes to be greyed out, but display their checked/unchecked status under Windows (XP and above), but I'm having some issues.

我希望只读复选框变灰,但在 Windows(XP 及更高版本)下显示其选中/未选中状态,但我遇到了一些问题。

NOTE - Regarding 'read-only': It appears that Delphi's TCheckBox, doesn't even have a read-only option, this has been 'faked' by placing it on a TPanel and disabling that... However the question still remains valid, how does one achieve a read-only check-box that is greyed out, OR a inactive check-box that displays it's state.

注意 - 关于“只读”:似乎 Delphi 的 TCheckBox 甚至没有只读选项,这是通过将其放置在 TPanel 上并禁用它来“伪造”的……但是问题仍然有效,如何实现一个显示为灰色的只读复选框,或一个显示其状态的非活动复选框。

Disabled checkboxes are greyed out, but these don't display a checked or unchecked state. Read-only check boxes can, but when Windows themes them, they just look like normal editable check boxes. The read-only box cannot have its value changed, but it looks like it can.

禁用的复选框呈灰色,但不会显示选中或未选中状态。只读复选框可以,但是当 Windows 主题化它们时,它们看起来就像普通的可编辑复选框。只读框无法更改其值,但看起来可以。

In XP with themes turned off (i.e. under classic mode), it works correctly.

在关闭主题的 XP 中(即在经典模式下),它可以正常工作。

Solutions that aren't acceptable, due to how clumsy/unprofessional they are for a large app or the development time/cash ratio of it, include these: - Manually greying the text and displaying an image of the checkbox status. - Disabling themes on the check-boxes, as the look without them is ugly. - Using custom checkboxes

由于大型应用程序的笨拙/不专业或它的开发时间/现金比率而无法接受的解决方案包括: - 手动使文本变灰并显示复选框状态的图像。- 禁用复选框上的主题,因为没有它们的外观很难看。- 使用自定义复选框

Screenshots of the issue - These are three checked check boxes, one disabled, one read only and one normal:

问题的屏幕截图 - 这些是三个选中的复选框,一个禁用,一个只读,一个正常:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Although the read-only and editable check boxes appear different, that's just because the editable box in the first image has the focus. The read-only one will look the same if it's the one with the focus, as see in the second image.

虽然只读和可编辑复选框看起来不同,但这只是因为第一张图像中的可编辑框具有焦点。如果它是具有焦点的只读的,则它看起来是一样的,如第二张图片所示。

采纳答案by David Heffernan

Anonymous has asked for code that demonstrates disabled check boxes showing their checked state.

Anonymous 要求提供代码来演示显示其选中状态的禁用复选框。

program Project28;

uses
  Forms, StdCtrls;

var
  Form: TForm;

procedure Initialise;
var
  cb1, cb2: TCheckBox;
begin
  cb1 := TCheckBox.Create(Form);
  cb2 := TCheckBox.Create(Form);
  cb1.Parent := Form;
  cb2.Parent := Form;
  cb1.Top := 0;
  cb2.Top := 16;
  cb1.Enabled := False;
  cb2.Enabled := False;
  cb1.Checked := False;
  cb2.Checked := True;
  cb1.Caption := 'Checkbox1';
  cb2.Caption := 'Checkbox2';
end;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm, Form);
  Initialise;
  Application.Run;
end.

enter image description here

在此处输入图片说明

回答by jachguate

Checkboxes with themes show the checked mark when disabled, as you can see in this screenshot:

带有主题的复选框在禁用时显示选中标记,如您在此屏幕截图中所见:

enter image description here

在此处输入图片说明

The dfm used to create this looks like this:

用于创建它的 dfm 如下所示:

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 337
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object CheckBox1: TCheckBox
    Left = 8
    Top = 8
    Width = 153
    Height = 17
    Caption = 'Disabled an checked'
    Checked = True
    Enabled = False
    State = cbChecked
    TabOrder = 0
  end
  object CheckBox2: TCheckBox
    Left = 8
    Top = 31
    Width = 153
    Height = 17
    Caption = 'Enabled and checked'
    Checked = True
    State = cbChecked
    TabOrder = 1
  end
  object CheckBox3: TCheckBox
    Left = 8
    Top = 54
    Width = 153
    Height = 17
    Caption = 'Disabled an un-checked'
    Enabled = False
    TabOrder = 2
  end
  object CheckBox4: TCheckBox
    Left = 8
    Top = 77
    Width = 153
    Height = 17
    Caption = 'Enabled and un-checked'
    TabOrder = 3
  end
end