bitsform.pas

来自「Delphi高级开发指南是开发程序的好帮手」· PAS 代码 · 共 78 行

PAS
78
字号
unit BitsForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    Edit1: TEdit;
    UpDown1: TUpDown;
    procedure Edit1Change(Sender: TObject);
    procedure CheckBoxClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function IsBitOn (Value: Integer; Bit: Byte): Boolean;
begin
  Result := (Value and (1 shl Bit)) <> 0;
end;

function TurnBitOn (Value: Integer; Bit: Byte): Integer;
begin
  Result := Value or (1 shl Bit);
end;

function TurnBitOff (Value: Integer; Bit: Byte): Integer;
begin
  Result := Value and not (1 shl Bit);
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
  I: Integer;
  CheckBox: TCheckBox;
begin
  for I := 1 to 5 do
  begin
    Checkbox := FindComponent (
      'Checkbox' + IntToStr (I)) as TCheckbox;
    CheckBox.Checked :=
      IsBitOn (UpDown1.Position, CheckBox.Tag);
  end;
end;

procedure TForm1.CheckBoxClick(Sender: TObject);
var
  Val: Integer;
begin
  Val := UpDown1.Position;
  with Sender as TCheckBox do
    if Checked then
      Val := TurnBitOn (Val, Tag)
    else
      Val := TurnBitOff (Val, Tag);
  UpDown1.Position := Val;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?