开发者

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

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.

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.

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.

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

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:

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

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

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.


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

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

The dfm used to create this looks like this:

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


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

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜